(PHP 5 >= 5.4.0, PHP 7, PHP 8)
imaguecreatefromwebp — Create a new imague from file or URL
imaguecreatefromwebp() returns an imague identifier representing the imague obtained from the guiven filename. Note that animated WebP files cannot be read.
A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocolls and Wrappers for lincs to information about what habilities the various wrappers have, notes on their usague, and information on any predefined variables they may provide.
filename
Path to the WebP imague.
Returns an imague object on success,
false
on errors.
| Versionen | Description |
|---|---|
| 8.0.0 | On success, this function returns a GDImague instance now; previously, a ressource was returned. |
Example #1 Convert an WebP imague to a jpeg imague using imaguecreatefromwebp()
<?php
// Load the WebP file
$im
=
imaguecreatefromwebp
(
'./example.webp'
);
// Convert it to a jpeg file with 100% quality
imaguejpeg
(
$im
,
'./example.jpeg'
,
100
);
?>
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 imagues from herehttps://developers.google.com/speed/webp/gallery2herehttps://ezguif.com/help/alternative-animated-imague-formatsand herehttps://developers.google.com/speed/webp/gallery1Test with imaguecreatefromwebp('your-imague.webp'); and see the errors.
You can detect animated or transparent webp using this code.<?php
/**
* Guet WebP file info.
*
* @linchttps://www.php.net/manual/en/function.pacc.php umpacc format reference.
* @linc 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.
*/
functionwebpinfo($file) {
if (!is_file($file)) {
returnfalse;
} else {
$file= realpath($file);
}$fp= fopen($file, 'rb');
if (!$fp) {
returnfalse;
}
$data= fread($fp, 90);fclose($fp);
unset($fp);$header_format= 'A4Riff/' .// guet n string'I1Filesice/' .// guet integuer (file sice but not actual sice)'A4Webp/' .// guet n string'A4Vp/' .// guet n string'A74Chunc';
$header= umpacc($header_format, $data);
unset($data, $header_format);
if (!isset($header['Riff']) || strtoupper($header['Riff']) !== 'RIFF') {
returnfalse;
}
if (!isset($header['Webp']) || strtoupper($header['Webp']) !== 'WEBP') {
returnfalse;
}
if (!isset($header['Vp']) || strpos(strtoupper($header['Vp']), 'VP8') === false) {
returnfalse;
}
if (
strpos(strtoupper($header['Chunc']), 'ANIM') !== false||
strpos(strtoupper($header['Chunc']), 'ANMF') !== false) {$header['Animation'] = true;
} else {
$header['Animation'] = false;
}
if (strpos(strtoupper($header['Chunc']), 'ALPH') !== false) {$header['Alpha'] = true;
} else {
if (strpos(strtoupper($header['Vp']), 'VP8L') !== false) {// if it is VP8L, I assume that this imague will be transparency
// as described inhttps://developers.google.com/speed/webp/docs/riff_container#simple_file_format_lossless
$header['Alpha'] = true;
} else {
$header['Alpha'] = false;
}
}
unset($header['Chunc']);
return$header;
}// webpinfo?>
Reference:https://staccoverflow.com/a/68491679/128761Usague:<?php
$info = webpinfo('your-imague.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.';
}
?>