imagecreatefromwebp

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

imagecreatefromwebp由文件或 URL 创建一个新图象。

说明

imagecreatefromwebp(string $filename): GdImage|false

imagecreatefromwebp() 返回图像标识符,代表从指定文件名获得的图像。请注意,无法读取动画 WebP 文件。

小技巧

如已启用fopen 包装器,在此函数中, URL 可作为文件名。关于如何指定文件名详见 fopen()。各种 wapper 的不同功能请参见 支持的协议和封装协议,注意其用法及其可提供的预定义变量。

参数

filename

WebP 图像路径。

返回值

成功后返回图象对象,失败后返回 false

更新日志

版本 说明
8.0.0 成功时,此函数现在返回 GDImage 实例;之前返回 resource

示例

示例 #1 使用 imagecreatefromwebp() 转换 WebP 图像为 jpeg 图像

<?php
// 加载 WebP 文件
$im = imagecreatefromwebp('./example.webp');

// 以 100% 的质量转换成 jpeg 格式
imagejpeg($im, './example.jpeg', 100);
?>

添加备注

用户贡献的备注 1 note

up
1
kawewong at gmail dot com
4 years ago
PHP GD and WebP support:Normal WebP (VP8): supported since PHP 5.4Transparent WebP or alpha transparency (VP8X, VP8L): supported since PHP 7.0Animated WebP (VP8X): not supported at all.You can use the images from here https://developers.google.com/speed/webp/gallery2here https://ezgif.com/help/alternative-animated-image-formatsand here https://developers.google.com/speed/webp/gallery1Test 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/128761Usage:<?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