/    Sign up×
Community /Pin to ProfileBookmark

Can I use the gd library to find out the colors in an image?

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

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@JonaApr 03.2005 — [font=trebuchet ms]You might want to [url=http://us2.php.net/manual/en/function.imagecolorsforindex.php]look again[/url].[/font]
Copy linkTweet thisAlerts:
@ConorauthorApr 03.2005 — 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?
Copy linkTweet thisAlerts:
@JonaApr 03.2005 — [font=trebuchet ms]Look at [url=http://us2.php.net/manual/en/function.imagecolorstotal.php]imagecolorstotal[/url] and the comments therein.[/font]
Copy linkTweet thisAlerts:
@ConorauthorApr 03.2005 — I only saw a couple of comments and I tried this code
[code=php]
<?
$im = imagecreatefromgif('colors.gif');
imagetruecolortopalette($im, false, 256);
$colors=imagecolorstotal($im);
echo $colors;
?>
[/code]

but it did not return anything
Copy linkTweet thisAlerts:
@JonaApr 03.2005 — [font=trebuchet ms]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:[/font]

[code=php]
<?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>';

?>
[/code]
Copy linkTweet thisAlerts:
@ConorauthorApr 03.2005 — thanks, thats grabs all the colors for the image. I should be able to work off that.
Copy linkTweet thisAlerts:
@JonaApr 03.2005 — [font=trebuchet ms]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.[/font]
Copy linkTweet thisAlerts:
@ConorauthorApr 03.2005 — yeah I'll be working with 80 by 80 pixels, so it wont be a problem
Copy linkTweet thisAlerts:
@JonaApr 03.2005 — [font=trebuchet ms]Okay. Just curious, what exactly are you doing this for? In any case, good luck![/font]
Copy linkTweet thisAlerts:
@ConorauthorApr 03.2005 — 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.
Copy linkTweet thisAlerts:
@JonaApr 03.2005 — [font=trebuchet ms]What's the key for what array?[/font]
Copy linkTweet thisAlerts:
@ConorauthorApr 03.2005 — the multidimensional colors array, that will allow me to use this

[code=php]
for($i=0;$i<sizeof($colors);$i++)
$counts[$colors[$i][the_key]]++;

foreach($counts as $key => $value)
echo "$key: $value times<br>n";
[/code]
Copy linkTweet thisAlerts:
@JonaApr 03.2005 — [font=trebuchet ms]There are four keys: red, green, blue, alpha.[/font]
Copy linkTweet thisAlerts:
@ConorauthorApr 03.2005 — thank you
Copy linkTweet thisAlerts:
@ConorauthorApr 03.2005 — [code=php] <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";
}
}
?>[/code]
Copy linkTweet thisAlerts:
@JonaApr 04.2005 — [font=trebuchet ms]Awesome! I'm curious, though. Why did you need this?[/font]
×

Success!

Help @Conor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...