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
| Approach | What changes |
|---|---|
| GD / Imagick / EXIF | GD operates on raster images in PHP; Imagick delegates to ImageMagick; EXIF reads metadata and is not an image decoder. |
| Choosing safely | Check image dimensions, true decoder availability, and actual output format rather than trusting a filename suffix. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Trusting the image extension or MIME header alone | Decode the source and verify the actual format, dimensions, and resource limits. |
| Forgetting transparency or aspect ratio | Preserve alpha for transparent formats and calculate dimensions before resampling. |
| Skipping operational constraints | Never 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.