7

I am save the png image webp format using following code but sometime show the

Fatal error: Paletter image not supported by webp

so, please give me suggestions

$im = imagecreatetruecolor(120, 20); 

$text_color = imagecolorallocate($im, 128, 128, 128);

$img = imagecreatefrompng('l2.png'); 

imagestring($img,5, 5,5,'abc',$text_color);

// Save the image 

imagewebp($img, 'images/l2.webp'); 

// Free up memory 

imagedestroy($img); 
urfusion
  • 5,528
  • 5
  • 50
  • 87
user3406206
  • 71
  • 1
  • 3
  • you might want to look into converting the color from RGB. Check out this link: https://stackoverflow.com/questions/25248382/how-to-create-a-webp-image-in-php – Syfer Aug 04 '17 at 00:01

4 Answers4

18

Before creating an image in .webp format, convert your file to RGB

$img = imagecreatefrompng('l2.png'); 
imagepalettetotruecolor($img);

Also in your case, you can save the alpha channel of the png-file

$img = imagecreatefrompng('l2.png'); 
imagepalettetotruecolor($img);
imagealphablending($img, true);
imagesavealpha($img, true);
b.tokman
  • 284
  • 3
  • 5
4

I got a similar issue and I have fixed it by adding the following code.

imagepalettetotruecolor($this->_imageHandler);

$this->_imageHandler is 'gd' as I was using GD2 image library of PHP.

Function imagepalettetotruecolor converts a palette based image to true color and returns TRUE if the convertion was complete, or if the source image already is a true color image, otherwise FALSE is returned.

My new code looked something like below:

imagepalettetotruecolor($this->_imageHandler);
$res = imagewebp($this->_imageHandler, $fileName);
Anshu Mishra
  • 361
  • 1
  • 7
  • 18
1

You are saving the wrong image. It should be $im instead of $img.

-1

May be caused by an abnormal picture, damaged or edited or directly modify the extension. You can use PS to open the picture to save again, it will not be an error. Facts have proved that it is due to being illegally edited

Dennis
  • 1
  • 1