Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 43,846 times

Contents

Downloads

Related Categories

Image Generation on the FLY using PHP - Generating the random number

jdk

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.

A fellow who believes in simple things, simple way.

Comments

  • dynamic image on the fly

    Posted by babulgogoi on 02 Apr 2005

    Hi

    I saw them using a image-on-the-fly script as (at www.worldspaceasia.com):
    Image Generation on the fly

    Posted by chukws on 06 Nov 2004

    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 co...

  • php_gd.dll

    Posted by altimastr on 03 Sep 2004

    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 ...

  • Checking

    Posted by niceguy_reddy on 18 Aug 2004

    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 d...

  • great tutorial, thanks

    Posted by Idea_Rat on 05 Aug 2004

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