PHP
  Home arrow PHP arrow Page 4 - 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  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
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 / 55
    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 - Displaying our image


    (Page 4 of 7 )

    Displaying our image in a web browser is a synch, and takes just two easy steps:
    1. Telling the browser that we are outputting an image, and not the default HTML content type
    2. Creating the actual image
    Firstly, let me explain why we need to change the content type of our PHP script. By default, PHP is configured to send HTML output to the browser. The web server does this by sending a content-type = text/html header along with your HTML code. This tells the browser that it will be receiving HTML, and to process anything that comes from the server as pure HTML. Nothing else.

    In our example, we don’t want the browser to treat our page as HTML, because it doesn’t contain any. Our page will simply spit out the results of our new image. We want the browser to render our image as a standard JPEG image file, so we change the content type using the header function, like this:

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

    This line MUST be placed right at the top of your PHP script, before any output occurs for it to work correctly. There can be no spaces before this line, and it must be enclosed with the PHP <?php and ?> tags respectively.

    Secondly, we will want to actually output our image to the web browser. We can do this by using the imagejpeg function. The imagejpeg function simply takes one parameter, which is a reference to an image canvas created using the imagecreate function:

    imagejpeg($img_number);

    So, just to recap, our complete image generation script is shown below. Create a new file named random_number.php. Copy the code shown below into random_number.php and run the script from your web browser.

    <?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);

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

    imagejpeg($img_number);

    ?>


    When you run random_number.php from within your web browser, you should see a simple rectangle, like the one shown below:

    The result of our simple image generation script

    If you don’t see the rectangle in your browser, or if any errors occur, then make sure you have copied the code shown above exactly as it appears. Also, make sure there is no white space before the first <?php tag.

    Now that we’ve got our basic image out of the way, let’s actually generate a random number to display as part of our image.

    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-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT