← Back to PHP Course | Chapter 16: Testing & Tools | Lesson 8 of 10

PHP Image Processing (GD Library)

The GD library lets PHP draw and edit pictures, like a tiny paint program running on the server. It can resize photos, write text on them, and save the result.
Syntax
php
$image = imagecreatetruecolor(width, height);
$color = imagecolorallocate($image, red, green, blue);
imageline($image, x1, y1, x2, y2, $color);
imagepng($image);
imagedestroy($image);

Introduction to GD Library

The GD library is a PHP extension for creating and manipulating images programmatically -- resizing thumbnails, adding watermarks, or generating charts entirely on the server.

Example: Introduction to GD Library

php
<?php
// Print `extension_loaded('gd') ? "GD library available" : "GD not installed"` to the output
echo extension_loaded('gd') ? "GD library available" : "GD not installed";
?>

Creating Canvas Surfaces

imagecreatetruecolor() allocates a blank canvas of a given width and height in memory, which becomes the surface you draw shapes, text, or loaded images onto before saving or outputting it.

Example: Creating Canvas Surfaces

php
<?php
// Declare `$image`, set to `imagecreatetruecolor(100, 100)`
$image = imagecreatetruecolor(100, 100);
// Print `"Canvas created: " . imagesx($image) . "x" . imagesy($image)` to the output
echo "Canvas created: " . imagesx($image) . "x" . imagesy($image);
// Call `imagedestroy($image)`
imagedestroy($image);
?>

Drawing Geometry

GD provides functions like imageline(), imagerectangle(), and imageellipse() for drawing basic shapes directly onto a canvas, which is the foundation for generating simple graphics like charts on the fly.

Example: Drawing Geometry

php
<?php
// Declare `$image`, set to `imagecreatetruecolor(100, 100)`
$image = imagecreatetruecolor(100, 100);
// Declare `$color`, set to `imagecolorallocate($image, 255, 0, 0)`
$color = imagecolorallocate($image, 255, 0, 0);
// Call `imagerectangle($image, 10, 10, 90, 90, $color)`
imagerectangle($image, 10, 10, 90, 90, $color);
// Print "Rectangle drawn on canvas" to the output
echo "Rectangle drawn on canvas";
// Call `imagedestroy($image)`
imagedestroy($image);
?>

Loading and Resizing

imagecopyresampled() loads an existing image and produces a resized copy with smooth interpolation, which is how you generate properly scaled thumbnails instead of oversized originals.

Example: Loading and Resizing

php
<?php
// Declare `$src`, set to `imagecreatetruecolor(200, 200)`
$src = imagecreatetruecolor(200, 200);
// Declare `$thumb`, set to `imagecreatetruecolor(50, 50)`
$thumb = imagecreatetruecolor(50, 50);
// Call `imagecopyresampled($thumb, $src, 0, 0, 0, 0, 50, 50, 200, 200)`
imagecopyresampled($thumb, $src, 0, 0, 0, 0, 50, 50, 200, 200);
// Print `"Thumbnail: " . imagesx($thumb) . "x" . imagesy($thumb)` to the output
echo "Thumbnail: " . imagesx($thumb) . "x" . imagesy($thumb);
// Call `imagedestroy($src)`
imagedestroy($src);
// Call `imagedestroy($thumb)`
imagedestroy($thumb);
?>

Saving Image Files

Once processing is done, imagejpeg() or imagepng() either saves the result to a file on disk or streams it straight to the browser, provided you send the matching Content-Type header first.

Example: Saving Image Files

php
<?php
// Declare `$image`, set to `imagecreatetruecolor(50, 50)`
$image = imagecreatetruecolor(50, 50);
// Call `imagepng($image, "output.png")`
imagepng($image, "output.png");
// Print `file_exists("output.png") ? "Saved to output.png" : "Save failed"` to the output
echo file_exists("output.png") ? "Saved to output.png" : "Save failed";
// Call `imagedestroy($image)`
imagedestroy($image);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to call imagedestroy or to free memory for large images.
  2. Sending output before header('Content-Type: image/png').
  3. Trying to use GD functions when the GD extension is not enabled.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.