Conor
04-03-2005, 08:27 AM
I was looking through the GD functions but have not found anything. Any help is appreciated, I do not really mind if the colors returned are in rgb or hex
|
Click to See Complete Forum and Search --> : Can I use the gd library to find out the colors in an image? Conor 04-03-2005, 08:27 AM I was looking through the GD functions but have not found anything. Any help is appreciated, I do not really mind if the colors returned are in rgb or hex Jona 04-03-2005, 08:28 AM You might want to look again (http://us2.php.net/manual/en/function.imagecolorsforindex.php). Conor 04-03-2005, 09:57 AM wow can't believe I missed that. Now I just need to write a function that goes through each color and finds two most prominent. It might be tough though for something like an 80 by 80 image thats 6400 pixels of color and I'm almost positive that would time out. Any ideas? Jona 04-03-2005, 10:10 AM Look at imagecolorstotal (http://us2.php.net/manual/en/function.imagecolorstotal.php) and the comments therein. Conor 04-03-2005, 10:54 AM I only saw a couple of comments and I tried this code <? $im = imagecreatefromgif('colors.gif'); imagetruecolortopalette($im, false, 256); $colors=imagecolorstotal($im); echo $colors; ?> but it did not return anything Jona 04-03-2005, 11:28 AM That worked for me, but it only gives the total number of colors. It turns out you'll have to loop through the image. This might be a good start: <?php $im = ImageCreateFromJPEG("image.jpg"); $width = imagesx($im); $height = imagesy($im); $colors = array(); for ($cy=0;$cy<$height;$cy++) { for ($cx=0;$cx<$width;$cx++) { $rgb = ImageColorAt($im, $cx, $cy); $col = imagecolorsforindex($im, $rgb); $colors[] = $col; } } echo '<pre>', print_r($colors), '</pre>'; ?> Conor 04-03-2005, 11:49 AM thanks, thats grabs all the colors for the image. I should be able to work off that. Jona 04-03-2005, 11:51 AM Yeah. I was looking for some sort of built-in array sorting function to do it, but it looks like you'll have to do some math in the second for loop to get what you're after. On a large image, this can take up a ton of processing power and might even freeze up your server for a few minutes, so be careful. I wouldn't advise anything over 400x400 pixels. Conor 04-03-2005, 12:15 PM yeah I'll be working with 80 by 80 pixels, so it wont be a problem Jona 04-03-2005, 12:16 PM Okay. Just curious, what exactly are you doing this for? In any case, good luck! Conor 04-03-2005, 01:52 PM sorry to bother you, I just have one last question. What is the key for the array. Its neccasary for me to know that so I can finish up my script. Jona 04-03-2005, 01:53 PM What's the key for what array? Conor 04-03-2005, 01:59 PM the multidimensional colors array, that will allow me to use this for($i=0;$i<sizeof($colors);$i++) $counts[$colors[$i][the_key]]++; foreach($counts as $key => $value) echo "$key: $value times<br>\n"; Jona 04-03-2005, 02:11 PM There are four keys: red, green, blue, alpha. Conor 04-03-2005, 02:13 PM thank you Conor 04-03-2005, 04:45 PM <form action="<?$_SERVER['PHP_SELF'];?>" method="post"> <input type="text" name="url" value="GIF URL" /> <input type="submit" name="submit" value="Get Colors"> <?php if(isset($_POST['submit'])) { $size= getimagesize("$url"); $totpix=$size[0] * $size[1]; $im = ImageCreateFromGif("$url"); $width = imagesx($im); $height = imagesy($im); $colors = array(); for ($cy=0;$cy<$height;$cy++) { for ($cx=0;$cx<$width;$cx++) { $rgb = ImageColorAt($im, $cx, $cy); $col = imagecolorsforindex($im, $rgb); $colors[] = $col; } } echo"<br />Your image is ".$size[0]." by ".$size[1]." and contains ".$totpix." pixels"; echo"<br /><br />"; for($i=0;$i<sizeof($colors);$i++) $counts[$colors[$i][red]."_".$colors[$i][green]."_".$colors[$i][blue]."_".$colors[$i][alpha]]++; $color_count=0; foreach($counts as $key => $value) { $temp_arr=explode("_",$key); $color_colors[$color_count][red]=$temp_arr[0]; $color_colors[$color_count][green]=$temp_arr[1]; $color_colors[$color_count][blue]=$temp_arr[2]; $color_colors[$color_count][alpha]=$temp_arr[3]; $color_counts[$color_count]=$value; $color_count++; } array_multisort($color_counts,SORT_DESC,$color_colors); for($i=0;$i<$color_count;$i++) { $j=$i+1; $r=$color_colors[$i][red]; $g=$color_colors[$i][green]; $b=$color_colors[$i][blue]; $hexcolor=sprintf("#%x%x%x", $r, $g, $b); echo "$j:".$hexcolor."-".$color_counts[$i]." times<br>\n"; } } ?> Jona 04-03-2005, 09:24 PM Awesome! I'm curious, though. Why did you need this? webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |