PHP 8.1: GD: Lossless WebP encoding support

Version8.1
TypeNew Feature

In PHP 8.1, the GD extension supports lossless encoding, and declares a new PHP constant IMG_WEBP_LOSSLESS which can be passed to imagewebp function to enable lossless encoding of a GdImage object.

The lossless mode requires:

IMG_WEBP_LOSSLESS constant

GD extension declares a new IMG_WEBP_LOSSLESS constant in PHP 8.1 and later if it supports lossless WebP mode.

IMG_WEBP_LOSSLESS constant is assigned 101.

Usage Example

$image = imagecreatefromjpeg('image.jpg');
imagewebp($image, 'image.webp', IMG_WEBP_LOSSLESS);

Backwards Compatibility Impact

The new IMG_WEBP_LOSSLESS is only declared in PHP 8.1 and later, and only if the underlying libraries support lossless mode. This feature cannot be ported to older PHP versions.


Implementation