Scale Image Using PHP and Maintaining Aspect Ratio

Scale Image Using PHP and Maintaining Aspect Ratio

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Scaling Images in PHP Without Imagick: The GD Alternative

Scaling an uploaded image while preserving its aspect ratio is a common requirement for web applications, yet it often trips up developers who assume they need the imagick extension. When imagick isn't available on the server, you fall back to PHP's built-in GD library. While GD is ubiquitous, understanding how to manipulate images without distorting them requires a specific approach that many tutorials gloss over.

The core challenge lies in calculating the correct output dimensions. If you simply set the width and height to fixed values—say, 800x600—you risk squashing or stretching the image unless the original file matches that exact ratio. To maintain the aspect ratio, you must determine the largest possible dimension within your constraints.

Calculating the Target Dimensions

The logic is straightforward: compare the width and height of the source image against your maximum limits. You calculate a scaling factor based on which dimension hits the limit first. If the original width is larger than the max width relative to its height, you scale down by width. If the height is the constraint, you scale by height.

Here is how that calculation looks in practice using GD functions:

// Get current image dimensions
$width = getimagesize($uploadedPath)[0];
$height = getimagesize($uploadedPath)[1];

$maxWidth = 800;
$maxHeight = 600;

$newWidth = $width;
$newHeight = $height;

if ($width > $maxWidth) {
    $scale = $maxWidth / $width;
    $newWidth = $maxWidth;
    $newHeight = round($height * $scale);
} elseif ($height > $maxHeight) {
    $scale = $maxHeight / $height;
    $newHeight = $maxHeight;
    $newWidth = round($width * $scale);
}

This snippet ensures that if an image is 2000x1500 and your max width is 800, the result will be exactly 800 wide. The height automatically adjusts to maintain the original proportions. If the image were already smaller than your constraints, it remains untouched.

Implementing the Resizing with GD

Once you have the target dimensions, creating the new canvas is trivial. You instantiate a new image resource using imagecreatetruecolor, then copy the original into it using imagecopyresampled. The latter function is preferred over imagecopyresized because it applies antialiasing, resulting in sharper edges when reducing image size.

$srcImage = imagecreatefromjpeg($uploadedPath); // Or png, gif, etc.

$newImage = imagecreatetruecolor($newWidth, $newHeight);

// Optional: Preserve transparency if needed (e.g., for PNGs)
if ($info['mime'] === 'image/png') {
    imagesavealpha($newImage, true);
}

imagecopyresampled(
    $newImage,
    $srcImage,
    0, 0,
    0, 0,
    $newWidth, $newHeight,
    $width, $height
);

Handling Formats and Quality

A frequent oversight involves the output format. If you are resizing a JPEG and saving it back as JPEG, you must account for quality settings. The default imagejpeg() call can produce artifacts if not tuned correctly. Passing a quality parameter (between 0 and 100) allows you to balance file size against visual fidelity.

imagejpeg($newImage, 'resized_image.jpg', 85);
imagedestroy($srcImage);
imagedestroy($newImage);

Why This Approach Matters

Relying on imagick is not inherently superior for simple scaling tasks; it adds a dependency and requires specific server configurations. The GD approach is lean, requiring no additional packages, and works on nearly every shared hosting environment. However, the trade-off is that GD does not support advanced features like image profiling or complex compositing out of the box.

If you are building an application where consistency across environments is critical, sticking to GD avoids the "missing extension" error entirely. For Laravel projects specifically, this logic can be encapsulated into a service class or helper, keeping your controllers clean. While resources like https://laravelcompany.com discuss broader Laravel architecture, the underlying image manipulation remains a fundamental PHP task that requires no framework magic to solve correctly.

The final result is a robust resizing mechanism that respects user-uploaded content without forcing them to distort their media.

Our network