PHP
  Home arrow PHP arrow Page 5 - Generating Images on the Fly With PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Generating Images on the Fly With PHP
By: Divyesh Jariwala
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 58
    2001-12-13

    Table of Contents:
  • Generating Images on the Fly With PHP
  • The GD image library
  • Creating our image
  • Displaying our image
  • Generating the random number
  • Changing the font of our random number
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Generating Images on the Fly With PHP - Generating the random number


    (Page 5 of 7 )

    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)
    1. im: A reference to an image canvas created using the imagecreate function
    2. 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
    3. x: The horizontal displacement from the left of the image where the text will be drawn from, measured in pixels.
    4. y: The vertical displacement from the top of the image where the text will be drawn from, measured in pixels.
    5. s: The text to be drawn on the image
    6. 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):

    Our complete image, which now contains a random number

    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.

    More PHP Articles
    More By Divyesh Jariwala


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT