PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

imagecolorsforindex> <imagecolorresolvealpha
Last updated: Fri, 26 Sep 2008

view this page in

imagecolorset

(PHP 4, PHP 5)

imagecolorsetSet the color for the specified palette index

Description

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

This sets the specified index in the palette to the specified color. This is useful for creating flood-fill-like effects in palleted images without the overhead of performing the actual flood-fill.

Parameters

image

An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

index

An index in the palette.

red

Value of red component.

green

Value of green component.

blue

Value of blue component.

Return Values

No value is returned.

Examples

Example #1 imagecolorset() example

<?php
// Create a 300x100 image
$im imagecreate(300100);

// Set the background to be red
imagecolorallocate($im25500);

// Get the color index for the background
$bg imagecolorat($im00);

// Set the backgrund to be blue
imagecolorset($im$bg00255);

// Output the image to the browser
//header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>

See Also



imagecolorsforindex> <imagecolorresolvealpha
Last updated: Fri, 26 Sep 2008
 
add a note add a note User Contributed Notes
imagecolorset
Thomas Mueller
23-Apr-2008 07:42
Here is a function to colorize a picture gradient style.

All you have to do is to pass an img-object and an array of colors.

e.g.

$arr = array('#000000', '#990000', '#00FFFF', '#FFFFDD');
colorize ($img, $arr);

<?php
function colorize($imgdata, $palette)
{
   
imageTrueColorToPalette($imgdata,false,0xFF);
   
$l = count($palette)-1;
   
$i = imagecolorstotal($imgdata);
    while (
$i--)
    {
        list(
$r,$g,$b) = array_values(imageColorsForIndex($imgdata,$i));
       
       
$grayscale = ($r*.3 + $g*.59 +$b*.11) / 0xFF;
       
       
$pos = $l*$grayscale;
       
       
$perc = $pos-floor($pos);
       
       
$tbase = str_replace("#", '', $palette[$pos]);
       
$baseR = hexdec(substr($tbase,0,2));
       
$baseG = hexdec(substr($tbase,2,2));
       
$baseB = hexdec(substr($tbase,4,2));
       
       
$tmix = str_replace("#", '', $palette[$pos+1]);
       
$mixR = hexdec(substr($tmix,0,2));
       
$mixG = hexdec(substr($tmix,2,2));
       
$mixB = hexdec(substr($tmix,4,2));
       
       
$resR = $baseR+($mixR-$baseR)*$perc;
       
$resG = $baseG+($mixG-$baseG)*$perc;
       
$resB = $baseB+($mixB-$baseB)*$perc;
       
       
imagecolorset($imgdata, $i, $resR, $resG, $resB);
    }
}
?>
heavyraptor2 at hotmail dot com
20-Feb-2008 10:08
I've been looking for a simple function which "colorizes" an image, without any success ... it looks like a lot of people mean something different with "colorizing" an image, because actually colorzing means multiplying the old color's grayscale with the new color. So a white pixel would become 100% of the colorize-to-color and a black pixel would stay black (I know I can't explain well ... I hope you understood, otherwise take a look at the example below the functions' code).

<?php
function image_colorize(&$img,$rgb) {
 
imageTrueColorToPalette($img,true,256);
 
$numColors = imageColorsTotal($img);

  for (
$x = 0; $x < $numColors; $x++) {
    list(
$r,$g,$b) = array_values(imageColorsForIndex($img,$x));

   
// calculate grayscale in percent
   
$grayscale = ($r + $g + $b) / 3 / 0xff;

   
imageColorSet($img,$x,
     
$grayscale * $rgb[0],
     
$grayscale * $rgb[1],
     
$grayscale * $rgb[2]
    );
  }

  return
true;
}
?>

Example of usage:
<?php
$color
= array(0xff,0xaa,0x2a); // color to convert to
$url = 'http://sundog.net/images/uploads/1_google_logo.jpg';
$img = imageCreateFromJpeg($url);

image_colorize($img,$color);

header('Content-type: image/gif');
imageGif($img);
exit;
?>

Enjoy
admin at phpgfx dot com
25-Aug-2007 08:37
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 );
    }
}

?>
moxleystratton.com
05-Oct-2006 12: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
02-Oct-2004 10: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
23-Apr-2004 06: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
08-Aug-2002 11: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 02: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;
 
 }

imagecolorsforindex> <imagecolorresolvealpha
Last updated: Fri, 26 Sep 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites