PHP Conference Japan 2024

imagecreatefromwebp

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

imagecreatefromwebp從檔案或 URL 建立新影像

說明

imagecreatefromwebp(字串 $filename): GdImage|false

imagecreatefromwebp() 會傳回一個影像識別碼,表示從給定檔名取得的影像。請注意,無法讀取動畫 WebP 檔案。

提示

如果已啟用 fopen wrappers,則可以使用 URL 作為此函數的檔案名稱。有關如何指定檔案名稱的更多詳細資訊,請參閱 fopen()。有關各種 wrappers 的功能、使用注意事項以及它們可能提供的任何預定義變數的資訊連結,請參閱 支援的協定和 Wrappers

參數

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);
imagedestroy($im);
?>

新增註釋

使用者貢獻的註釋 1 則註釋

11
kawewong at gmail dot com
3 年前
PHP GD 和 WebP 支援

一般 WebP (VP8):從 PHP 5.4 開始支援
透明 WebP 或 Alpha 透明度 (VP8X, VP8L):從 PHP 7.0 開始支援
動畫 WebP (VP8X):完全不支援。

您可以使用這裡的圖片 https://developers.google.com/speed/webp/gallery2
這裡 https://ezgif.com/help/alternative-animated-image-formats
以及這裡 https://developers.google.com/speed/webp/gallery1

使用 imagecreatefromwebp('your-image.webp'); 進行測試並查看錯誤。

您可以使用以下程式碼偵測動畫或透明 WebP。

<?php
/**
* Get WebP file info.
*
* @link https://php.dev.org.tw/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
?>

參考: https://stackoverflow.com/a/68491679/128761

用法

<?php
$info
= webpinfo('your-image.webp');
if (isset(
$info['Animation']) && $info['Animation'] === true) {
echo
'這是動態 WebP 圖檔。';
}
if (isset(
$info['Alpha']) && $info['Alpha'] === true) {
echo
'這是透明 WebP 圖檔。';
}
?>
To Top