Jump to content

Retrieve pixel color from image


robertboyle

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.
Link to comment
https://forums.phpfreaks.com/topic/9235-retrieve-pixel-color-from-image/
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]

Archived

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

×
×
  • Create New...

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.