18.5 Getting the Color of a Certain Part of an Image


18.5 Getting the Color of a Certain Part of an Image

You want to get the color of a particular part of an image.

Technique

Use the ImageColorAt() function, which will return the color index for the specified pixel, and then translate that into the RGB values by using the ImageColorsForIndex() function:

 <?php $im = ImageCreateFromJpeg("colors.jpg"); $cindex = ImageColorAt($im, 34, 40); $rgb_color = ImageColorsForIndex($im, $cindex); echo "colors.jpg has a red value of {$rgb_color ['red']} a green value of "; echo "{$rgb_color['green']} and a blue value of {$rgb_color['blue']} at "; echo "a pixel position of 34, 40"; ?> 

Comments

The ImageColorAt() function returns the color index, not the RGB value of the specified pixel location. Therefore, you must use the ImageColorsForIndex() function, which converts a color index into its red, green, and blue values and returns those values as an associative array.

You might be wondering how these functions are useful. Why would someone ever need to find the RGB color of a certain part of an image? There is a good reason for these functions being in here, but first let me explain a bit about images and color palettes.

Every image has a color palette with a limited number of colors. For example, a GIF image has a 256-color palette, meaning that each GIF image can have only 256 unique colors. Also, the fewer colors used by the palette, the smaller the size of the image file. So, a GIF image with 16 colors has a smaller file size than a GIF image with 32 colors, and is much smaller than an image with 256 colors.

Knowing this, it becomes apparent that if you can reuse colors that are already in the image palette, the size of the image file will be smaller. By getting certain colors in an image, you can reuse those colors and reduce the file size of the image.

Another use of this function is to get the background color on which you are drawing your enhancements. For example, if the background is black, you probably will want to draw white text; or, if the background is white, you might want to draw some other color. After you get the colors for a certain area, you can perform some manipulations on the RGB indexes and create a color that matches perfectly with the background:

 <?php header("Content-type: image/png"); $im = ImageCreateFromPng("tst.png"); $cindex = ImageColorAt($im, 50, 100);  // middle of the image $rgb_color = ImageColorsForIndex($im, $cindex); if ($rgb_color["red"] > 150) $r = 10; else $r = 230; if ($rgb_color["green"] > 150) $g = 10; else $g = 180; if ($rgb_color["blue"]  > 150) $b = 10; else $b = 210; $color = ImageColorAllocate($im, $r, $g, $b); ImageString($im, 4, 25, 50, "Hello World", $color); ImagePng($im); ImageDestroy($im); ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net