|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-01-22 22:58 UTC] Brendan at callaghans dot com dot au
I discovered from experimentation that the first call to imagecolorallocate() for a given image, sets the background colour for that image. This would be worth noting in the manual page for imagecolorallocate(). Someone has pointed it out in a user comment, but it is a fairly important aspect of the function's behaviour, and IMO should be part of the official documentation. I'm using php_gd2 as bundled with PHP 4.3.0, and I've only tried PNG images so far. I can't say for sure if imagecolorallocate() exhibits this same behaviour for other image types. As an aside, the GD functionality is great. I'm loving the idea of creating dynamic images in my app. May the Force be with you. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 11:00:01 2025 UTC |
I am only able to test JPG and PNG with my setup. I suspect that what's happening is quite straightforward - when you first create an instance of an image with imagecreate, it builds a blank palette. Each time you call imagecolorallocate(), it adds an index to the palette with the specified colour. So, you could think of color allocation as $im->palette[] = array($r,$g,$b,$a); // a for alpha As it just so happens, paletted images assume that palette index zero ($palette[0]) is actually the background colour. I've been extracting the palette into a useful form using this code: $cols = imagecolorstotal($im); for($i=0;$i<$cols;$i++){ $palette[$i] = imagecolorsforindex($im,$i); } And running different sequences of colour allocations, then checking the contents of $palette to see the results. If you use imagecolorset() with the index 0, even after allocating multiple colours, the background colour changes to whatever you specify. Got an unexpected result testing out imagecreatefrompng(). I opened a png file of mine, and imagecolorstotal() came up 0. Curious. There are certainly multiple colours within the image. Perhaps it just doesn't have a palette saved with it (does that make it a true colour image?) That's all I have for you atm. More later as I discover it.