php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67325 imagetruecolortopalette: white is duplicated in palette
Submitted: 2014-05-22 11:42 UTC Modified: 2016-09-03 14:34 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: icp at freigre dot com Assigned: cmb (profile)
Status: Closed Package: GD related
PHP Version: 5.5.12 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: icp at freigre dot com
New email:
PHP Version: OS:

 

 [2014-05-22 11:42 UTC] icp at freigre dot com
Description:
------------
This bug has been already posted 2004 here:
https://bugs.php.net/bug.php?id=28949

But the bug is still alive in PHP 5.5!

I convert a truecolor image with 1000s of different colors into a palette image using

imagetruecolortopalette ($image, no, 256)

But the palette of the resulting image doesn't contain the 256 most appearing individual colors of the original truecolor image. Many of the palette colors are just white (RGB value: #FF FF FF).
For example just 120 colors are individual, the rest of 136 palette entries is white.
This results in a worse color reproduction of the palette image than it should be using 256 individual colors.

Please fix this bug, since imagetruecolortopalette is very important for creating small-filesized GIF and PNG images. Their filesize is dramatically lower than its truecolor counterpart while retaining satisfying output quality.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-08 07:42 UTC] remi@php.net
I can reproduce, with php 5.4.34 (bundled libgd) and 5.6.3RC1 (system libgd)

Result (with or without dither) have 187 different colors, 70 are white.
 [2014-11-10 14:02 UTC] pajoye@php.net
Given that the default algo works with similar colors to create palettes, it sounds like we may not be able to fix it.



@remi have you tried using the new algo? Afair it is not exposed in php but it provides a way better quality in that regard.
 [2016-09-03 14:34 UTC] cmb@php.net
-Status: Open +Status: Analyzed -Assigned To: +Assigned To: cmb
 [2016-09-03 14:34 UTC] cmb@php.net
I can confirm the issue. The culprit has already been hinted at in
bug #28949, namely the change to cater to `total==0`[1]. Just from
looking at the code, the condition in the following line is
obviously wrong; we don't want to test `count` but rather `total`
to avoid the division by zero. After fixing this, some quick tests
confirm that there are no more duplicate palette colors.

As this issue also affects libgd, I'm going to fix it there first,
and will port the fix to PHP's bundled libgd.

> have you tried using the new algo? Afair it is not exposed in
> php but it provides a way better quality in that regard.

Actually, gdImageTrueColorToPaletteSetMethod() and
gdImageTrueColorToPaletteSetQuality() are not yet available in
PHP's bundled libgd. Seems it doesn't get boring. :-)

[1] <https://github.com/php/php-src/blob/PHP-7.0.11/ext/gd/libgd/gd_topal.c#L762-L774>
    <https://github.com/libgd/libgd/blob/gd-2.2.3/src/gd_topal.c#L661-L670>
 [2016-09-03 14:34 UTC] cmb@php.net
-Summary: imagetruecolortopalette: many colors in palette are missing +Summary: imagetruecolortopalette: white is duplicated in palette
 [2016-09-03 18:21 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=d58224136801ce93b013079557d1de4d068b9b99
Log: Fix #67325: imagetruecolortopalette: white is duplicated in palette
 [2016-09-03 18:21 UTC] cmb@php.net
-Status: Analyzed +Status: Closed
 [2016-10-17 10:08 UTC] bwoebi@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=d58224136801ce93b013079557d1de4d068b9b99
Log: Fix #67325: imagetruecolortopalette: white is duplicated in palette
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC