Retrieve pixel color from image

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
robertboyle
Forum Newbie
Posts: 2
Joined: Sun May 07, 2006 6:18 am

Retrieve pixel color from image

Post by robertboyle »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, 

Imagine I have a photo/image and I want to know what color is attributed to a certain pixel. 
Is it possible to do that with PHP?

I found this in the PHP manual:

Code: Select all

<?php 
$im = ImageCreateFromPng("rockym.png"); 
$rgb = ImageColorAt($im, 100, 100); 
$r = ($rgb >> 16) & 0xFF; 
$g = ($rgb >>  & 0xFF; 
$b = $rgb & 0xFF; 
?>
but apparently, it gives me three different colors. Plus, would it still work if $im points to an image file?

Thanks.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: Retrieve pixel color from image

Post by aerodromoi »

robertboyle wrote: but apparently, it gives me three different colors. Plus, would it still work if $im points to an image file?
rgb stands for red, green and blue - so you'll always have three integers (ranging from 0 to 255).
eg. white (255,255,255), black (0,0,0)
For more information I'd recommend this article.

aerodromoi
robertboyle
Forum Newbie
Posts: 2
Joined: Sun May 07, 2006 6:18 am

yeah

Post by robertboyle »

Yes, I knew about the RGB, but can we convert the resulting color to HTML for example?

Thx
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: yeah

Post by aerodromoi »

robertboyle wrote:Yes, I knew about the RGB, but can we convert the resulting color to HTML for example?

Thx
Yes, we can ;)

Code: Select all

$hex = "#".dechex($r).dechex($g).dechex($b);
aerodromoi
Post Reply