Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Image Functions : imagecolorsforindex

imagecolorsforindex

Get the colors for an index (PHP 4, PHP 5)
array imagecolorsforindex ( resource image, int index )

Example 993. imagecolorsforindex() example

<?php

// open an image
$im = imagecreatefrompng('nexen.png');

// get a color
$start_x = 40;
$start_y = 50;
$color_index = imagecolorat($im, $start_x, $start_y);

// make it human readable
$color_tran = imagecolorsforindex($im, $color_index);

// what is it ?
print_r($color_tran);

?>

The above example will output something similar to:

Array
(
  [red] => 226
  [green] => 222
  [blue] => 252
  [alpha] => 0
)

Code Examples / Notes » imagecolorsforindex

adspeed.com

To correct m4551 at abasoft dot it example:
ImageTrueColorToPalette($im,1,$t);
might give less colors than $t, so the for loop should call "$i<ImageColorsTotal($im)" instead of "$i<$t" just to be sure, or you'll get the warning: Color index [0-9] out of range


admin

this is a sepia filter using microsoft's definition
<?php
function imagesepia( $img ) {
$total = imagecolorstotal( $img );
for ( $i = 0; $i < $total; $i++ ) {
$index = imagecolorsforindex( $img, $i );
$red = ( $index["red"] * 0.393 + $index["green"] * 0.769 + $index["blue"] * 0.189 ) / 1.351;
$green = ( $index["red"] * 0.349 + $index["green"] * 0.686 + $index["blue"] * 0.168 ) / 1.203;
$blue = ( $index["red"] * 0.272 + $index["green"] * 0.534 + $index["blue"] * 0.131 ) / 2.140;
imagecolorset( $img, $i, $red, $green, $blue );
}
}
?>


strozek a deas harvard edu

Regarding m4551's method of conversion -- the actual CCIR-approved RGB-to-grayscale conversion is as follows:
grayscale component = 0.2125*R + 0.7154*G + 0.0721*B
(cf. CCIR Recommendation 709 for modern monitors)


slepichev

If you would like to change the intensity or lightness level of a specific color, you will need to convert the color format from RGB to HSL.
following function convert RGB array(red,green,blue) to HSL array(hue, saturation, lightness)
<?php
/**
* Convert RGB colors array into HSL array
*
* @param array $ RGB colors set
* @return array HSL set
*/
function rgb2hsl($rgb){
   
    $clrR = ($rgb[0] / 255);
    $clrG = ($rgb[1] / 255);
    $clrB = ($rgb[2] / 255);
   
    $clrMin = min($clrR, $clrG, $clrB);
    $clrMax = max($clrR, $clrG, $clrB);
    $deltaMax = $clrMax - $clrMin;
   
    $L = ($clrMax + $clrMin) / 2;
   
    if (0 == $deltaMax){
        $H = 0;
        $S = 0;
        }
   else{
        if (0.5 > $L){
            $S = $deltaMax / ($clrMax + $clrMin);
            }
       else{
            $S = $deltaMax / (2 - $clrMax - $clrMin);
            }
        $deltaR = ((($clrMax - $clrR) / 6) + ($deltaMax / 2)) / $deltaMax;
        $deltaG = ((($clrMax - $clrG) / 6) + ($deltaMax / 2)) / $deltaMax;
        $deltaB = ((($clrMax - $clrB) / 6) + ($deltaMax / 2)) / $deltaMax;
        if ($clrR == $clrMax){
            $H = $deltaB - $deltaG;
            }
       else if ($clrG == $clrMax){
            $H = (1 / 3) + $deltaR - $deltaB;
            }
       else if ($clrB == $clrMax){
            $H = (2 / 3) + $deltaG - $deltaR;
            }
        if (0 > $H) $H += 1;
        if (1 < $H) $H -= 1;
        }
    return array($H, $S, $L);
    }
?>


m4551

here's a function to greyscale an image even from a truecolor source (jpeg or png).
slightly poor quality, but very fast...
function imagegreyscale(&$img, $dither=1) {
if (!($t = imagecolorstotal($img))) {
$t = 256;
imagetruecolortopalette($img, $dither, $t);
}
for ($c = 0; $c < $t; $c++) {    
$col = imagecolorsforindex($img, $c);
$min = min($col['red'],$col['green'],$col['blue']);
$max = max($col['red'],$col['green'],$col['blue']);
$i = ($max+$min)/2;
imagecolorset($img, $c, $i, $i, $i);
}
}


