Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
Releasing v0.4.0
Browse files Browse the repository at this point in the history
OOP rewrite
  • Loading branch information
S1SYPHOS authored Mar 3, 2018
1 parent 964737f commit bf72bde
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 65 deletions.
108 changes: 47 additions & 61 deletions core/generate_webp.php
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());
}
}
17 changes: 14 additions & 3 deletions kirby-webp.php
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
*/

Expand All @@ -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);
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit bf72bde

Please sign in to comment.