From bf72bde301d1ff44780ea19c7846cc8269f087bd Mon Sep 17 00:00:00 2001 From: Martin Folkers Date: Sat, 3 Mar 2018 12:58:10 +0100 Subject: [PATCH] Releasing v0.4.0 OOP rewrite --- core/generate_webp.php | 108 ++++++++++++++++++----------------------- kirby-webp.php | 17 +++++-- package.json | 2 +- 3 files changed, 62 insertions(+), 65 deletions(-) diff --git a/core/generate_webp.php b/core/generate_webp.php index 5b9d587..d76bc2d 100644 --- a/core/generate_webp.php +++ b/core/generate_webp.php @@ -1,67 +1,53 @@ option('thumb.quality') ? - 'actions' => ['upload'], - 'quality' => 90, // Desired WebP compression quality - 'stripMetadata' => TRUE, - 'serveConverted' => FALSE, - 'serveOriginalOnFail' => TRUE, - 'preferredConverters' => ['gd', 'webp', 'imagick'] // TODO: include 'thumbs.driver' - ]; - - // If config settings exist, return the config with fallback - if(isset($settings) && array_key_exists($name, $settings)) { - return c::get($prefix . $name, $settings[$name]); - } + namespace Kirby\Plugins\WebP; + + use c; + use WebPConvert; + +/* + * TODO: + * + * $kirby->option('thumb.quality') && 'thumbs.driver' + * + */ + +class Convert { + + private $quality; + private $strip; + private $serve; + private $log; + private $converters; + + public function __construct() { + $this->quality = c::get('plugin.kirby-webp.quality', 90); + $this->strip = c::get('plugin.kirby-webp.stripMetadata', TRUE); + $this->serve = c::get('plugin.kirby-webp.converters', TRUE); + $this->log = c::get('plugin.kirby-webp.serveOriginalOnFail', TRUE); + $this->converters = c::get('plugin.kirby-webp.converters', ['gd', 'cwebp']); } -} - -foreach (settings::actions() as $action) { - kirby()->hook('panel.file.' . $action, 'generateWebP'); -} - -function generateWebP($file) { - - try { - - // Checking file type since only images are processed - if ($file->type() == 'image') { - - // Defining image-related options - $input = $file->dir() . '/' . $file->filename(); - $output = $file->dir() . '/' . $file->name() . '.webp'; - $quality = settings::quality(); - $strip = settings::stripMetadata(); - - // Defining WebPConvert-related options - WebPConvert::$serve_converted_image = settings::serveConverted(); - WebPConvert::$serve_original_image_on_fail = settings::serveOriginalOnFail(); - WebPConvert::set_preferred_converters(settings::preferredConverters()); - - // Generating WebP image & placing it alongside the original version - WebPConvert::convert($input, $output, $quality, $strip); + + public function generateWebP($file) { + + try { + // Checking file type since only images are processed + if ($file->type() == 'image') { + + // WebPConvert options + $input = $file->dir() . '/' . $file->filename(); + $output = $file->dir() . '/' . $file->name() . '.webp'; + + WebPConvert::$serve_original_image_on_fail = $this->serve; + WebPConvert::$serve_converted_image = $this->log; + WebPConvert::set_preferred_converters($this->converters); + // WebPConvert::$debug = FALSE; + + // Generating WebP image & placing it alongside the original version + WebPConvert::convert($input, $output, $this->quality, $this->strip); + } + } catch (Exception $e) { + return response::error($e->getMessage()); } - } catch (Exception $e) { - return response::error($e->getMessage()); } } diff --git a/kirby-webp.php b/kirby-webp.php index 68e0c8b..bafa9da 100644 --- a/kirby-webp.php +++ b/kirby-webp.php @@ -1,12 +1,12 @@ * @link http://twobrain.io - * @version 0.1.0 + * @version 0.4.0 * @license MIT */ @@ -16,4 +16,15 @@ require_once __DIR__ . DS . 'vendor' . DS . 'autoload.php'; // Loading settings & core -include_once __DIR__ . DS . 'core' . DS . 'generate_webp.php'; +function webp() { + require_once __DIR__ . DS . 'core' . DS . 'generate_webp.php'; + return new Kirby\Plugins\WebP\Convert; +} + +$hooks = c::get('plugin.kirby-webp.hooks', ['upload']); + +foreach ($hooks as $hook) { + kirby()->hook('panel.file.' . $hook, function ($file) { + webp()->generateWebP($file); + }); +} diff --git a/package.json b/package.json index f7a2c1b..de7fb5a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "kirby-webp", "description": "WebP generation for Kirby", "author": "S1SYPHOS ", - "version": "0.1.0", + "version": "0.4.0", "type": "kirby-plugin", "license": "MIT" }