tim

Here's a better grayscale, sepia, and general tinting function.  This function is better because:
1) Works with true color images (the other sepia code didn't).
2) Provides a more gooder grayscale conversion (yes, I said "more gooder").  The other grayscale code used imagetruecolortopalette, which just doesn't work well for grayscale conversion.
3) The other sepia code was really colorful, a little too much for my taste.  This function allows you to optionally set the tinting of the grayscale to anything you wish.
4) Single function for grayscale, sepia, and any other tinting you can dream up.
Here's some examples:
imagegrayscaletint ($img);  // Grayscale, no tinting
imagegrayscaletint ($img,304,242,209);  // What I use for sepia
imagegrayscaletint ($img,0,0,255);  // A berry blue image
The RGB values for tinting are normally from 0 to 255.  But, you can use values larger than 255 to lighten and "burn" the image.  The sepia example above does this a little, the below example provides a better example of lightening the image and burning the light areas out a little:
imagegrayscaletint ($img,400,400,400);  // Lighten image
imagegrayscaletint ($img,127,127,127);  // Darken image
<?
function imagegrayscaletint (&$img, $tint_r = 255, $tint_g = 255, $tint_b = 255) {
 $width = imagesx($img); $height = imagesy($img);
 $dest = imagecreate ($width, $height);
 for ($i=0; $i<256; $i++) imagecolorallocate ($dest, $i, $i, $i);
 imagecopyresized ($dest, $img, 0, 0, 0, 0, $width, $height, $width, $height);
 for ($i = 0; $i < 256; $i++) imagecolorset ($dest, $i, min($i * abs($tint_r) / 255, 255), min($i * abs($tint_g) / 255, 255), min($i * abs($tint_b) / 255, 255));
 $img = imagecreate ($width, $height);
 imagecopy ($img, $dest, 0, 0, 0, 0, $width, $height);
 imagedestroy ($dest);
}
?>


Change Language


Follow Navioo On Twitter
gd_info
getimagesize
image_type_to_extension
image_type_to_mime_type
image2wbmp
imagealphablending
imageantialias
imagearc
imagechar
imagecharup
imagecolorallocate
imagecolorallocatealpha
imagecolorat
imagecolorclosest
imagecolorclosestalpha
imagecolorclosesthwb
imagecolordeallocate
imagecolorexact
imagecolorexactalpha
imagecolormatch
imagecolorresolve
imagecolorresolvealpha
imagecolorset
imagecolorsforindex
imagecolorstotal
imagecolortransparent
imageconvolution
imagecopy
imagecopymerge
imagecopymergegray
imagecopyresampled
imagecopyresized
imagecreate
imagecreatefromgd2
imagecreatefromgd2part
imagecreatefromgd
imagecreatefromgif
imagecreatefromjpeg
imagecreatefrompng
imagecreatefromstring
imagecreatefromwbmp
imagecreatefromxbm
imagecreatefromxpm
imagecreatetruecolor
imagedashedline
imagedestroy
imageellipse
imagefill
imagefilledarc
imagefilledellipse
imagefilledpolygon
imagefilledrectangle
imagefilltoborder
imagefilter
imagefontheight
imagefontwidth
imageftbbox
imagefttext
imagegammacorrect
imagegd2
imagegd
imagegif
imagegrabscreen
imagegrabwindow
imageinterlace
imageistruecolor
imagejpeg
imagelayereffect
imageline
imageloadfont
imagepalettecopy
imagepng
imagepolygon
imagepsbbox
imagepsencodefont
imagepsextendfont
imagepsfreefont
imagepsloadfont
imagepsslantfont
imagepstext
imagerectangle
imagerotate
imagesavealpha
imagesetbrush
imagesetpixel
imagesetstyle
imagesetthickness
imagesettile
imagestring
imagestringup
imagesx
imagesy
imagetruecolortopalette
imagettfbbox
imagettftext
imagetypes
imagewbmp
imagexbm
iptcembed
iptcparse
jpeg2wbmp
png2wbmp
eXTReMe Tracker