5

我有一个 ios 和 android 应用程序。有时用户将 webP 图像上传到我的服务器。问题是 ios 从我的服务器下载时无法显示此图像。

所以我想检查我的 php 代码。如果图像是 webP 格式。然后我会将其转换为 png 格式。

我怎么能用php做到这一点?

4

2 回答 2

18

已经晚了,但只是为了它。它只能使用 PHP 来完成。无需任何外部工具。

引用自 PHP.net文档

<?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);
?>

所以我假设您可以使用imagepng()而不是示例中的 imagejpeg。

于 2017-03-11T15:55:42.870 回答
5

使用libwebp:( 我假设$file是绝对路径并libwebp已安装)

$regex="/^(.*)(\.webp)$/";
if(preg_match($regex, $file)){
   $out=preg_replace($regex, "${1}.png", $file);
   exec("dwebp $file -o $out");
}

没有测试,但应该工作......

于 2015-06-30T08:05:58.330 回答