php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #14820 GD: ImageColorLeastUsed
Submitted: 2002-01-03 04:05 UTC Modified: 2003-02-07 19:34 UTC
From: ceo at l-i-e dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.1.1 OS: Any
Private report: No CVE-ID: None
 [2002-01-03 04:05 UTC] ceo at l-i-e dot com
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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-28 05:42 UTC] sniper@php.net
reclassified.
 [2003-02-07 19:34 UTC] iliaa@php.net
To implement such a function would require PHP to go through every single pixel of the image, which would be tremendously slow. There is just no practical way to implement this feature, sorry.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 11:01:37 2024 UTC