View Full Version : Detecting colors in an image

05-11-2008, 12:33 PM
I have already made this short function:

function fetch_palette($img)
$imgDetails = getimagesize($img);

$x = 1;
$y = 1;

while ($y <= intval($imgDetails[1]))
while ($x <= intval($imgDetails[0]))
$color = imagecolorat(imagecreatefromjpeg($img), $x, $y);
if ($colors[$color])
$tmp = $colors[$color];
$tmp = intval($tmp);
$colors[$color] = $tmp;
$colors[$color] = 1;


$x = 1;
return $colors;

but as you can tell, running this on an image thats bigger than 50x50 and it starts really messing with the server, and over 300x300 it will actually timeout due to it taking over 60 seconds to load.

Is there something I can do to make it more efficient, or is there a "get_palette" already existing in PHP?

The overall purpose is that I find the most popular used colours in the image palette.

05-12-2008, 09:53 AM
Are you doing this when someone is loadin a page, or when the image is uploaded?

You would be better to do the latter, if you are not already, and save the palette details to a database and pull these figures when someone views the page.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum