iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 16, 2008
Who Are Your Top Friends on Facebook?
About
 
May 15, 2008
Reader Question - Would you host your client's work on your website?
About
 
May 15, 2008
How to Create an Ajax Autocomplete Text Field: Part 6
WebReference.com
 
May 14, 2008
Poll: Are the browser safe colors still needed?
About
 
May 14, 2008
Google Doctype launched
About
 
May 14, 2008
Web Editor Reviews - 6 New Reviews
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

Creating an Image with PHP's GD 

  Views:    6028
  Votes:    2
by Chief Programmabilities 3/07/05 Rating: 

Synopsis:

You want to create a new image on the fly with PHP, whether it is in GIF, PNG, or JPEG format.
Pages: 
The Article

Introduction

PHP is not limited to creating just HTML output. It can also be used to create and manipulate image files in a variety of different image formats, including gif, png, jpg, wbmp, and xpm. Even more convenient, PHP can output image streams directly to a browser.

Solution

PHP's GD library is a great tool to manipulate graphics on the fly. Use the ImageCreate() function and then use the ImagePng(), ImageJpeg(), or ImageGif function, depending on your system:

<?php
      header( "Content-type: image/gif" );   
      $im = ImageCreate( 153, 20 ); // width, height

      $red   = ImageColorAllocate( $im, 255, 0, 0 );
      $white = ImageColorAllocate( $im, 255, 255, 255 );

      ImageString($im,3,3,3,"Programmabilities.com",$white);
      ImageGif( $im );
      // Or ImageGif($im, $filename) if you want to save
      // it to a file. --Provide an optional second argument.

      ImageDestroy( $im ); // Free memory
?>

  
Discussion

If you run the preceding script, you should see something like the image on this page:
http://programmabilities.com/php/image-create.php.   Let's go over this line by line:

Line 2: Print the header telling the Web browser thet this is a GIF image.

Line 3: Create a GD image stream that can be manipulated by any of the other GD functions. The format of this image is not determined until we tell GD that it is a GIF and print it out on line 9.

Line 5: Create the background color for the image and set it to red using the ImageColorAllocate() function.

Line 6: Create the foreground color for the image and set it to white by using the ImageColorAllocate() function.

Line 8: Draw a string to image $im using a built-in font at x-pixel location of 3 and a y-pixel location of 3. The contents of the string is "Programmabilities.com" and the color of the string is white, denoted by the variable $white.

Line 9: Convert the image to a GIF and output to the browser.

Line last: Destroy the memory allocated for the GIF. This is done when the script executes anyway.



Chief Programmabilities,
Programmabilities.com
Pages: 

Similar/related articles:


 
  Sponsors