imagecolorset

(PHP 3, PHP 4, PHP 5)

imagecolorset -- 给指定调色板索引设定颜色

说明

void imagecolorset ( resource image, int index, int red, int green, int blue )

本函数将调色板中指定的索引设定为指定的颜色。对于在调色板图像中创建类似区域填充(flood-fill)的效果很有用,免去了真的去填充的开销。

参见 imagecolorat()


add a note add a note User Contributed Notes
moxleystratton.com
05-Oct-2006 06:59
If you want to convert a Color image to Grayscale without creating a blotchy image, use this color calculation:
<?php
function imagetograyscale($im)
{
   if (
imageistruecolor($im)) {
      
imagetruecolortopalette($im, false, 256);
   }

   for (
$c = 0; $c < imagecolorstotal($im); $c++) {
      
$col = imagecolorsforindex($im, $c);
      
$gray = round(0.299 * $col['red'] + 0.587 * $col['green'] + 0.114 * $col['blue']);
      
imagecolorset($im, $c, $gray, $gray, $gray);
   }
}
?>
dade dot c at email dot it
03-Oct-2004 04:47
this is helpful if you would like to implement a color theme system in your website... try it out
Davide Candiloro Italy

function colorize ($pngpath, $r, $g, $b)
/*
REQUIRES: $pngpath to be a valid path of a greyscale PNG-8 image with 64 colors palette
         $r, $g, $b to be integers in the range 0..255
EFFECTS: returns the png image colorized with the color represented by $r, $g, $b.
*/
{
   header("Content-type: image/png");
   $im = imagecreatefrompng("images/table.png");

   imagetruecolortopalette($im, FALSE, 256);
  
     for ($c = 0; $c < 64; $c++){ /*64 is the number of colors in the PNG-8 palette*/
       $col = imagecolorsforindex($im, $c);         
       imagecolorset ( $im, $c, $r*$col['red']/256, $g*$col['green']/256, $b*$col['blue']/256); /*replaces original greyscale palette with a colorized one*/
     }

   imagepng($im);
   imagedestroy($im);
}
m4551 at abasoft dot it
24-Apr-2004 12:19
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);
   }
}
info at devking dot com
09-Aug-2002 05:20
here's a simple function to greyscale an image...

function imagecolorgrey( &$img ) {
 for( $i=0; $i<imagecolorstotal( $img ); $i++ ) {
  $c = ImageColorsForIndex( $img, $i );
  $t = ($c["red"]+$c["green"]+$c["blue"])/3;
  imagecolorset( $img, $i, $t, $t, $t );   
 }
}
olivier at moostik dot net
12-Jul-2001 08:49
This function change colors of an image with ranges 0>100 for each primary color:

int ImageSelectiveColor (int im, int red, int green, int blue)

im is image pointer
red, green and blue are ranges 0->100

function ImageSelectiveColor($im,$red,$green,$blue)
 {
  for($i=0;$i<imagecolorstotal($im);$i++)
   {
   $col=ImageColorsForIndex($im,$i);
   $red_set=$red/100*$col['red'];
   $green_set=$green/100*$col['green'];
   $blue_set=$blue/100*$col['blue'];
   if($red_set>255)$red_set=255;
   if($green_set>255)$green_set=255;
   if($blue_set>255)$blue_set=255;
   imagecolorset($im,$i,$red_set,$green_set,$blue_set);
  
   }
  return $im;
 
 }