#native_company# #native_desc#
#native_cta#

Advanced Image Editing Under the GD Library Page 5

By Brandon “Mordecai” Cash
on May 26, 2003

Textures

Textures are one of the hardest text effects possible. This is due to the fact that you need to know how to use a mask to create the texture.

<?php

function imagettftexttexture(&$im,&$textureim,$size,$angle,$x,$y,$fontfile,$text) {

    
$width imagesx($im); // Get the width of the image

    
$height imagesy($im); // Get the height of the image

    
$buffer imagecreate($width,$height); // Create the buffer image

    
$tile_w imagesx($textureim); // Get the width of the texture image

    
$tile_h imagesy($textureim); // Get the height of the texture image

    
$fits_x = (int)($im_w/$tile_w); // Find out how many times it fits horizontally

    
$fits_y = (int)($im_h/$tile_h); // Find out how many times it fits vertically

    
for ($i=0;$i<=$fits_x;$i++) { // Loop through every time (and another, for extra space) it fits horizontally

        
$x = (int)($tile_w*$i); // Change the X location based on where in the loop it is

        
for ($i2=0;$i2<=$fits_y;$i2++) { // Loop through every time it fits vertically

            
$y = (int)($tile_h*$i2); // Change the Y location

            
$copy imagecopy($im,$textureim,$x,$y,0,0,$tile_w,$tile_h); // Copy the image to the X,Y location

        
}

    }

    
$pink imagecolorclosest($im,255,0,255); // Create magic pink, a color commonly used for masks

    
$trans imagecolortransparent($im,$pink); // Make magic pink the transparent color

    
imagettftext($im,$size,$angle,$x,$y,-$pink,$fontfile,$text); // Draw text over magic pink without aliasing

    
imagecopy($buffer,$im,0,0,0,0,$width,$height); // Copy the main image onto the buffer

    
imagecopy($im,$buffer,0,0,0,0,$width,$height); // Copy the buffer back onto the main image

    
imagedestroy($buffer); // Destroy the buffer

}

?>



This is the longest code section from this entire tutorial, all for a simple textured effect. You see this done in many graphic-editing programs, and now you know how it can be done through PHP and GD. Since we had to use very specific colors in order to get the mask to work, aliasing the text is nearly impossible; therefore, the image may appear more “crude,” or choppy. If we call imagettftexttexture ($img,$texture,40,0,10,50,”arial.ttf”,”AaBbCcDd”) where $texture contains a brick tile, we can create this image:
ABCDEF with brick
This concludes the advanced image editing tutorial. Hopefully, you can have some fun with advanced features in GD, and create some interesting image scripts. Thanks for sticking around!
Every function in this tutorial (and more) can be found here.

1
|
2
|
3
|
4
|
5