Generating Images using PHP


The four basic steps in creating an image in PHP are-

  1. Creating a canvas image on which to work.
  2. Drawing shape,text on that canvas.
  3. Output the final graphic obtained.
  4. Clean the resources.

 

the code for same will be like this—>

lets name it createimage.php

<?php
// set up image
$height = 200;
$width = 200;
$im = imagecreatetruecolor($width, $height); //this will create a canvas, height and width are passed as parameters
$white = imagecolorallocate ($im, 255, 255, 255); //this function will select colour for your image
$blue = imagecolorallocate ($im, 0, 0, 64);
// draw on image
imagefill($im, 0, 0, $blue); // paint a blue background on which to draw
imageline($im, 0, 0, $width, $height, $white); //draws a line from top left corner to bottom right corner
imagestring($im, 4, 50, 150, ‘Sales’, $white); // this function adds a label to the graph
// output image
Header (‘Content-type: image/png’);
imagepng ($im); //ends the output to the browser in PNG format

// clean up
imagedestroy($im);
?>