david
david

Reputation: 3360

How to convert webP image format to normal ? php

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?

Upvotes: 6

Views: 16998

Answers (2)

Usama Ejaz
Usama Ejaz

Reputation: 1175

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.

Upvotes: 27

n00dl3
n00dl3

Reputation: 21564

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...

Upvotes: 4

Related Questions