我在使用 imagewebp 将图像转换为 webp 时遇到问题。
我使用这段代码:
$filename = dirname(__FILE__) .'/example.jpg';
$im = imagecreatefromjpeg($filename);
$webp =imagewebp($im, str_replace('jpg', 'webp', $filename));
imagedestroy($im);
var_dump($webp);
$webp 返回 true,但是当我尝试在 Chrome 中查看 webp-image 时,它只显示空白,但大小正确。如果我改为加载图像并使用 PHP 设置标题(见下文),它会显示出来,但颜色错误(黄色太多)。
$im = imagecreatefromwebp('example.webp');
header('Content-Type: image/webp');
imagewebp($im);
imagedestroy($im);
如果我使用命令行转换相同的图像,它会按预期工作。
cwebp -q 100 example.jpg -o example.webp
我正在 Ubuntu 14、Apache 2.4.7 和 PHP 5.5.9-1ubuntu4.4 上对此进行测试。