php8.5
Home/ Manual/ image / functions/ imagewebp

imagewebp

PHP function Edit on GitHub ✎

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

Output a WebP image to browser or file

Description

imagewebp(GdImage $image, resource|string|null $file = null, int $quality = -1): bool

Outputs or saves a WebP version of the given image.

Parameters

file

Path

quality

quality ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). If -1 is provided, the default value 80 is used.

Return Values

Success

Trueonerror

Errors/Exceptions

Throws a ValueError if quality is invalid.

Changelog

VersionDescription
8.4.0Now throws a ValueError if quality is invalid.

Examples

Saving an WebP file

php
<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);

imagestring($im, 1, 5, 5,  'WebP with PHP', $text_color);

// Save the image
imagewebp($im, 'php.webp');
?>

Source: reference/image/functions/imagewebp.xml · from the official PHP manual (php/doc-en)