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

Commit 11a8ac0

Browse files
committed
Initial commit
0 parents  commit 11a8ac0

17 files changed

+839
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
composer.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Martin Folkers
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Kirby WebP
2+
[![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)
3+
4+
This plugin generates `.webp` images alongside your uploaded `.jp(e)g` & `.png` versions - so **you** don't have to!
5+
6+
**Table of contents**
7+
- [1. What is it good for?](#whats-is-it-good-for)
8+
- [2. Getting started](#getting-started)
9+
- [3. Configuration](#configuration)
10+
- [4. Troubleshooting](#troubleshooting)
11+
- [5. Credits / License](#credits--license)
12+
13+
## What is it good for?
14+
Absolutely .. smaller image size!
15+
16+
## Getting started
17+
Use one of the following methods to install & use `kirby-webp`:
18+
19+
### Git submodule
20+
21+
If you know your way around Git, you can download this plugin as a [submodule](https://github.com/blog/2104-working-with-submodules):
22+
23+
```text
24+
git submodule add https://github.com/S1SYPHOS/kirby-webp.git site/plugins/kirby-webp
25+
```
26+
27+
### Clone or download
28+
29+
1. [Clone](https://github.com/S1SYPHOS/kirby-webp.git) or [download](https://github.com/S1SYPHOS/kirby-webp/archive/master.zip) this repository.
30+
2. Unzip / Move the folder to `site/plugins`.
31+
32+
### Activate the plugin
33+
Activate the plugin with the following line in your `config.php`:
34+
35+
```text
36+
c::set('plugin.kirby-webp', true);
37+
```
38+
39+
## Configuration
40+
After uploading some images, you are now officially ready to serve their newly generated WebP versions.
41+
42+
### Apache
43+
If you're using [Apache](http://httpd.apache.org/) as your webserver, add the following lines to your `.htaccess` (right after `RewriteBase`):
44+
45+
```text
46+
<IfModule mod_rewrite.c>
47+
RewriteEngine On
48+
49+
# Checking for WebP browser support ..
50+
RewriteCond %{HTTP_ACCEPT} image/webp
51+
52+
# .. and if there's a WebP version for the requested image
53+
RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
54+
55+
# Well, then go for it & serve WebP instead
56+
RewriteRule (.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1]
57+
</IfModule>
58+
59+
<IfModule mod_headers.c>
60+
Header append Vary Accept env=REDIRECT_accept
61+
</IfModule>
62+
63+
<IfModule mod_mime.c>
64+
AddType image/webp .webp
65+
</IfModule>
66+
```
67+
68+
### NGINX
69+
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)):
70+
71+
```text
72+
// First, make sure that NGINX' `mime.types` file includes 'image/webp webp'
73+
include /etc/nginx/mime.types;
74+
75+
// Checking if HTTP's `ACCEPT` header contains 'webp'
76+
map $http_accept $webp_suffix {
77+
default "";
78+
"~*webp" ".webp";
79+
}
80+
81+
server {
82+
// ...
83+
84+
// Checking if there's a WebP version for the requested image ..
85+
location ~* ^.+\.(jpe?g|png)$ {
86+
add_header Vary Accept;
87+
// .. and if so, serving it
88+
try_files $1$webp_ext $uri =404;
89+
}
90+
}
91+
```
92+
93+
## Troubleshooting
94+
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:
95+
96+
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`.
97+
98+
## Credits / License
99+
`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)?
100+
101+
## Special Thanks
102+
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 :)

composer.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"repositories": [
3+
{
4+
"type": "vcs",
5+
"url": "https://github.com/S1SYPHOS/webp-convert"
6+
}
7+
],
8+
"require": {
9+
"rosell-dk/webp-convert": "dev-master"
10+
}
11+
}

core/generate_webp.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
// namespace S1SYPHOS\WEBP;
4+
5+
// use c;
6+
// use WebPConvert;
7+
8+
class Settings {
9+
10+
/**
11+
* Returns the default options for `kirby-webp`
12+
*
13+
* @return array
14+
*/
15+
16+
public static function __callStatic($name, $args) {
17+
18+
// Set prefix
19+
$prefix = 'plugin.kirby-webp.';
20+
21+
// Set config names and fallbacks as settings
22+
$settings = [
23+
// TODO: $kirby->option('thumb.quality') ?
24+
'actions' => ['upload'],
25+
'quality' => 90, // Desired WebP compression quality
26+
'stripMetadata' => TRUE,
27+
'serveConverted' => FALSE,
28+
'serveOriginalOnFail' => TRUE,
29+
'preferredConverters' => ['gd', 'webp', 'imagick'] // TODO: include 'thumbs.driver'
30+
];
31+
32+
// If config settings exist, return the config with fallback
33+
if(isset($settings) && array_key_exists($name, $settings)) {
34+
return c::get($prefix . $name, $settings[$name]);
35+
}
36+
}
37+
}
38+
39+
foreach (settings::actions() as $action) {
40+
kirby()->hook('panel.file.' . $action, 'generateWebP');
41+
}
42+
43+
function generateWebP($file) {
44+
45+
try {
46+
47+
// Checking file type since only images are processed
48+
if ($file->type() == 'image') {
49+
50+
// Defining image-related options
51+
$input = $file->dir() . '/' . $file->filename();
52+
$output = $file->dir() . '/' . $file->name() . '.webp';
53+
$quality = settings::quality();
54+
$strip = settings::stripMetadata();
55+
56+
// Defining WebPConvert-related options
57+
WebPConvert::$serve_converted_image = settings::serveConverted();
58+
WebPConvert::$serve_original_image_on_fail = settings::serveOriginalOnFail();
59+
WebPConvert::set_preferred_converters(settings::preferredConverters());
60+
61+
// Generating WebP image & placing it alongside the original version
62+
WebPConvert::convert($input, $output, $quality, $strip);
63+
}
64+
} catch (Exception $e) {
65+
return response::error($e->getMessage());
66+
}
67+
}

kirby-webp.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* Kirby WebP - INSERT DESCRIPTION
5+
*
6+
* @package Kirby CMS
7+
* @author S1SYPHOS <hello@twobrain.io>
8+
* @link http://twobrain.io
9+
* @version 0.1.0
10+
* @license MIT
11+
*/
12+
13+
if(!c::get('plugin.kirby-webp')) return;
14+
15+
// Initialising composer's autoloader
16+
require_once __DIR__ . DS . 'vendor' . DS . 'autoload.php';
17+
18+
// Loading settings & core
19+
include_once __DIR__ . DS . 'core' . DS . 'generate_webp.php';

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "kirby-webp",
3+
"description": "WebP generation for Kirby",
4+
"author": "S1SYPHOS <hello@twobrain.io>",
5+
"version": "0.1.0",
6+
"type": "kirby-plugin",
7+
"license": "MIT"
8+
}

vendor/autoload.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// autoload.php @generated by Composer
4+
5+
require_once __DIR__ . '/composer/autoload_real.php';
6+
7+
return ComposerAutoloaderInitf7b42677ece290731e6e707bbc57bdfe::getLoader();

0 commit comments

Comments
 (0)