From: [EMAIL PROTECTED] Operating system: Any PHP version: 4.1.1 PHP Bug Type: GD related Bug description: Feature Request: ImageColorLeastUsed
ImageColorResolve et al are really cool, but SOMETIMES you *need* the color you want, and they just won't cut it. So you want to deallocate the least-used color, and then allocate the one you really want. Alas, the only way to do that (as far as I can figger) is this: $colorcount = array(); for ($x = 0; $x < $width; $x++){ for ($y = 0; $y < $height; $y++){ $colorindex = imagecolorat($jpg, $x, $y); if (!isset($colorcount[$colorindex])){ $colorcount[$colorindex] = 1; } else{ $colorcount[$colorindex]++; } } } asort($colorcount); reset($colorcount); $black = imagecolorexact($jpg, 0, 0, 0); if ($black == -1){ $goner = key($colorcount); $rgb = imagecolorsforindex($jpg, $goner); #error_log("Need black: About to kill $goner ($rgb[red], $rgb[green], $rgb[blue]) which was only used in $colorcount[$goner] pixels", 0); unset($colorcount[$goner]); imagecolordeallocate($jpg, $goner); $black = imagecolorallocate($jpg, 0, 0, 0); } if ($black == -1){ $black = imagecolorresolve($jpg, 0, 0, 0); #error_log("Damn! STILL couldn't allocate the color!", 0); } Ugly, ain't it :-) Anyway, if GD has any internal structures of a histogram nature, that one could use for a function matching this documentation: int ImageColorLeastUsed(int img) Returns a color index of the least-used color in the image. Suitable for ImageDeallocate() when you really need a color that is not in the image. Also handy would be something like: array ImageColorMostContrasty(int img) Returns an a array of 'red', 'green', 'blue' at a maxium distance from all other colors currently in the image. I have no clue which color-distance algorithm I would actually want here. But it would be nice for adding overlays to existing images that come from a random source. If you want some idea of what I'm talking about: http://chatmusic.com/maplocater.htm The latitude/longitude lines and the red circles are being pulled from a database and overlaid in real-time. However, somebody might upload a map with lots of red and black in it, and then I've got un-usable images. -- Edit bug report at: http://bugs.php.net/?id=14820&edit=1 -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]