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



PHP : Function Reference : Image Functions : imagecolorset

imagecolorset

Set the color for the specified palette index (PHP 4, PHP 5)
void imagecolorset ( resource image, int index, int red, int green, int blue )


Code Examples / Notes » imagecolorset

dade dot c

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);
}


olivier

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;

}


moxleystratton.com

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);
}
}
?>


info

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 );    
}
}


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);
}
}


admin

Here is a function to turn an image into pure black and white
<?php
function imagepurebw( $img, $amount = 383 ) {
$total = imagecolorstotal( $img );
for ( $i = 0; $i < $total; $i++ ) {
$index = imagecolorsforindex( $img, $i );
array_pop( $index );
$color = ( array_sum( $index ) > $amount ) ? 255 : 0;
imagecolorset( $img, $i, $color, $color, $color );
}
}
?>


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