4

I am trying to convert a webp file to JPEG using imagecreatefromwebp() but unfortunately, it throws me a warning: Warning: imagecreatefromwebp(): WebP decode: fail to decode input data.

Here's my code

$filename = dirname(__FILE__)."\\".$keyword."1.webp"; // $keyword = 'xyz';

$im = imagecreatefromwebp($filename);

// Convert it to a jpeg file with 100% quality
imagejpeg($im, './example.jpeg', 100);
imagedestroy($im);

Please help.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Bishnu Dev
  • 117
  • 1
  • 12

1 Answers1

0

i am using this code, it works fine for me. Here $data contains the base64encoded data

$im = imagecreatefromwebp($data);
$imageResult = imagejpeg($im, $destinationPath . $fileName, 100);
imagedestroy($im);

The imagecreatefromwebp() function accepts either a valid file or URL. You can also pass the your binary data in that function. You can check the function definition and example here http://php.net/manual/en/function.imagecreatefromwebp.php

RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
  • RAUSHAN KUMAR you're right, this is the actual solution: `$data = file_get_contents ($filename); $image = @imagecreatefromwebp ('data://image/webp;base64,' . base64_encode ($data)); ` this works , whereas just supplying the filename directly fails. (also [thanks](https://hotexamples.com/examples/-/-/imagecreatefromwebp/php-imagecreatefromwebp-function-examples.html) ) – gregn3 Jul 22 '19 at 18:31