edison23

Zápisky šíleného jazykového editora

WebP support in Piwigo

There are plethora of threads about WebP support in Piwigo. The official support is not here yet,though. But there are good people on the Internet who make the support fairly easy to get.

Datum: 2023-08-04
Kategorie: EN Guides
Štítky: piwigo

Obsah


This post is obsolete since the release of Piwigo 14. Piwigo now supports WebP out of the box.

Sadly, though, there's no EXIF support (yet?), so you get similar results as with the approach described below.

Piwigo supports many image formats, but they're mostly "traditional" ones -- ie., those that are around for decades; most notably JPG. But JPG is now more than 30 years old (which wouldn't be a bad thing in itself) and it's suboptimal for photography, to say at least.

However, in Piwigo, the only officially supported image format, that's viable for viewing photos, is JPG. Here are some relevant threads about the topic:

One of these threads mentions a pull request (PR) on GitHub (GH) that's actually helpful.

It's thanks to the good person Phlogi who forked Piwigo and implemented the support for WebP.

It's worth noting, though, that the implementation in the PR linked above and described here doesn't handle EXIF metadata. After you complete the steps here, you can display WebP images in your Piwigo gallery, but you won't see EXIF metadata of WebP images and they won't sort correctly.

Enable support in Piwigo

To enable WebP support in Piwigo, you need to implement this pull request by Phlogi into your local installation of Piwigo.

Prerequisites

  • A vital prerequisite is ImageMagick compiled with WebP support. (Seems to be default in *buntu repos, at least, so it's fine.)
  • Backup original of every file you change.

If you succeed, you'll have image versions (thumbnails and other sizes) generated from WebP source images. These versions will be in WebP. Versions based on images in other formats will continue to work as they did before you touched your Piwigo files.

Should you be doing this? Maybe not…

The caveat of the PR implementation I used and propose here is that you'll have to watch the changed files and merge updates with your changes every time Piwigo updates and overwrites the files.

Another concern are niche systems that don't support WebP, like Android 4.4 KitKat or some Apple systems and browsers. However, as for Apple, I tested this on iPhone 14 (Safari and Firefox) and everything was fine there.

Implement the PR into your Piwigo

I went the hard and cumbersome way -- change the files manually. You can pull the whole repo with the fork containing the PR, but bare in mind the Phlogi's fork is currently many commits behind the master branch.

Here are diffs of 5 files you need to change. Implement those and you should be good to go.

The paths to the files are relative to the root of your Piwigo installation.

The diffs contain 3-line context. (diff -C 3 fileA fileB)


./action.php
@@ -21,6 +21,7 @@ function guess_mime_type($ext)
      case "jpg": $ctype="image/jpeg"; break;
      case "png": $ctype="image/png"; break;
      case "gif": $ctype="image/gif"; break;
+     case "webp": $ctype="image/webp"; break;
      case "tiff":
      case "tif": $ctype="image/tiff"; break;
      case "txt": $ctype="text/plain"; break;

./i.php
@@ -357,6 +357,7 @@ function send_derivative($expires)
      case ".jpe": case ".jpeg": case ".jpg": $ctype="image/jpeg"; break;
      case ".png": $ctype="image/png"; break;
      case ".gif": $ctype="image/gif"; break;
+     case ".webp": $ctype="image/webp"; break;
    }
    header("Content-Type: $ctype");

./admin/include/image.class.php
@@ -47,6 +47,7 @@ class pwg_image
    function __construct($source_filepath, $library=null)
    {
+     global $conf;
      $this->source_filepath = $source_filepath;

      trigger_notify('load_image_library', array(&$this) );
@@ -58,7 +59,7 @@ function __construct($source_filepath, $library=null)
      $extension = strtolower(get_extension($source_filepath));

-     if (!in_array($extension, array('jpg', 'jpeg', 'png', 'gif')))
+     if (!in_array($extension, $conf['picture_ext']))
      {
        die('[Image] unsupported file extension');
      }

./admin/include/functions_upload.inc.php
@@ -221,6 +221,10 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie
      {
        $file_path.= 'jpg';
      }
+     elseif (IMAGETYPE_WEBP == $type)
+     {
+       $file_path.= 'webp';
+     }
      elseif (isset($conf['upload_form_all_types']) and $conf['upload_form_all_types'])
      {
        $original_extension = strtolower(get_extension($original_filename));

./include/config_default.inc.php
@@ -40,7 +40,7 @@
  // picture_ext : file extensions for picture file, must be a subset of
  // file_ext
- $conf['picture_ext'] = array('jpg','jpeg','png','gif');
+ $conf['picture_ext'] = array('jpg','jpeg','png','gif','webp');

  // file_ext : file extensions (case sensitive) authorized
  $conf['file_ext'] = array_merge(