如何将 webP 图片格式转换为普通格式? php

How to convert webP image format to normal ? php

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

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

我如何使用 php 做到这一点?

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

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

没有测试,但应该可以...

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

引自PHP.net documentation:

<?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。