PHP GD Image Drawing

This post was written by Brandon on April 18, 2009
Posted Under: PHP

Recently, I had PHP GD installed on my server so I can use a CAPTCHA script on other sites hosted here. Since I had instaleld PHP GD, I figured I would take a look at it and see if I can get anything out of it.

After playing around, here is what I created:

As you can see, each time you refresh the page, the date and time is updated within the image!
The code is simple, the image is generated and you call the php file with the regular image tags.

In the code below, I have a check to only try to draw the image if a 1 is passed. The reason for this is because that allows you to generate the image directly within the php file.
Test it here: http://bncapps.com/img.php
Here, the code generates and displays the image at once.

The code is below, the functions should be easy to follow:

 
if ($_GET['draw'] == '1') {
  $img = imagecreate(150, 150);
  $white = imagecolorallocate($img, 255, 255, 255);
  $black = imagecolorallocate($img, 0, 0, 0);
  $red   = imagecolorallocate($img, 255,   0,   0);
 
  //             x|  y|  width|height|startangle|endangle
  imagearc($img, 50, 70, 100, 100,  0, 360, $black);
  //                font x   y
  imagestring($img, 5, 8, 60, date("Y-m-d"), $black);
  imagestring($img, 5, 12, 75, date("H:i:s"), $black);
  imagestring($img, 5, 12, 1, 'BncApps.com', $red);
  imagepng($img, null, 3);
}
 

Then, to draw the image, you simply call the php file:

<img src="img.php?draw=1"/>

If you have any questions, or further suggestions, post them!

Add a Comment

required, use real name
required, will not be published
optional, your blog address

Previose Post: