Content · Web Development · PHP

Resizing Images with GD

Resize photographs by decoding the source, allocating the destination and resampling pixels.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Admin

Resize photographs by decoding the source, allocating the destination and resampling pixels.

What It Does and When to Use It


Resample an image; preserve source aspect ratio in real inputs. GD operates on raster images in PHP; Imagick delegates to ImageMagick; EXIF reads metadata and is not an image decoder.

Requirements, Inputs and Expected Behavior


Image decoding and encoding need the relevant GD, EXIF, or Imagick support; features vary by PHP build and server. Never upscale without reason; preserve alpha for PNG/WebP and verify the decoder returns GdImage.

Example 1: Basic Operation


Resample an image; preserve source aspect ratio in real inputs.

<?php
$source = imagecreatefromjpeg(__DIR__ . '/photo.jpg');
 if (!$source) throw new RuntimeException('Invalid JPEG');
 $target = imagecreatetruecolor(320, 180);
 imagecopyresampled($target, $source, 0, 0, 0, 0, 320, 180, imagesx($source), imagesy($source));
 imagejpeg($target, __DIR__ . '/small.jpg', 85);
 imagedestroy($source); imagedestroy($target);

Example 2: Behavior and Validation


Calculate dimensions without stretching small images or changing proportions.

<?php
[$width, $height] = getimagesize(__DIR__ . '/photo.jpg');
 $max = 600;
 $ratio = min(1, $max / max($width, $height));
 $newWidth = max(1, (int) round($width * $ratio));
 $newHeight = max(1, (int) round($height * $ratio));
 echo "$newWidth x $newHeight";

Practical Example: Application Use


Rescale a PNG and retain the alpha channel in the saved result.

<?php
$src = imagecreatefrompng(__DIR__ . '/source.png');
 $dst = imagescale($src, 240, -1, IMG_BICUBIC_FIXED);
 if (!$dst) throw new RuntimeException('Resize failed');
 imagealphablending($dst, false);
 imagesavealpha($dst, true);
 imagepng($dst, __DIR__ . '/scaled.png');
 imagedestroy($src); imagedestroy($dst);

Important Behavior and Edge Cases


The second example highlights: Calculate dimensions without stretching small images or changing proportions. The practical example adds: Rescale a PNG and retain the alpha channel in the saved result.

Comparison with Related Approaches


ApproachWhat changes
GD / Imagick / EXIFGD operates on raster images in PHP; Imagick delegates to ImageMagick; EXIF reads metadata and is not an image decoder.
Choosing safelyCheck image dimensions, true decoder availability, and actual output format rather than trusting a filename suffix.

Common Beginner Mistakes


MistakeCorrect approach
Trusting the image extension or MIME header aloneDecode the source and verify the actual format, dimensions, and resource limits.
Forgetting transparency or aspect ratioPreserve alpha for transparent formats and calculate dimensions before resampling.
Skipping operational constraintsNever upscale without reason; preserve alpha for PNG/WebP and verify the decoder returns GdImage.

Key Takeaways


  • Resize photographs by decoding the source, allocating the destination and resampling pixels.
  • Rescale a PNG and retain the alpha channel in the saved result.
  • Check image dimensions, true decoder availability, and actual output format rather than trusting a filename suffix.
Share this resourceSend this page to someone who may find it useful.