search for in the  
<imagecolorallocatealphaimagecolorclosest>
Last updated: Thu, 19 May 2005

imagecolorat

(PHP 3, PHP 4, PHP 5)

imagecolorat -- Get the index of the color of a pixel

Description

int imagecolorat ( resource image, int x, int y )

Returns the index of the color of the pixel at the specified location in the image specified by image.

If PHP is compiled against GD library 2.0 or higher and the image is a truecolor image, this function returns the RGB value of that pixel as integer. Use bitshifting and masking to access the distinct red, green and blue component values:

Example 1. Access distinct RGB values

<?php
$im
= ImageCreateFromPng("rockym.png");
$rgb = ImageColorAt($im, 100, 100);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
?>

See also imagecolorset() and imagecolorsforindex().



User Contributed Notes
imagecolorat
sys NO SPAM coder at gmail dot NOSPAM dot com
26-Apr-2005 05:40
Just for fun: another snippet for drawing images using html only X)

<html>
<head>
<style>
<!--
body {
font-family: courier new, courier;
font-size: 7pt;
background: #000000;
color: #000000;
}
-->
</style>
</head>
<body>
<font color="#000000">
<?php

$im
= ImageCreateFromPng($_GET['img']);

$lastcolor=0;
for(
$y=0;$y<imagesy($im);$y++)
{
  for(
$x=0;$x<imagesx($im);$x++)
  {
  
$pixel = ImageColorAt($im, $x, $y);
   if(
$lastcolor != $pixel)
   {
    
printf('</font><font color="#%06x">', $pixel);
    
$lastcolor = $pixel;
   }
   echo
"*";
  }
  echo
"<br />";
}

?>
</font>
</body>
</html>
18-Jan-2005 04:08
Optimal:
<?php
$rgb
= imagecolorat($im, $x, $y);
$r = ($rgb >> 16);
$g = ($rgb >> 8) & 255;
$b = $rgb & 255;
?>
As you see, you can also use decimal values (faster).
jed at jed dot bz
19-Oct-2004 04:14
In my various play stages with photomosaics I ended up writing this function that works with GD 2. The conversion from RGB to HSL is straightforward and there are a number of papers on it on our beloved Internet; however, this function simply finds the lightness value for a pixel and works exactly the same as imagecolorat().

<?php

/* Computes the lightness of a pixel. Lightness is used in HSL notation, as well
 * as in various operations on images. This function returns a normalized value
 * for lightness between 0 and 1, inclusive (0.0 being black, 1.0 being white).
 *
 * double imagelightnessat(resource img, int x, int y)
 *        img        An open image resource to operate on (true color or palette)
 *        x, y        The coordinates of the pixel to work on
 *
 * by Jed Smith <?php $u = "jed"; $d = "bz"; printf("<%s@%s.%s>", $u, $u, $d) ?>
 */
function imagelightnessat($img, $x, $y)
{
   if(!
is_resource($img))
   {
      
trigger_error("imagelightnessat(): supplied argument is not a valid "
          
. "Image resource", E_USER_WARNING);
       return
0.0;
   }
  
$c = @imagecolorat($img, $x, $y);
   if(
$c === false) return false;
   if(
imageistruecolor($img))
   {
      
$red = ($c >> 16) & 0xFF;
      
$green = ($c >> 8) & 0xFF;
      
$blue = $c & 0xFF;
   }
   else
   {
      
$i = imagecolorsforindex($img, $c);
      
$red = $i['red'];
      
$green = $i['green'];
      
$blue = $i['blue'];
   }   
  
$m = min($red, $green, $blue);
  
$n = max($red, $green, $blue);
  
/* Because RGB isn't normalized in GD, we divide by 510 here.
     *  Lightness = (Max(RGB) + Min(RGB)) / 2
     * But that's assuming red, green, and blue are 0 through 1 inclusive.
     * Red, green, and blue are actually 0-255 (255 + 255 = 510).
     */
  
$lightness = (double)(($m + $n) / 510.0);
   return(
$lightness);
}

?>
mumig at poczta dot onet dot pl
27-Feb-2004 10:13
imagecolorat() works differently for png's with true color and for paletted png's - for true color it returns value of color, for paletted it returns index number and you have to use  imagecolorsforindex() to get rgb color value.
geat AT zoom DOT co DOT uk
10-Jun-2003 06:35
The HTML image renderer didn't work for me for some reason, but here's a version that does...

echo '<table border="0" cellspacing="0" cellpadding="0">';
$im = ImageCreateFromJPEG("IMAGE NAME");
$width = imagesx($im);
$height = imagesy($im);
for ($cy=0;$cy<$height;$cy++) {
  echo '<tr>';
  for ($cx=0;$cx<$width;$cx++) {
   $rgb = ImageColorAt($im, $cx, $cy);
   $col = imagecolorsforindex($im, $rgb);
   printf('<td width="2" height="2" bgcolor=#%02x%02x%02x></td>', $col["red"], $col["green"], $col["blue"]);
  }
echo '</tr>';
}
echo '</table>';
28-May-2003 10:54
Just for fun, here is a little snippet that "paints" a image in html only. Try it out with very small images, as this is a real browser-killer. mozilla > 120mb ram, ie > 100mb with a 300x200 image :-)

$file = "image.png";
$im = imagecreatefrompng($file);
$size_arr = getimagesize($file);

echo "<table width=".$size_arr[0]." height=".$size_arr[1]." cellpadding=0 cellspacing=0 border=0>";
for ($y=0; $y<$size_arr[1]; $y++) {
   echo "<tr>";
   for ($x=0; $x<$size_arr[0]; $x++) {
       $rgb = ImageColorAt($im, $x, $y);
       printf("<td width=1 bgcolor=%06x><img width=1></td>", $rgb);
   }
   echo "</tr>";
}
echo "</table>";
lachy at kennedia dot com
20-Jan-2003 08:25
It seems that if you don not have GD 2.0.1+ then the alpha value will not be available.
ceresATdivxmaniaDOTit
02-Jan-2003 05:46
To get RGB values of a pixel in a truecolor image:

<?
$image
=imageCreateFromJPEG('image.jpg');
$x = 10;
$y = 15;
$colorindex = imagecolorat($image,$x,$y);
$colorrgb = imagecolorsforindex($image,$colorindex);

echo
"RGB values of the pixel at position $x - $y are: $colorrgb['red'] $colorrgb['green'] $colorrgb['blue'] \n Apha value is: $colorrgb['alpha']";
?>
pete at spamisbad_holidian dot com
05-Jul-2002 02:36
If you have a truecolor image and absolutely need to get the actual color of a pixel, it seems like you could create a 16x16 indexed color image from a part of the True Color image containing the pixel(s) in question. Then you should be able to guarantee that the indexed sample has the indentical colors to the True Color image. you can then use this function to get the color index of a pixel in the sample.

Obviously, this would be an intensive way to get pixel colors, especially for a large image, but as far as I can tell, it's one of the very few ways to do it right now.

<imagecolorallocatealphaimagecolorclosest>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT