Tiling Images


You can also use one image to tile another image by appearing repeatedly in the background. Here's how you use this function:

 imagesettile(resource image, resource tile) 

This function sets the tile image to be used by all region-filling functions (such as imagefilledrectangle and imagefilledpolygon) when filling with the special color IMG_COLOR_TILED.

Here's an example, where we're going to tile an image with our image.jpg image and display some text on top of the results. First, we create a new imagewe're going to use the imagecreatetruecolor function here, not just imagecreate, to preserve the image in the tiles as much as possibleand then use imagecreatefromjpeg to create the tile we'll use to cover the background:

 $image_width = 300; $image_height = 200; $image = imagecreatetruecolor($image_width, $image_height); $tile = imagecreatefromjpeg ('image.jpg'); 

Now we're able to use imagesettile to tile the background of our image and then draw a filled rectangle with multiple tiles using imagefilledrectangle:

 $image_width = 300; $image_height = 200; $image = imagecreatetruecolor($image_width, $image_height); $tile = imagecreatefromjpeg ('image.jpg'); imagesettile($image, $tile); imagefilledrectangle ($image, 0, 0, $image_width, $image_height,     IMG_COLOR_TILED); 

Then we use imagestring to add some text to the result, as you see in phptile.php, Example 14.

Example 14. Tiling images, phptile.php
 <?php     $image_width = 300;     $image_height = 200;     $image = imagecreatetruecolor($image_width, $image_height);     $tile = imagecreatefromjpeg ('image.jpg');     imagesettile($image, $tile);     imagefilledrectangle ($image, 0, 0, $image_width, $image_height,         IMG_COLOR_TILED);     $drawing_color = imagecolorallocate($image, 0, 0, 0);     imagestring($image, 4, 20, 90, 'No worries....', $drawing_color);     Header("Content-type: image/jpeg");     imagejpeg($image);     imagedestroy ($image);     imagedestroy ($tile); ?> 

We'll embed the results in an HTML document, phptile.html:

 <HTML>     <HEAD>         <TITLE>Tiling images</TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>Tiling images</H1>             <BR>             <IMG src="/books/1/265/1/html/2/phptile.php">         </CENTER>     </BODY> </HTML> 

You can see what everything looks like in Figure 15. Nice.

Figure 15. Tiling images.




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net