Colorizing

Colorizing images is fairly easy to do. The easiest way to colorize an image is fairly simple to grasp. Create an image of the same dimensions and fill that image with the color you want to change it to. This new image is then placed on top of the older image, giving it a colorized look.

colorizing” makes use of the built-in PHP functions imagesx( ), imagesy( ), imagesetpixel( ), imagecolorat( ), imagecolorsforindex( ), imagecreate( ), imagecolorallocate( ), imagefill( ), imagecopymerge( ) and imagedestroy( ).

01 <?php
02 
03 function imagecolorize(&$im,&$col,$pct)
04 {
05     // Get the image's width
06     $im_w = imagesx($im);
07     // Get the image's height
08     $im_h = imagesy($im);
09     // Set a pixel with the color, so we can get it easily
10     $setpixel = imagesetpixel($im,$im_w,0,$col);
11     // Get the color
12     $index = imagecolorat($im,$im_w,0);
13     // Find the color in the index
14     $rgb = imagecolorsforindex($im,$index);
15     // Get the red value
16     $r = $rgb['red'];
17     // Get the green value
18     $g = $rgb['green'];
19     // Get the blue value
20     $b = $rgb['blue'];
21     // Create the layover
22     $layover = imagecreate($im_w,$im_h);
23     // Allocate the color on this image
24     $color = imagecolorallocate($layover,$r,$g,$b);
25     // Fill the image with the new color(this really isn't needed)
26     $fill = imagefill($layover,0,0,$color);
27     // Merge the layover on top of the older image
28     $merge = imagecopymerge($im,$layover,0,0,0,0,$im_w,$im_h,$pct);
29     imagedestroy($layover); // Destroy the layover
30 }
31  
32 ?>