Page 1 of 1

PHP: PNG Image loading color issue

Posted: Tue Jan 14, 2003 4:06 am
by dbcooper
Hello all,

I'm experiencing an issue with the way the GD/libpng and/or PHP is loading an existing PNG graphic and apparently messing up the color index (or something along those lines) either upon loading or somewhere before the final output.

The PHP platform is Redhat 7.2 i386 w/ the following package versions (installed as RPMs):
  • Apache 1.3.27
  • PHP 4.1.2
  • GD 1.8.4
  • libpng 1.0.14
I'm currently investigating it, but I wanted to see if anyone had already figured it out. Is this an old issue and has been resolved in subsequent versions? Or is this something that hasn't been worked out yet?

The comments in the on-line PHP Manual acknowledge the problem, but don't seem to offer a solution short of modifying the input files themselves. In my situation, let's say that I don't have that option (i.e., I must deal with the files as they are submitted--I have no way of massaging the input).

Here are 2 screenshots of what I'm talking about. The first one is the actual image loaded up in Mozilla:

Image

Now, here is the same image when I load the PNG from a file via imagecreatefrompng() and print it out again:

Image

I got the same results in IE as well.

Here is the source of the image_test.php script where I load the image and print it out:

Code: Select all

<?php

$im = imagecreatefrompng("green-button.png");

Header("Content-type: image/png");
imagepng($im);

?>
Anyone know if this issue has been resolved already?

Thanks,

dbcooper

Posted: Tue Jan 14, 2003 4:14 am
by evilcoder
solution. add it manually.

PHP uses 256 colour index (webindex). Best off just adding it <img> wise.

sorry i couldnt help more.

Posted: Tue Jan 14, 2003 5:10 am
by dbcooper
evilcoder wrote:PHP uses 256 colour index (webindex).
Does it use a 256 color index for all image objects? Or just PNGs?

I'm inferring by what you stated, that it is indeed a feature, and not a bug.

This comment in the imagecreatefromjpeg() function listing makes me wonder if its an old version of GD which is causing this?

The reason I can't go with the simple solution is that the processing of images is going to be automatic, and the images may not be pre-processed by someone who knows what they're doing. If PHP and/or the GD library can't handle images w/ more than 256 colors or the extension's support is lacking then I may have to go w/ Perl and ImageMagick instead. I would prefer to know this before I invest too much time in PHP code.

Thanks,

db

Posted: Tue Jan 14, 2003 5:56 am
by volka
take a look at http://www.php.net/manual/en/function.i ... ecolor.php
maybe that's sufficient for your needs
nevertheless ImageMagick is a good choice for either languages

Posted: Wed Jan 15, 2003 12:55 am
by dbcooper
volka wrote:take a look at http://www.php.net/manual/en/function.i ... ecolor.php
maybe that's sufficient for your needs
Actually, I'm interested in being able to load existing images that may have more than 256 colors and manipulate them. If an image is automatically mapped to a 256 color palette whenever its loaded, then I'm losing information without the option of preserving it.

I ran another simple test with a JPEG and it appears that the same thing is occuring when I use imagecreatefromjpeg() and imagejpeg(). From various comments spread about the PHP Manual Image function section, it appears that the GD library (at least pre 2.0) doesn't allocate more than 256 colors for a loaded image object:
There are more, but I don't feel like hunting for them right now.

Perhaps compiling/installing the GD library post 2.0 will solve the issue. Anyone know?

-db