Skip to content

Convert jpeg/png to webp with PHP (if at all possible)

License

Notifications You must be signed in to change notification settings

henrikalves/webp-convert

 
 

Repository files navigation

WebP Convert

Latest Stable Version Minimum PHP Version Build Status Coverage Status Quality Score Software License

Convert JPEG & PNG to WebP with PHP

This library enables you to do webp conversion with PHP. It supports an abundance of methods for converting and automatically selects the most capable of these that is available on the system.

The library can convert using the following methods:

In addition to converting, the library also has a method for serving converted images, and we have instructions here on how to set up a solution for automatically serving webp images to browsers that supports webp.

News: 2.6.0 has just been released

  • Introduced auto-limit option which replaces setting "quality" to "auto" #281
  • Added "sharp-yuv" option and made it default on. Its great, use it! Works in most converters (works in cwebp, vips, imagemagick, graphicsmagick, imagick and gmagick) #267, #280, #284
  • Bumped cwebp binaries to 1.2.0 #273
  • vips now supports "method" option and "preset" option.
  • graphicsmagick now supports "auto-filter" potion
  • vips, imagick, imagemagick, graphicsmagick and gmagick now supports "preset" option [#275(rosell-dk#275)
  • cwebp now only validates hash of supplied precompiled binaries when necessary. This cuts down conversion time. #287
  • Added new option to cwebp for skipping precompiled binaries that are known not to work on current system. This will cut down on conversion time. #288
  • And more (view closed issues here)

Installation

Require the library with Composer, like this:

composer require rosell-dk/webp-convert

Converting images

Here is a minimal example of converting using the WebPConvert::convert method:

// Initialise your autoloader (this example is using Composer)
require 'vendor/autoload.php';

use WebPConvert\WebPConvert;

$source = __DIR__ . '/logo.jpg';
$destination = $source . '.webp';
$options = [];
WebPConvert::convert($source, $destination, $options);

The WebPConvert::convert method comes with a bunch of options. The following introduction is a must-read: docs/v2.0/converting/introduction-for-converting.md.

If you are migrating from 1.3.9, read this

Serving converted images

The WebPConvert::serveConverted method tries to serve a converted image. If there already is an image at the destination, it will take that, unless the original is newer or smaller. If the method cannot serve a converted image, it will serve original image, a 404, or whatever the 'fail' option is set to. It also adds X-WebP-Convert-Log headers, which provides insight into what happened.

Example (version 2.0):

require 'vendor/autoload.php';
use WebPConvert\WebPConvert;

$source = __DIR__ . '/logo.jpg';
$destination = $source . '.webp';

WebPConvert::serveConverted($source, $destination, [
    'fail' => 'original',     // If failure, serve the original image (source). Other options include 'throw', '404' and 'report'
    //'show-report' => true,  // Generates a report instead of serving an image

    'serve-image' => [
        'headers' => [
            'cache-control' => true,
            'vary-accept' => true,
            // other headers can be toggled...
        ],
        'cache-control-header' => 'max-age=2',
    ],

    'convert' => [
        // all convert option can be entered here (ie "quality")
    ],
]);

The following introduction is a must-read (for 2.0): docs/v2.0/serving/introduction-for-serving.md.

The old introduction (for 1.3.9) is available here: docs/v1.3/serving/convert-and-serve.md

WebP on demand

The library can be used to create a WebP On Demand solution, which automatically serves WebP images instead of jpeg/pngs for browsers that supports WebP. To set this up, follow what's described in this tutorial (not updated for 2.0 yet).

Projects using WebP Convert

CMS plugins using WebP Convert

This library is used as the engine to provide webp conversions to a handful of platforms. Hopefully this list will be growing over time. Currently there are plugins / extensions / modules / whatever the term is for the following CMS'es (ordered by market share):

Other projects using WebP Convert

Supporting WebP Convert

Bread on the table don't come for free, even though this library does, and always will. I enjoy developing this, and supporting you guys, but I kind of need the bread too. Please make it possible for me to have both:

Supporters

Persons currently backing the project via patreon - Thanks!

Persons who contributed with coffee within the last 30 days:

  • Anon
  • Eder Ribeiro
  • Christian

Persons who contributed with extra generously amounts of coffee / lifetime backing (>50$) - thanks!:

  • Justin - BigScoots ($105)
  • Sebastian ($99)
  • Tammy Lee ($90)
  • Max Kreminsky ($65)
  • Steven Sullivan ($51)

About

Convert jpeg/png to webp with PHP (if at all possible)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 98.9%
  • Shell 1.1%