PHP 8.5.0 Alpha 1 available for testing

imagecreatefromwebp

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

imagecreatefromwebpCrea una nueva imagen a partir de un fichero o de una URL

Descripción

imagecreatefromwebp(string $filename): GdImage|false

imagecreatefromwebp() devuelve un identificador de imagen que representa la imagen obtenida desde el fichero proporcionado. Tenga en cuenta que los ficheros WebP animados no pueden ser leídos.

Sugerencia

Se puede emplear un URL como nombre de fichero con esta función si las envolturas de fopen han sido activadas. Véase fopen() para más información de cómo especificar el nombre de fichero. Véanse las Protocolos y Envolturas soportados; continen enlaces con información sobre las diferentes capacidades que tienen las envolturas, notas sobre su empleo, e información de cualquier variable predefinida que podría proporcionarse.

Parámetros

filename

Ruta hacia la imagen WebP.

Valores devueltos

En caso de éxito, devuelve un identificador de recurso de imagen, y false en caso de error.

Historial de cambios

Versión Descripción
8.0.0 En caso de éxito, esta función devuelve ahora una instancia de GDImage ; anteriormente, se devolvía un resource.

Ejemplos

Ejemplo #1 Convierte una imagen WebP en una imagen jpeg utilizando la función imagecreatefromwebp()

<?php
// Carga el fichero WebP
$im = imagecreatefromwebp('./example.webp');

// Se convierte en un fichero jpeg con una calidad del 100%
imagejpeg($im, './example.jpeg', 100);
?>

add a note

User Contributed Notes 1 note

up
14
kawewong at gmail dot com
3 years ago
PHP GD and WebP support:

Normal WebP (VP8): supported since PHP 5.4
Transparent WebP or alpha transparency (VP8X, VP8L): supported since PHP 7.0
Animated WebP (VP8X): not supported at all.

You can use the images from here https://developers.google.com/speed/webp/gallery2
here https://ezgif.com/help/alternative-animated-image-formats
and here https://developers.google.com/speed/webp/gallery1

Test with imagecreatefromwebp('your-image.webp'); and see the errors.

You can detect animated or transparent webp using this code.

<?php
/**
* Get WebP file info.
*
* @link https://www.php.net/manual/en/function.pack.php unpack format reference.
* @link https://developers.google.com/speed/webp/docs/riff_container WebP document.
* @param string $file
* @return array|false Return associative array if success, return `false` for otherwise.
*/
function webpinfo($file) {
if (!
is_file($file)) {
return
false;
} else {
$file = realpath($file);
}

$fp = fopen($file, 'rb');
if (!
$fp) {
return
false;
}

$data = fread($fp, 90);

fclose($fp);
unset(
$fp);

$header_format = 'A4Riff/' . // get n string
'I1Filesize/' . // get integer (file size but not actual size)
'A4Webp/' . // get n string
'A4Vp/' . // get n string
'A74Chunk';
$header = unpack($header_format, $data);
unset(
$data, $header_format);

if (!isset(
$header['Riff']) || strtoupper($header['Riff']) !== 'RIFF') {
return
false;
}
if (!isset(
$header['Webp']) || strtoupper($header['Webp']) !== 'WEBP') {
return
false;
}
if (!isset(
$header['Vp']) || strpos(strtoupper($header['Vp']), 'VP8') === false) {
return
false;
}

if (
strpos(strtoupper($header['Chunk']), 'ANIM') !== false ||
strpos(strtoupper($header['Chunk']), 'ANMF') !== false
) {
$header['Animation'] = true;
} else {
$header['Animation'] = false;
}

if (
strpos(strtoupper($header['Chunk']), 'ALPH') !== false) {
$header['Alpha'] = true;
} else {
if (
strpos(strtoupper($header['Vp']), 'VP8L') !== false) {
// if it is VP8L, I assume that this image will be transparency
// as described in https://developers.google.com/speed/webp/docs/riff_container#simple_file_format_lossless
$header['Alpha'] = true;
} else {
$header['Alpha'] = false;
}
}

unset(
$header['Chunk']);
return
$header;
}
// webpinfo
?>

Reference: https://stackoverflow.com/a/68491679/128761

Usage:

<?php
$info
= webpinfo('your-image.webp');
if (isset(
$info['Animation']) && $info['Animation'] === true) {
echo
'It is animated webp.';
}
if (isset(
$info['Alpha']) && $info['Alpha'] === true) {
echo
'It is transparent webp.';
}
?>
To Top