Here is the third installment of PHP Artwork. ImageTTFText is yet another function that will allow you to really strut your stuff in creating images on the fly. This function is used in many of your “Captcha programs” for your contact pages.
Just in case you missed the previous installments. We used the imageline in an article called PHP Artwork. Imageline and a FOR flow control can create some really cool “prisms.” We also showed you how to use the ImageCreateFromJPEG function. By using this function you can take a small 80px by 80px image and fill up a browser using any number of strings as the fill.
In this example of PHP artwork, we will be using a true type font called cowboys. You can use any ttf from your Window font folder, make sure to correct the path in your code snippet or put the font in the same directory as your file. Of course, you will want to change the name in your coding. On to the fun stuff. First, we declare the image variable and set the size. Explained in earlier posts but the ImageColorAllocate is where you set your colors. In our case, it is black on white. The previous example used green on white.
The next line is the doozy. We are going to use the ImageTTFText function and set our parameters. (file, float, angle, x, y, call color, fontfile, string). Lastly, lets put the “image” on the page. Pretty simple, huh? Setting colors and the float can be a challenge when you first start out.
The code is pretty straightforward but I am happy to answer any questions via your comments.
<?php
$image = ImageCreate(200, 80);
$white = ImageColorAllocate($image, 0xFF, 0xFF, 0xFF);
$black = ImageColorAllocate($image, 0x00, 0x00, 0x00);
ImageTTFText($image, 20, 0, 10, 40, $black, 'cowboys.ttf', 'The TTF font');
header('Content-Type: image/png');
ImagePNG($image);
?>

This completes our third installment of PHP Artwork. I hope you have use for it and please explore. Feel free to share your experiences.
Coming up we are going to build a green box using the ImageFilledRectangle function and then we are going to add text to it using the PHP built-in function called ImageString. So stay tuned.
Happy Coding!
Z
Tags: Extend, fun, Functions, PHP


Want Something Else?