|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2002-06-28 05:42 UTC] sniper@php.net
[2003-02-07 19:34 UTC] iliaa@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 19:00:01 2025 UTC |
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.