PHP allows developers to dynamically generate images using the GD Library, a powerful graphics extension. You can create images from scratch, add text, draw shapes, resize images, and even convert formats. This is commonly used for CAPTCHA creation, charts, watermarks, image processing tools, and more.
Before using GD functions, make sure your server has GD enabled.
You can check using:
phpinfo();
Look for GD Support → enabled.
Creating a Simple Image in PHP
The GD library allows you to create images in formats such as JPEG, PNG, GIF, and WebP.
Example: Creating a blank image
<?php
header("Content-Type: image/png");
$image = imagecreatetruecolor(400, 200);
$bgColor = imagecolorallocate($image, 200, 200, 200);
imagefill($image, 0, 0, $bgColor);
imagepng($image);
imagedestroy($image);
?>
This generates a 400×200 gray PNG image.
Adding Text to an Image
You can add custom text using imagestring() or imagettftext() (for custom fonts).
Example: Adding text
<?php
header("Content-Type: image/png");
$img = imagecreatetruecolor(400, 200);
$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $white);
$black = imagecolorallocate($img, 0, 0, 0);
imagestring($img, 5, 50, 80, "Hello, Savanka!", $black);
imagepng($img);
imagedestroy($img);
?>
Drawing Shapes
Drawing lines, rectangles, arcs, and filled shapes
$lineColor = imagecolorallocate($img, 0, 0, 255);
imageline($img, 0, 0, 200, 200, $lineColor);
$rectColor = imagecolorallocate($img, 255, 0, 0);
imagerectangle($img, 50, 50, 150, 150, $rectColor);
$filledColor = imagecolorallocate($img, 0, 255, 0);
imagefilledellipse($img, 200, 100, 100, 50, $filledColor);
Creating an Image with Custom Fonts (TTF)
Use imagettftext() to draw text using .ttf fonts.
$font = "fonts/Roboto-Regular.ttf";
imagettftext($img, 20, 0, 50, 120, $black, $font, "Dynamic Text");
Saving an Image to Server
Instead of outputting to browser, you can save to a file:
$image = imagecreatetruecolor(300, 150);
$color = imagecolorallocate($image, 100, 50, 200);
imagefill($image, 0, 0, $color);
imagepng($image, "generated/banner.png"); // Save here
imagedestroy($image);
Working with Existing Images
You can load images using:
$image = imagecreatefromjpeg("photo.jpg");
$image = imagecreatefrompng("image.png");
$image = imagecreatefromgif("graphic.gif");
This allows you to edit or overlay elements.
Resizing an Image
Example: Resize to new width and height
$src = imagecreatefromjpeg("photo.jpg");
$width = imagesx($src);
$height = imagesy($src);
$newWidth = 200;
$newHeight = 200;
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($dst, "photo_resized.jpg");
Adding Watermarks
Text watermark
imagettftext($image, 18, 0, 20, 40, $black, $font, "© Savanka");
Image watermark (overlay)
$watermark = imagecreatefrompng("logo.png");
imagecopy($image, $watermark, 10, 10, 0, 0, 100, 50);
CAPTCHA Image Generation
CAPTCHAs often use PHP GD to generate random codes.
$img = imagecreatetruecolor(120, 40);
$bg = imagecolorallocate($img, 240, 240, 240);
imagefill($img, 0, 0, $bg);
$black = imagecolorallocate($img, 0, 0, 0);
$code = rand(1000, 9999);
imagestring($img, 5, 30, 10, $code, $black);
imagepng($img);
Image Formats Supported by GD
| Function | Format |
|---|---|
| imagepng() | PNG |
| imagejpeg() | JPEG |
| imagegif() | GIF |
| imagewebp() | WebP |
| imagecreatefrom* | Load formats |
Common Errors When Generating Images
GD library not installed
Install using:
- Ubuntu/Debian:
sudo apt install php-gd - cPanel: Enable GD extension
Wrong headers
Always send:
header("Content-Type: image/png");
Missing font files
Check if .ttf font path exists before calling imagettftext().
Incorrect permissions
Make sure the folder where you save images is writable (CHMOD 755 or 777).
Practical Use Cases
- Generating dynamic charts
- Creating promotional banners automatically
- Watermarking images
- Generating profile thumbnails
- CAPTCHA security images
- Custom QR code or barcode images
- Certificate generation
Citations
🔗 View other articles about PHP:
http://savanka.com/category/learn/php/
🔗 External PHP Documentation:
https://www.php.net/manual/en/