Jump to content


Photo

Retrieve pixel color from image


  • Please log in to reply
2 replies to this topic

#1 robertboyle

robertboyle
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 07 May 2006 - 11:30 AM

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.
-- Everything is part of a plan. [a href="http://www.bluleb.com" target="_blank"]http://www.bluleb.com[/a]

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 07 May 2006 - 02:05 PM

It gives you three components of the color at that pixel - the red, green, and blue values (RGB values)
Legend has it that reading the manual never killed anyone.
My site

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 07 May 2006 - 05:58 PM

If it's not a truecolor image then imagecolorat() returns the index of the pixel color in the pallette so you need

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users