Jump to content

Archived

This topic is now archived and is closed to further replies.

robertboyle

Retrieve pixel color from image

Recommended Posts

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:

<?php
$im = ImageCreateFromPng("rockym.png");
$rgb = ImageColorAt($im, 100, 100);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 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.

Share this post


Link to post
Share on other sites
It gives you three [b]components[/b] of the color at that pixel - the red, green, and blue values (RGB values)

Share this post


Link to post
Share on other sites
If it's not a truecolor image then imagecolorat() returns the index of the pixel color in the pallette so you need

[code]$a = imagecolorat($im, 83, 50);
$c = imagecolorsforindex($im, $a);
printf('#%02X%02X%02X', $c['red'], $c['green'], $c['blue']);[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.