Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

imagewebp

Description

The imagewebp of Image for PHP output a WebP image to browser or file.

Syntax

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

Parameters

image

A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor().

file

The path or an open stream resource (which is automatically closed after this function returns) to save the file to. If not set or null, the raw image stream will be output directly.

quality

quality ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file).

Return

Returns true on success or false on failure.

Caution: However, if libgd fails to output the image, this function returns true.

Examples

1 · image

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$return = imagewebp($image);

var_export($return);

?>
RIFFLWEBPVP8 @��*dd>m6�I�$�� ��
�in�s�ck���^X���E�k���^X����`true

2 · file

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$file = $_SERVER['DOCUMENT_ROOT'] . '/assets/webp/1.webp';

$return = imagewebp($image, $file);

var_export($return);

?>
true

3 · quality

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$file = $_SERVER['DOCUMENT_ROOT'] . '/assets/webp/1.webp';
$quality = 100;

$return = imagewebp($image, $file, $quality);

var_export($return);

?>
true

4 · header

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

header("content-type: image/webp");

imagewebp($image);

?>
RIFFLWEBPVP8 @��*dd>m6�I�$�� ��
�in�s�ck���^X���E�k���^X����`

5 · path

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$path = "/assets/webp/1.webp";
$file = $_SERVER['DOCUMENT_ROOT'] . $path;
imagewebp($image, $file);

echo '<!doctype html>
<html>
<body>
    <img src="' . $path . '">
</body>
</html>';

?>
<!doctype html>
<html>
<body>
    <img src="/assets/webp/1.webp">
</body>
</html>

6 · base64

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

ob_start();

    imagewebp($image);

$output = ob_get_clean();

echo '<!doctype html>
<html>
<body>
    <img src="data:image/webp;base64,' . base64_encode($output) . '">
</body>
</html>';

?>
<!doctype html>
<html>
<body>
    <img src="data:image/webp;base64,UklGRkwAAABXRUJQVlA4IEAAAACwBQCdASpkAGQAPm02mUmmJKOiIKgAwA2JaW7hc+AAY2upvcReWAa6m9xF5YBrqb3EXlgGngAA/v+DYAAAAAAA">
</body>
</html>
HomeMenu