This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OOP rewrite
- Loading branch information
Showing
3 changed files
with
62 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,53 @@ | ||
<?php | ||
|
||
// namespace S1SYPHOS\WEBP; | ||
|
||
// use c; | ||
// use WebPConvert; | ||
|
||
class Settings { | ||
|
||
/** | ||
* Returns the default options for `kirby-webp` | ||
* | ||
* @return array | ||
*/ | ||
|
||
public static function __callStatic($name, $args) { | ||
|
||
// Set prefix | ||
$prefix = 'plugin.kirby-webp.'; | ||
|
||
// Set config names and fallbacks as settings | ||
$settings = [ | ||
// TODO: $kirby->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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
<?php | ||
|
||
/** | ||
* Kirby WebP - INSERT DESCRIPTION | ||
* Kirby WebP - WebP generation for Kirby | ||
* | ||
* @package Kirby CMS | ||
* @author S1SYPHOS <[email protected]> | ||
* @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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"name": "kirby-webp", | ||
"description": "WebP generation for Kirby", | ||
"author": "S1SYPHOS <[email protected]>", | ||
"version": "0.1.0", | ||
"version": "0.4.0", | ||
"type": "kirby-plugin", | ||
"license": "MIT" | ||
} |