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
filePath
qualityqualityranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). If-1is provided, the default value80is used.
Return Values
Success
Trueonerror
Errors/Exceptions
Throws a ValueError if quality is invalid.
Changelog
| Version | Description |
|---|---|
| 8.4.0 | Now 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');
?>