Here is the fourth installment of PHP Artwork. Coming up we are going to build a green box using the ImageFilledRectangle function and then we are going to add a caption to it using the PHP built-in function called ImageString. So stay tuned.
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. The latest installment utilized the ImageTTFText to build a quick png graphic. Great for Captcha.
The setup for this example is pretty much like the previous installment. We set the same $image variable for our ImageCreate function. Its parameters are the width and height. Our graphic is set to 180px.
ImageColorAllocate does just what you would expect. Lets set our background and image colors. To keep it simple, we used green on white.
ImageFilledRectangle is a built-in function call. I bet you can guess what it does. =) The first parameter being the resource image or $image variable. Don’t forget to call in your color variable as the last parameter.
Lastly, we set our placement and string for the “caption” and then “call” the image. The code is pretty straightforward but I am happy to answer any questions via your comments.
<?php
$image =ImageCreate(180, 180);
$white = ImageColorAllocate($image, 0xFF, 0xFF, 0xFF);
$green = ImageColorAllocate($image, 0x00, 0xFF, 0x22);
ImageFilledRectangle($image, 50, 50, 150, 150, $green);
ImageString($image, 5, 50, 150, "Z Green Box", $green);
Header('Content-Type: image/png');
ImagePNG($image);
?>

This completes our fourth installment of PHP Artwork. I hope you enjoyed this simple tutorial. Feel free to share your experiences.
Happy Coding!
Z
Tags: Extend, Functions, PHP, Structure, Web Design


Want Something Else?