PHP  Images

php provides a range of functions that enable you to create, open, manipulate, and output images both to the web browser and to the disk.

PHP - creating images

create an image in php requires four steps:

  • :create a blank-image canvas for php to work with.this is an area of the web server's memory that is set aside for drawing onto.
  • includes setting up colors and drawing the shapes and text that you want within your image.
  • send your finished image to the web browser or save it to disk.
  • remove your image from the server memory.

first step : the imagecreatetruecolor function create a 24-bit true color image capable of including up to 16.7 million colors.

this function return an image resource identifier on success, false on errors.

syntax:

         $image   = imagecreatetruecolor( width, height )
  • width  :- specifies the image width
  • height :- specifies the image height

allocating colors: the imagecolorallocate() function return a color identifier representing the color composed of the given rgb components.

syntax:

         $color  = imagecolorallocate( image, red, green, blye)

color value represent between 0 and 255.

outputting images

before sending the image output to web browser php must set appropriate http header look:

     header( "content-type: image/jpeg" )   or  header ( "content-type : image/gif" )
     header( "content-type: image/png" )

for output images, you can use below three functions:

  • imagejpeg() outputs the image in jpeg format.
  • imagegif() outputs the image in gif format.
  • imagepng() outputs the image in png format.
<?php header( "content-type: image/png" );
             
             $image = imagecreatetruecolor( 500, 300)  or
             
                                       die( "cannot create new gd image stream" );
             
             $color = imagecolorallocate( $image, 222, 225,122 );
             
              imagepng( $image );
              
              imagedestroy( $image );             
?>

the imagedestroy() function frees memory associated with image.


php drawing an image

the imageline() function is used to draw a line in an image canvas.

      imageline(  image, x1, x2, y1, y2, color) 
<?php header ( "content-type: image/png" );
         
          $image = imagecreate( 400, 400 );   // create a empty  image canvas
          
          $co = imagecolorallocate( $image, 0, 0, 23);  //set  canvas  color
          
          $color = imagecolorallocate( $image,  0, 254, 25);  // line color
    
          imageline( $image, 100, 100, 600, 600, $color); 
          
          imagepng( $image );
          
          imagedestroy( $image );
            
?>

php draw rectangles

the imagerectangle() function is used to draw a rectangle in an image canvas.

      imagerectangle( image, x1, y1, x2,y2, color )
<?php header ( "content-type: image/png" );
 
          $image = imagecreate( 200, 300 );   // create a empty  image canvas
          
          $co = imagecolorallocate( $image, 252, 254, 204);  //set  canvas  color
          
          $color = imagecolorallocate( $image, 22, 222, 256);  // line color
          
          $col = imagecolorallocate( $image, 254, 10, 10);     // line color
          
          $cols = imagecolorallocate( $image, 10, 10, 254); // line color
          
          imagerectangle( $image, 10, 60, 160, 90, $color);// create first rectangle 
          
          imagerectangle( $image, 10, 100, 160, 150, $col);// create second rectangle
                    
          imagerectangle( $image, 10, 180, 180, 250, $cols);// create third rectangle
          
          imagepng( $image );
          
          imagedestroy( $image );
  
?>

php draw circles

the imageellipse() function is used to draw a circle in an image canvas:

      imageellipse( image, x, y, width, height, color)
  • x : x-coordinate of the center.
  • y : y-coordinate of the center.
  • width : specifies the width of the ellipses
  • height : specifies the height of the ellipses
<?php header ( "content-type: image/png" );
 
          $image = imagecreate( 500, 500 );   // create a empty  image canvas
          
          $co = imagecolorallocate( $image, 0, 0, 0);  //set  canvas  color
          
          $color = imagecolorallocate( $image, 22, 222, 256);  // line color
          
          $col = imagecolorallocate( $image, 254, 10, 10);  // line color
          
          $cols = imagecolorallocate( $image, 10, 10, 254);  // line color
          
          imageellipse( $image, 140, 120, 160, 90, $color); // create first circle 
          
          imageellipse( $image, 180, 100, 160, 150, $col); //create second circle
                    
          imageellipse( $image, 100, 100, 160, 150, $cols); // create third circle
          
          imagepng( $image );
          
          imagedestroy( $image );
  
?>

uses an existing images

      
<?php header( "content-type: image/jpeg" ); // this script use for jpeg images

   $e_image = imagecreatefromjpeg( "e:\\project\\images\\blueflower.jpg");
   
      imagejpeg( $e_image );
      
      imagedestroy( $e_image );
      
?>       

<?php header( "content-type: image/gif" ); // this script use for gif images

   $e_image = imagecreatefromgif( "e:\\project\\images\\box_model.gif");
   
      imagegif( $e_image );
      
      imagedestroy( $e_image );
      
?>       

<?php header( "content-type: image/png" ); // this script use for png images

   $e_image = imagecreatefrompng( "e:\\project\\images\\databaseview.png");
   
      imagepng( $e_image );
      
      imagedestroy( $e_image );
      
?>