codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: imagewebp(): Palette image not supported by webp #8197

Closed stevy80 closed 1 year ago

stevy80 commented 1 year ago

PHP Version

8.2

CodeIgniter4 Version

4.4.2

CodeIgniter4 Installation Method

Composer (as dependency to an existing project)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

MySQL 8

What happened?

When converting some .png images to .webp with the using of the image library (gd), the following error occurs: imagewebp(): Palette image not supported by webp

Steps to Reproduce

$image = \Config\Services::image(); 
$image->withfile($filePath); // $filePath to the png file
$image->convert(IMAGETYPE_WEBP);
$image->save();

Expected Output

valid webp file

Anything else?

currently the code in the GDHandler.php (save function):

// for png and webp we can actually preserve transparency
if (in_array($this->image()->imageType, $this->supportTransparency, true)) {
   imagealphablending($this->resource, false);
   imagesavealpha($this->resource, true);
}

if I use imagepalettetotruecolor there, the problem solved for me, like:

// for png and webp we can actually preserve transparency
if (in_array($this->image()->imageType, $this->supportTransparency, true)) {
   imagepalettetotruecolor($this->resource);
   imagealphablending($this->resource, false);
   imagesavealpha($this->resource, true);
}
vini123 commented 1 year ago

虚幻之物对应着冥冥之路!:《羽》

kenjis commented 1 year ago

Cannot reproduce. The following test passes without errors. Does it depend on the source PNG file?

--- a/tests/system/Images/GDHandlerTest.php
+++ b/tests/system/Images/GDHandlerTest.php
@@ -412,6 +412,14 @@ final class GDHandlerTest extends CIUnitTestCase
         $this->assertSame(exif_imagetype($this->start . 'work/ci-logo.png'), IMAGETYPE_PNG);
     }

+    public function testImageConvertPngToWebp(): void
+    {
+        $this->handler->withFile($this->origin . 'ci-logo.png');
+        $this->handler->convert(IMAGETYPE_WEBP);
+        $this->handler->save($this->start . 'work/ci-logo.webp');
+        $this->assertSame(exif_imagetype($this->start . 'work/ci-logo.webp'), IMAGETYPE_WEBP);
+    }
+
     public function testImageReorientLandscape(): void
     {
         for ($i = 0; $i <= 8; $i++) {
stevy80 commented 1 year ago

Yes, it depends from the png. Some works (like ci-good.png), some not (ci-bad.png).

ci-bad ci-good

kenjis commented 1 year ago

@stevy80 Thank you! I sent a PR #8210