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

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
S1SYPHOS committed Feb 24, 2018
0 parents commit 11a8ac0
Show file tree
Hide file tree
Showing 17 changed files with 839 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
composer.lock
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Martin Folkers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
102 changes: 102 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Kirby WebP
[![Release](https://img.shields.io/github/release/S1SYPHOS/kirby-webp.svg?color="brightgreen")](https://github.com/S1SYPHOS/kirby-webp/releases) [![License](https://img.shields.io/github/license/S1SYPHOS/kirby-webp.svg)](https://github.com/S1SYPHOS/kirby-webp/blob/master/LICENSE) [![Issues](https://img.shields.io/github/issues/S1SYPHOS/kirby-webp.svg)](https://github.com/S1SYPHOS/kirby-webp/issues)

This plugin generates `.webp` images alongside your uploaded `.jp(e)g` & `.png` versions - so **you** don't have to!

**Table of contents**
- [1. What is it good for?](#whats-is-it-good-for)
- [2. Getting started](#getting-started)
- [3. Configuration](#configuration)
- [4. Troubleshooting](#troubleshooting)
- [5. Credits / License](#credits--license)

## What is it good for?
Absolutely .. smaller image size!

## Getting started
Use one of the following methods to install & use `kirby-webp`:

### Git submodule

If you know your way around Git, you can download this plugin as a [submodule](https://github.com/blog/2104-working-with-submodules):

```text
git submodule add https://github.com/S1SYPHOS/kirby-webp.git site/plugins/kirby-webp
```

### Clone or download

1. [Clone](https://github.com/S1SYPHOS/kirby-webp.git) or [download](https://github.com/S1SYPHOS/kirby-webp/archive/master.zip) this repository.
2. Unzip / Move the folder to `site/plugins`.

### Activate the plugin
Activate the plugin with the following line in your `config.php`:

```text
c::set('plugin.kirby-webp', true);
```

## Configuration
After uploading some images, you are now officially ready to serve their newly generated WebP versions.

### Apache
If you're using [Apache](http://httpd.apache.org/) as your webserver, add the following lines to your `.htaccess` (right after `RewriteBase`):

```text
<IfModule mod_rewrite.c>
RewriteEngine On
# Checking for WebP browser support ..
RewriteCond %{HTTP_ACCEPT} image/webp
# .. and if there's a WebP version for the requested image
RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
# Well, then go for it & serve WebP instead
RewriteRule (.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1]
</IfModule>
<IfModule mod_headers.c>
Header append Vary Accept env=REDIRECT_accept
</IfModule>
<IfModule mod_mime.c>
AddType image/webp .webp
</IfModule>
```

### NGINX
If you're using [NGINX](https://nginx.org/en/) as your webserver, add the following lines to your virtual host setup (for more information, go [here](https://github.com/uhop/grunt-tight-sprite/wiki/Recipe:-serve-WebP-with-nginx-conditionally) or [there](https://optimus.keycdn.com/support/configuration-to-deliver-webp)):

```text
// First, make sure that NGINX' `mime.types` file includes 'image/webp webp'
include /etc/nginx/mime.types;
// Checking if HTTP's `ACCEPT` header contains 'webp'
map $http_accept $webp_suffix {
default "";
"~*webp" ".webp";
}
server {
// ...
// Checking if there's a WebP version for the requested image ..
location ~* ^.+\.(jpe?g|png)$ {
add_header Vary Accept;
// .. and if so, serving it
try_files $1$webp_ext $uri =404;
}
}
```

## Troubleshooting
Despite stating that `An unexpected error occurred`, WebP generation after renaming / updating images works - `panel.file.replace` doesn't work at all .. PRs are always welcome :champagne:

Because of that, only `panel.file.upload` is included by default. If you wish to investigate this further or don't care too much about the errror, go head with `c::set('plugin.webp.actions', ['upload', 'update', 'replace']);` in your `config.php`.

## Credits / License
`kirby-webp` is based on Bjørn Rosell's `convert-webp` library. It is licensed under the [MIT License](LICENSE), but **using Kirby in production** requires you to [buy a license](https://getkirby.com/buy). Are you ready for the [next step](https://getkirby.com/next)?

## Special Thanks
I'd like to thank everybody that's making great software - you people are awesome. Also I'm always thankful for feedback and bug reports :)
11 changes: 11 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/S1SYPHOS/webp-convert"
}
],
"require": {
"rosell-dk/webp-convert": "dev-master"
}
}
67 changes: 67 additions & 0 deletions core/generate_webp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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]);
}
}
}

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);
}
} catch (Exception $e) {
return response::error($e->getMessage());
}
}
19 changes: 19 additions & 0 deletions kirby-webp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Kirby WebP - INSERT DESCRIPTION
*
* @package Kirby CMS
* @author S1SYPHOS <[email protected]>
* @link http://twobrain.io
* @version 0.1.0
* @license MIT
*/

if(!c::get('plugin.kirby-webp')) return;

// Initialising composer's autoloader
require_once __DIR__ . DS . 'vendor' . DS . 'autoload.php';

// Loading settings & core
include_once __DIR__ . DS . 'core' . DS . 'generate_webp.php';
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "kirby-webp",
"description": "WebP generation for Kirby",
"author": "S1SYPHOS <[email protected]>",
"version": "0.1.0",
"type": "kirby-plugin",
"license": "MIT"
}
7 changes: 7 additions & 0 deletions vendor/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitf7b42677ece290731e6e707bbc57bdfe::getLoader();
Loading

0 comments on commit 11a8ac0

Please sign in to comment.