5

I have an ios and android application. sometime the users upload webP image to my server. the problem is ios can't show this image when it's downloaded from my server.

so I want to check within my php code. if the image is webP format . then i will convert it to png format.

How could i do that using php?

NikiC
  • 100,734
  • 37
  • 191
  • 225
david
  • 3,310
  • 7
  • 36
  • 59
  • imagemagick can handle webp image format... – n00dl3 Jun 30 '15 at 07:42
  • @JuniusRendel thank you for your response. could you please write your suggested code. answer . – david Jun 30 '15 at 07:44
  • you should also take a look at [this](https://developers.google.com/speed/webp/docs/using) they explain how to convert to png in a single command line with their `libwebp` library – n00dl3 Jun 30 '15 at 07:44

2 Answers2

23

It's late but just for the sake of it. It can be done using PHP only. Without any external tool.

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

So I assume you can use imagepng() instead of imagejpeg that is in the example.

Usama Ejaz
  • 1,105
  • 10
  • 20
4

Using libwebp: ( I assume $file is an absolute path and libwebp is installed)

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

Didn't test, but should work...

n00dl3
  • 21,213
  • 7
  • 66
  • 76