如何将webP图像格式转换为正常格式?php


How to convert webP image format to normal ? php

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

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

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

虽然很晚,但只是为了它。它只能使用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。

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

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

没有测试,但应该有效。。。