imagecreatefromwebp
(PHP 5 >= 5.5.0)
imagecreatefromwebp — Create a new image from file or URL
Description
$filename
)imagecreatefromwebp() returns an image identifier representing the image obtained from the given filename.
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 Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.
Parameters
-
filename
-
Path to the WebP image.
Return Values
Returns an image resource identifier on success, FALSE
on errors.
Examples
Example #1 Convert an WebP image to a jpeg image using imagecreatefromwebp()
<?php
// Load the WebP file
$im = imagecreatefromwebp('./example.webp');
// Convert it to a jpeg file with 100% quality
imagejpeg($im, './example.jpeg', 100);
imagedestroy($im);
?>
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Обработка и генерация изображений
- Обработка изображений и GD
- gd_info
- getimagesize
- getimagesizefromstring
- image_type_to_extension
- image_type_to_mime_type
- image2wbmp
- imageaffine
- imageaffinematrixconcat
- imageaffinematrixget
- imagealphablending
- imageantialias
- imagearc
- imagechar
- imagecharup
- imagecolorallocate
- imagecolorallocatealpha
- imagecolorat
- imagecolorclosest
- imagecolorclosestalpha
- imagecolorclosesthwb
- imagecolordeallocate
- imagecolorexact
- imagecolorexactalpha
- imagecolormatch
- imagecolorresolve
- imagecolorresolvealpha
- imagecolorset
- imagecolorsforindex
- imagecolorstotal
- imagecolortransparent
- imageconvolution
- imagecopy
- imagecopymerge
- imagecopymergegray
- imagecopyresampled
- imagecopyresized
- imagecreate
- imagecreatefromgd2
- imagecreatefromgd2part
- imagecreatefromgd
- imagecreatefromgif
- imagecreatefromjpeg
- imagecreatefrompng
- imagecreatefromstring
- imagecreatefromwbmp
- imagecreatefromwebp
- imagecreatefromxbm
- imagecreatefromxpm
- imagecreatetruecolor
- imagecrop
- imagecropauto
- imagedashedline
- imagedestroy
- imageellipse
- imagefill
- imagefilledarc
- imagefilledellipse
- imagefilledpolygon
- imagefilledrectangle
- imagefilltoborder
- imagefilter
- imageflip
- imagefontheight
- imagefontwidth
- imageftbbox
- imagefttext
- imagegammacorrect
- imagegd2
- imagegd
- imagegif
- imagegrabscreen
- imagegrabwindow
- imageinterlace
- imageistruecolor
- imagejpeg
- imagelayereffect
- imageline
- imageloadfont
- imagepalettecopy
- imagepalettetotruecolor
- imagepng
- imagepolygon
- imagepsbbox
- imagepsencodefont
- imagepsextendfont
- imagepsfreefont
- imagepsloadfont
- imagepsslantfont
- imagepstext
- imagerectangle
- imagerotate
- imagesavealpha
- imagescale
- imagesetbrush
- imagesetinterpolation
- imagesetpixel
- imagesetstyle
- imagesetthickness
- imagesettile
- imagestring
- imagestringup
- imagesx
- imagesy
- imagetruecolortopalette
- imagettfbbox
- imagettftext
- imagetypes
- imagewbmp
- imagewebp
- imagexbm
- iptcembed
- iptcparse
- jpeg2wbmp
- png2wbmp
Коментарии
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.';
}
?>