Library tutorials & articles

Image Generation on the FLY using PHP

Generating the random number

Generating our random number is very easy. We will simply create a function named get_random which will return a random number between 1 and the built-in PHP function, getrandmax(). Getrandmax() is an implementation specific function, and returns the largest seed value available on the current system. Our get_random function looks like this:

function get_random()
{
    srand(time());
    $max = getrandmax();
    return rand(1,$max) + rand(1,$max) ;
}

To actually place any piece of textual data onto our image, we use the imagestring function. The imagestring function takes six parameters, as shown below:

int imagestring (int im, int font, int x, int y, string s, int col)

im: A reference to an image canvas created using the imagecreate function
font: An integer value specifying the number of the font to draw the text in. If font is 1, 2, 3, 4 or 5, then a built-in font is used
x: The horizontal displacement from the left of the image where the text will be drawn from, measured in pixels.
y: The vertical displacement from the top of the image where the text will be drawn from, measured in pixels.
s: The text to be drawn on the image
col: The color that the text will be painted. A reference to a color created using the imagecolorallocate function.

The following code will use our get_random function to generate a random number. It will also use the imagestring function to draw that number onto our image canvas:

$number = get_random();
Imagestring($img_number,9,30,15,$number,$black);

For your convenience, the complete PHP script to generate our image containing a random number is shown below:

<?php

//random_number.php
$img_number = imagecreate(100,50);
$white = imagecolorallocate($img_number,255,255,255);
$black = imagecolorallocate($img_number,0,0,0);
$grey_shade = imagecolorallocate($img_number,204,204,204);

imagefill($img_number,0,0,$grey_shade);
ImageRectangle($img_number,5,5,94,44,$black);
ImageRectangle($img_number,0,0,99,49,$black);


$number = get_random();
Imagestring($img_number,9,30,15,$number,$black);

header("Content-type: image/jpeg");
imagejpeg($img_number);

function get_random()
{
    srand(time());
    $max = getrandmax();
    return rand(1,$max) + rand(1,$max) ;
}

?>

Copy the code shown above into random_numer.php, save the file, and load it into your web browser. You should be presented with an image similar to the one below (remember that we are using a random number, so yours will be different to mine):

Actually displaying the image as part of a HTML page is simple. Instead of specifying the src attribute of an image as a filename, you specify the name of our PHP script, like this:

<img src="random_number.php">

When the browser loads this image, it will call the random_number.php script, which would return our image as a stream of binary data. The browser would then write this binary data to the screen as the image.

One last thing I will discuss in this article is changing the font of the random number as it appears in our image. This step is optional, but demonstrates the flexibility of PHP and the GD library.

