Working with Images in PHP: A Quick Map of GD and Imagick
Get oriented on PHP's two image-handling extensions (GD and Imagick) and what each is for, with one runnable GD example, before diving into the full Image Processing lesson for resizing, thumbnails, EXIF metadata, and upload-safety details.
PHP handles raster images through two extensions: GD, bundled with virtually every PHP install and built for straightforward bitmap operations, and Imagick, a binding to the ImageMagick library for broader format support and richer compositing. This lesson is a short map of that landscape — the full mechanics of resizing, cropping, thumbnail generation, EXIF metadata, and safely validating uploaded images live in the dedicated 'Image Processing' lesson, which this one points to rather than repeats.
GD vs. Imagick, at a glance
GD works with an in-memory GdImage handle and a small set of format-specific load/save functions (imagecreatefromjpeg(), imagejpeg(), and their PNG/WebP/GIF equivalents), plus imagecopyresampled() as the one primitive underlying both resizing and cropping. It ships with most PHP installs and is the default choice for common web tasks — avatars, product thumbnails, resized uploads.
Imagick wraps the ImageMagick C library in an object-oriented API (new Imagick($path), $img->resizeImage(...)) and supports a much wider format range — PDF page rasterization, animated GIF/WebP composition, TIFF, PSD — along with finer color-space and compositing control. It's a separate system dependency rather than a core-bundled extension, so it's worth reaching for specifically when a project needs format coverage or compositing that GD doesn't offer.
Where the full depth lives
Resizing math (proportional fit vs. crop-to-fill), thumbnail generation, reading and stripping EXIF metadata for privacy, and — critically — validating and re-encoding uploaded images to defend against extension-spoofing and decompression-bomb attacks are all covered in depth in the 'Image Processing' lesson. If you're building an upload pipeline, that lesson (not this one) is the one to work through end to end.
• Resizing/cropping mechanics and quality settings per format: see Image Processing. • EXIF metadata reading and why re-encoding strips it: see Image Processing. • Upload validation against spoofed extensions and image-bomb resource exhaustion: see Image Processing.
Code Examples
A minimal proportional resize: compute the target height from the same aspect ratio as the source, allocate a destination canvas of that size, and let imagecopyresampled() do the actual pixel resampling in one call. Expected result: photo-thumb.webp written at 320 pixels wide, height scaled proportionally.
imagecopyresampled($dst, $src, 0, 0, 0, 0, $targetW, $targetH, $srcW, $srcH);
<?php
declare(strict_types=1);
$src = imagecreatefromjpeg('photo.jpg');
$srcW = imagesx($src);
$srcH = imagesy($src);
$targetW = 320;
$targetH = (int) round($srcH * ($targetW / $srcW));
$dst = imagecreatetruecolor($targetW, $targetH);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $targetW, $targetH, $srcW, $srcH);
imagewebp($dst, 'photo-thumb.webp', 82);
imagedestroy($src);
imagedestroy($dst);Why this lesson stays short
Image handling is one topic where the operational details (dimension guards before decoding, EXIF stripping, format-specific quality settings) matter more than a second overview of them — so this lesson exists to orient you to GD vs. Imagick and then hand off to the Image Processing lesson, which is where the production-grade patterns live.
move_uploaded_file($_FILES['avatar']['tmp_name'], $dest); // store the raw upload as-is
$img = imagecreatefromjpeg($tmp); imagejpeg($img, $dest, 85); // decode and re-encode before storing
Storing uploaded bytes verbatim skips both metadata stripping and payload-neutralizing re-encoding — see the Image Processing lesson's upload-validation section for the full dimension-guard-plus-re-encode pipeline.
Which PHP image extension is bundled with virtually every PHP install and best suited to common web tasks like thumbnails? A. Imagick B. GD C. Sodium D. mbstring
B. GD — GD ships with most PHP installations and handles common bitmap operations (resize, crop, format conversion) without an extra system library dependency; Imagick is the separate ImageMagick binding used when broader format/compositing support is needed.