项目作者: MRFD

项目描述 :
This plugin for Kirby 3 provides automatic WebP conversion when uploading images via the Panel.
高级语言: PHP
项目地址: git://github.com/MRFD/kirby-webp.git
创建时间: 2020-04-23T11:33:41Z
项目社区:https://github.com/MRFD/kirby-webp

开源协议:MIT License

下载


🖼 Kirby 3 WebP

[!IMPORTANT]
This plugin is no longer actively maintained, as WebP can now be generated natively in Kirby 4.

This plugin for Kirby 3 provides automatic WebP conversion when uploading images (JPG and PNG) via the Panel. With provisions for the use of field methods, KirbyTags and multi-language sites.

Note: When using the field methods and KirbyTags the WebP image will be automatically generated when missing. In addition the WebP image will also be removed when the original image is removed via the Panel.

Commerical Usage

This plugin is free but if you use it in a commercial project please consider to

Requirements

  • PHP 7.3+
  • Kirby 3

Installation

Download

Download and copy the files to /site/plugins/kirby-webp.

Git submodule

  1. $ git submodule add https://github.com/MRFD/kirby-webp.git site/plugins/kirby-webp

Composer

  1. composer require MRFD/kirby-webp

Usage

Field methods

\$field->picture()

Converts the field to a picture tag with WebP and fallback for unsupported browsers.

  1. <?php
  2. if ($image = $page->image('my-image.jpg')) {
  3. echo $image->picture('some-class', 'Image description');
  4. }
  5. ?>
  1. <picture class="some-class">
  2. <source srcset="/your/path/my-image.webp" type="image/webp" ></source>
  3. <img src="/your/path/my-image.jpg" alt="Image description" />
  4. </picture>

\$field->webp()

Converts the field to a WebP image tag.

  1. <?php
  2. if ($image = $page->image('my-image.jpg')) {
  3. echo $image->webp('some-class', 'Image description', [300, 800, 1024]);
  4. }
  5. ?>
  1. <img
  2. src="/your/path/my-image.webp"
  3. srcset="
  4. /your/path/my-image-300.webp 300w,
  5. /your/path/my-image-800.webp 800w,
  6. /your/path/my-image-1024.webp 1024w
  7. "
  8. class="some-class"
  9. alt="Image description"
  10. />
  11. <!-- Or when the browser does not support WebP: -->
  12. <img
  13. src="/your/path/my-image.jpg"
  14. srcset="
  15. /your/path/my-image-300.jpg 300w,
  16. /your/path/my-image-800.jpg 800w,
  17. /your/path/my-image-1024.jpg 1024w
  18. "
  19. class="some-class"
  20. alt="Image description"
  21. />

All arguments are optional.

It is also possible to use the predefined srcset. For the default option use: $image->webp('some-class', 'Image description', 'default').

\$field->isSupported()

Validates the WebP format is supported by the visitor’s browser.

  1. <?php
  2. $image = $page->image('my-image.jpg');
  3. if ($image->isSupported()) {
  4. echo "Browser supports WebP! :)";
  5. } else {
  6. echo "Browser doesn't support WebP.";
  7. }
  8. ?>

\$field->backgroundImage()

Returns an image url that can be used with inline CSS, for example with background images. Based on the visitor’s browser the url provides a regular or WebP format.

  1. <?php if ($image = $page->image('my-image.jpg')) : ?>
  2. <div style="background-image: url('<?= $image->backgroundImage() ?>')"></div>
  3. <?php endif ?>
  1. <div style="background-image: url('/your/path/my-image.webp')"></div>
  2. <!-- Or when the browser does not support WebP: -->
  3. <div style="background-image: url('/your/path/my-image.jpg')"></div>

KirbyTags

Picture tag

Creates an picture tag with fallback for unsupported browsers.

  1. (picture: myawesomepicture.jpg)
  1. (picture: myawesomepicture.jpg class: floated)
  1. (picture: myawesomepicture.jpg alt: This is an awesome picture)
  1. <picture class="some-class">
  2. <source srcset="/your/path/my-image.webp" type="image/webp" ></source>
  3. <img src="/your/path/my-image.jpg" alt="Image description" />
  4. </picture>

Image tag

Creates an image tag with WebP file. Can be a regular image, or a WebP format.

  1. (webp: myawesomepicture.jpg)
  1. (webp: myawesomepicture.jpg class: floated)
  1. (webp: myawesomepicture.jpg alt: This is an awesome picture)
  1. <img
  2. src="/your/path/my-image.webp"
  3. class="some-class"
  4. alt="Image description"
  5. />
  6. <!-- Or when the browser does not support WebP: -->
  7. <img src="/your/path/my-image.jpg" class="some-class" alt="Image description" />

Options

Convert on the fly

It is possible to convert images to WebP without uploading them through the panel. Add the following option to /site/config/config.php:

  1. # site/config/config.php
  2. return [
  3. 'mrfd.webp.autoconvert' => true
  4. ];

Note: Enabling this option may slow down the website. It is therefore advised to upload the images via the panel!

Conversion options

For configuring advanced WebP conversion settings. For example:

  1. # site/config/config.php
  2. return [
  3. 'mrfd.webp.convert.options' => [
  4. 'metadata' => 'all',
  5. 'jpeg' => [
  6. 'converters' => ['cwebp'],
  7. ],
  8. 'png' => [
  9. 'encoding' => 'auto',
  10. 'near-lossless' => 60,
  11. 'quality' => 85,
  12. ],
  13. ],
  14. ];

For all possible options, please read this and this.

Disclaimer

This plugin is provided “as is” with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.

License

Kirby WebP is open-sourced software licensed under the MIT license.

Copyright © 2020 Marijn Roovers

Credits