#native_company# #native_desc#
#native_cta#

Advanced Image Editing Under the GD Library Page 2

By Brandon “Mordecai” Cash
on May 26, 2003

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.

<?php

function imagecolorize(&$im,&$col,$pct) {

    
// Get the image's width

    
$im_w imagesx($im); 

    
// Get the image's height

    
$im_h imagesy($im); 

    
// Set a pixel with the color, so we can get it easily

    
$setpixel imagesetpixel($im,$im_w,0,$col); 

    
// Get the color

    
$index imagecolorat($im,$im_w,0); 

    
// Find the color in the index

    
$rgb imagecolorsforindex($im,$index); 

    
// Get the red value

    
$r $rgb["red"];

    
// Get the green value

    
$g $rgb["green"]; 

    
// Get the blue value

    
$b $rgb["blue"]; 

    
// Create the layover

    
$layover imagecreate($im_w,$im_h); 

    
// Allocate the color on this image

    
$color imagecolorallocate($layover,$r,$g,$b); 

    
// Fill the image with the new color (this really isn't needed)

    
$fill imagefill($layover,0,0,$color); 

    
// Merge the layover on top of the older image

    
$merge imagecopymerge($im,$layover,0,0,0,0,$im_w,$im_h,$pct);

    
imagedestroy($layover); // Destroy the layover

}

?>



If we use a blue layover RGB(0,0,255), we get this result:
Mona Lisa
Blue Mona Lisa
Now comes the really fun part of GD–text functions. If you’ve familiarized yourself with the function list at php.net, I’m sure you know that there aren’t many. In fact, there are only two TTF functions: imagettftext and imagettftextbbox. The former places text onto an image, while the latter returns the bounding box in pixels of the text. Earlier in this tutorial, I pointed you toward this tutorial, which hosts a very simple (but useful) script that uses a bounding box to create the image dimensions. This assures that the text fits the image perfectly.
Since PHP has the ability to use so many image functions, you should be able to use them in combination with the TTF functions to create some interesting effects. Such examples would be outlines, textures, shadows, and translucent text.

Shadowing

Perhaps the easiest text effect is shadowing. Shadowing is, quite simply, drawing text at an offset, and then drawing the original text over it. You could use translucent text to create a better shadow effect, but you haven’t read about translucent text yet.

<?php

function imagettftextshadow(&$im,$size,$angle,$x,$y,&$col,

            &
$shadowcolor,$fontfile,$text,$offsetx,$offsety

             
) {

    
// Draw the shadow

    
$text1 imagettftext($im,$size,$angle,$x+$offsetx,$y+$offsety,$shadowcolor,$fontfile,$text);

    
// Draw the original text

    
$text2 imagettftext($im,$size,$angle,$x,$y,$col,$fontfile,$text); 

}

?>



Quite obviously, this is extremely easy to do. I’ll leave it up to your imagination to wonder what shadowing looks like.