Posted by: Shofiur Rahman

Posted on: July 4, 2012 3:46 pm

-

Generating Images Using PHP

The basic four steps to generate an image using PHP are as follows:

  • Creating a canvas image on which to work.
  • Drawing Shapes or printing text on that canvas.
  • Outputting the final graphic
  • Cleaning up resources.

Sample PHP script:

// Creating a canvas image

$height = 200;

$width = 200;

$im = imagecreate($width, $height);

$white = imagecolorallocate($im, 255, 255, 255);

$black = imagecolorallocate($im, 0, 0, 0);

// Drawing Shapes or printing text

imagefill($im, 0, 0, $black);

imageline($im, 0, 0, $width, $height, $white);

imagestring($im, 4, 50, 150, 'Label text', $white);

// Output image

header('Content-type: image/png');

imagepng($im);

// Clean up

imagedestroy($im);

?>

Shofiur Rahman

Shofiur Rahman

As a professional web engineer, Shofiur is our Lead Web Developer and boasts years of experience with relevant applications and software. He is in the perfect position to bring our readers up to date with the latest development trends, including ideas relating to the Web 3.0 concept.
-
-

Tags: ,

Authors
Categories
Archives
Blogroll