Comments

  1. 02 Apr 2005 at 04:12

    Hi


    I saw them using a image-on-the-fly script as (at www.worldspaceasia.com):
    <a href="modules.php?name=Content&pa=listpagescategories&cid=2"><img src="includes/img.php?title=Western Classical/Jazz&textsize=8&border=1&imgype=randomTTF" border="0" alt="Western Classical/Jazz"></a>


    The :
    title=
    imgype=randomTTF


    are crucial!


    So whts the secret of the img.php?


    Regards

  2. 06 Nov 2004 at 04:32

    Very good tutorial and useful. But am wondering, if it can be converted to be used as a counter in a website, to get the number of visitors to the site. It will then start from a certain number and count sequencially upwards. It could start from 1 or 10.


    Thanks

  3. 03 Sep 2004 at 14:58
    after installing the gd.dll am i suppose to uncomment it in the php.ini file and if so do i restart my server to get it to load??? after putting it in my php.ini file and uncommenting it nothing came up? can you give me more detial instructions on fixing this  problem or installing this gd.dll? i am using windows xp iis5 and php5 just got it all working and jumping in for soome more, lol, well hope you can help .... altimastr
  4. 18 Aug 2004 at 02:09

    I used the script it was good. The random script is accessed as an image in another script, then how to access the number displayed on the image so that i can check that the user entered the correct displayed number.

  5. 05 Aug 2004 at 13:58

    very well-written & useful javascript:smilie('')

  6. 23 Jul 2003 at 12:49
    want to display an image on a  form when an item is selected from a ComboBox. the image will be stored on the a:\drive (floppy). there will be multiple items in the combobox, each 1 relating to a specific picture. i just need the  basic code to get the img to appear after selecting the item from the combobox. if n e 1 could email me the  code or tell me where to  get  it, that would be  great! oz_utester@hotmail.com

    thanx 4 ur time!
  7. 23 Jul 2003 at 12:47
    want to display an image on a  form when an item is selected from a ComboBox. the image will be stored on the a:\drive (floppy). there will be multiple items in the combobox, each 1 relating to a specific picture. i just need the  basic code to get the img to appear after selecting the item from the combobox. if n e 1 could email me the  code or tell me where to  get  it, that would be  great! shb53@hotmail.com

    thanx 4 ur time!
  8. 30 May 2003 at 13:09

    hi:


    I copied your code to my PHP4.3.x+Windows2000Server+Apache2, The picture does not show at a browser, only white page.  I checked the sapce befor the "<?php"  carefully.


    In fact, befor I have a similar bad experience that I can't show picture at my machine. But If I put the code to a linux machine , it works. The situation is all my other php codes, including session,mysql, work fine. Only for this method, header(header("Content-type:image/jpeg"), the send out a image, it does not work.


    I guess there must be some setting parameters problem at apache2. Does anyone have any idea?


    Thanks



  9. 17 Apr 2003 at 04:51

    I think your posting in the wrong place buddy! this is for creating and showing PHP images on-the-fly not VB! If you DO really want to take a PHP created image and put it in a VB form you can either post back here or email me at webmaster@isgeeky.com

  10. 17 Apr 2003 at 04:48

    To my knowledge it is not possible to send an image created on-the-fly using PHP without the content-type header being modified. Why do you not want to use the "Header("Content-Type: Image/jpg")" code?

  11. 31 Jan 2003 at 06:17
    Hi,
       Actually I have seen your image generation on the fly and i have used it on my project. But i facing a problem is i don't want to use a Header("Content-Type: Image/jpg") . So if you have link please send me on sshti123@hotmail.com.

    Vivek
  12. 31 Jan 2003 at 06:16
    Hi,
       Actually I have seen your image generation on the fly and i have used it on my project. But i facing a problem is i do want to use a Header("Content-Type: Image/jpg") . So if you have link please send me on sshti123@hotmail.com.

    Vivek
  13. 31 Jan 2003 at 06:15
    Hi,
       Actually I have seen your image generation f\on the fly and i have used it on my project. But i facing a problem is i do want to use a Header("Content-Type: Image/jpg") . So if you have link please send me on sshti123@hotmail.com.

    Vivek
  14. 09 Aug 2002 at 01:47

    i want to display an image on a  form when an item is selected from a ComboBox. the image will be stored on the a:\drive (floppy). there will be multiple items in the combobox, each 1 relating to a specific picture. i just need the  basic code to get the img to appear after selecting the item from the combobox. if n e 1 could email me the  code or tell me where to  get  it, that would be  great! oz_utester@hotmail.com


    thanx 4 ur time!

  15. 01 Jan 1999 at 00:00

    This thread is for discussions of Image Generation on the FLY using PHP.

Leave a comment

Sign in or Join us (it's free).

Divyesh Jariwala A fellow who believes in simple things, simple way.
AddThis

Related discussion

Related podcasts

  • EarthClassMail.com - Moving from LAMP to .NET 3.5

    Scott chats with Matt Davis, architect at EarthClassMail.com, about their move from a LAMP stack (Linux/Apache/mysql/PHP) to .NET 3.5. What's working, what's not, and what kinds of issues are they running into as their architect their solution.

Events coming up

  • Dec 3

    The Auckland PHP December meetup

    Auckland, New Zealand

    Topic: Magento E-Commerce platform Speaker: Robert Popovic, LERO9, Robert is the Technical Director and co-founder of LERO9. Robert attended the Electrotechnical Faculty at The University of Belgrade where he graduated with a Masters in Computer Science and Information Technology. Robert has worked exclusively in the field of web and software development throughout his career.

We'd love to hear what you think! Submit ideas or give us feedback