How to Generate Image using PHP

Sheeraz Gul Feb 02, 2024
  1. Use the GD Library to Generate JPEG Images in PHP
  2. Use the GD Library to Generate PNG Images in PHP
  3. Use the GD Library to Draw an Image in PHP
How to Generate Image using PHP

PHP has a built-in library, GD, for image processing operations. This library can also generate images in different formats.

The GD library is not enabled by default; you need to enable it from php.ini.

Use the GD Library to Generate JPEG Images in PHP

First of all, check if the GD library is enabled or not; it can be checked from phpinfo() or php.ini. Once enabled from php.ini, we can start creating images.

<?php
// Create a blank image and add some content
$new_image = imagecreatetruecolor(400, 400);
$bg_color = imagecolorallocate($new_image,  125, 125, 125);

// Set the background color
imagefill($new_image, 0, 0, $bg_color);
$text = imagecolorallocate($new_image, 225, 225, 225);
imagestring($new_image, 25, 50, 200,  'Welcome to Delftstack Tutorial.', $text);
imagestring($new_image, 15, 50, 240,  'This is JPEG image generation', $text);

// Save the image as jpeg
imagejpeg($new_image, 'delftstack.jpeg');

// Free the memory
imagedestroy($new_image);
?>

The above code generates a jpeg image with 400x400 with the given content as a string and background color grey.

Output:

Use the GD Library to generate JPEG Images in PHP

Use the GD Library to Generate PNG Images in PHP

Generating a png image is almost the same process, just saving the image as png.

<?php
// Create a blank image and add some content
$new_image = imagecreatetruecolor(400, 400);
$bg_color = imagecolorallocate($new_image,  125, 125, 125);

// Set the background color
imagefill($new_image, 0, 0, $bg_color);
$text = imagecolorallocate($new_image, 225, 225, 225);
imagestring($new_image, 25, 50, 200,  'Welcome to Delftstack Tutorial.', $text);
imagestring($new_image, 15, 50, 240,  'This is PNG image generation', $text);

// Save the image as png
imagepng($new_image, 'delftstack.png');
// Free the memory
imagedestroy($new_image);
?>

Output:

Use the GD Library to generate PNG Image in PHP

Use the GD Library to Draw an Image in PHP

The GD library has few built-in functions to draw particular figures which include imagearc(), imagefilledpolygon(), imagechar(), and imageellipse().

Draw arcs:

<?php
$new_image = imagecreatetruecolor(200, 200);
$color_white = imagecolorallocate($new_image, 255, 255, 255);
$color_red   = imagecolorallocate($new_image, 255,   0,   0);
$color_green = imagecolorallocate($new_image,   0, 255,   0);
$color_blue  = imagecolorallocate($new_image,   0,   0, 255);
$bg_color = imagecolorallocate($new_image,  125, 125, 125);
imagefill($new_image, 0, 0, $bg_color);
// drawing a smiling face
//head
imagearc($new_image, 100, 100, 200, 200,  0, 360, $color_white);
// smile
imagearc($new_image, 100, 100, 150, 150, 25, 155, $color_red);
// eyes
imagearc($new_image,  60,  75,  50,  50,  0, 360, $color_green);
imagearc($new_image, 140,  75,  50,  50,  0, 360, $color_blue);

//save image
imagepng($new_image,'arcimage.png');

// free memory
imagedestroy($new_image);

?>

Output:

Use the GD library to Draw a smiling face Arc Image in PHP

Let’s draw a polygon:

<?php
// value points for polygon
$polygon_values = array(
            30,  80,
            50,  240,
            29,  80,
            240, 80,
            58,  60,
            100,  100
            ); // The random (x,y) points

$new_image = imagecreatetruecolor(250, 250);
$bg_color   = imagecolorallocate($new_image,  125, 125, 125);
$color_blue = imagecolorallocate($new_image, 0, 0, 255);

// Fill the background color
imagefilledrectangle($new_image, 0, 0, 249, 249, $bg_color);

// draw the polygon
imagefilledpolygon($new_image, $polygon_values, 6, $color_blue);

//save image
imagepng($new_image, 'polygon.png');
imagedestroy($new_image);
?>

The above code will generate a polygon with given random values.

Output:

Use the GD library to Draw a Polygon Image in PHP

Let’s try to draw rectangles:

<?php
$new_image = imagecreatetruecolor(400, 400);
$bg_color = imagecolorallocate($new_image,  255, 255, 255);
// Set the background color
imagefill($new_image, 0, 0, $bg_color);
// colors of rectangles
$color_magenta = imagecolorallocate($new_image, 255,0,255);
$color_olive = imagecolorallocate($new_image, 128,128,0);
$color_green = imagecolorallocate($new_image, 0,128,0);

//three rectangles with different size and color
imagerectangle($new_image, 100, 100, 300, 300, $color_magenta);
imagerectangle($new_image, 90, 120, 240, 200, $color_olive );
imagerectangle($new_image, 200, 240, 150, 320, $color_green);

//save image
imagepng($new_image, 'rectangles.png');
imagedestroy($new_image);
?>

The code above will generate three rectangles in one image.

Output:

Use the GD library to Draw a Rectangle Image in PHP

The GD library has more functions to draw different images. These functions can be easily used with given parameters.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP Image