PHP Image Processing (GD Library)
In this page:
$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
// Print `extension_loaded('gd') ? "GD library available" : "GD not installed"` to the output
echo extension_loaded('gd') ? "GD library available" : "GD not installed";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
- Forgetting to call
imagedestroyor to free memory for large images. - Sending output before
header('Content-Type: image/png'). - Trying to use GD functions when the GD extension is not enabled.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: