PHP resize image proportionally with max width or weight

PHP resize image proportionally with max width or weight

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

Stefan Bogdanescu

Founder & Senior Architect

PHP resize image proportionally with max width or height

When handling user uploads, you often encounter images that exceed your layout requirements. A common requirement is to enforce a maximum dimension—say, 500 pixels on either axis—while preserving the original aspect ratio. If an image is uploaded at 500x1000, simply forcing both dimensions to 500 would squash it into a square. Instead, you need to calculate the correct scaling factor so that the largest dimension hits exactly 500, while the other remains smaller.

The core logic relies on comparing the image width and height against your maximum limit. If the current width is greater than or equal to 500, you divide both dimensions by current_width / 500. Conversely, if the height exceeds the limit, you divide by current_height / 500. This ensures the resulting image never exceeds the threshold on either axis and maintains its proportions.

In pure PHP without external libraries, you can achieve this using the built-in GD extension. Here is a function that calculates the new dimensions before resizing:

function getProportionalDimensions($width, $height, $maxDimension) {
    $scale = maxDimension / min($width, $height);
    
    // Ensure we don't scale up if the image is already smaller than the max
    if ($scale > 1) {
        $scale = 1;
    }

    return [
        'new_width'  => (int) floor($width * $scale),
        'new_height' => (int) floor($height * $scale),
    ];
}

Once you have the calculated dimensions, you pass them to imagecreatetruecolor and use imagecopyresampled. Note that imagecopyresampled is preferred over imagecopyresized for production code because it provides better interpolation quality. However, a significant trade-off exists: GD does not support transparency for PNGs generated via this method unless you explicitly set the background color to match the intended output (usually transparent if using alpha blending).

For projects built on Laravel, the process is often cleaner when leveraging existing libraries rather than writing raw GD code. Packages like intervention/image handle this logic abstractly. You can configure the driver to enforce a maximum size while preserving aspect ratio:

use Intervention\Image\Facades\Image;

$image = Image::make($pathToUpload)
    ->resizeCover(500, 500); // This method resizes to fit within the box

The resizeCover method in Intervention specifically targets this use case. It scales the image so that it fits entirely within the given width and height constraints, effectively implementing the logic described above without manual calculation. If you require specific quality settings or format conversions (like converting a WebP upload to JPEG), Intervention also simplifies those steps.

One pitfall to watch out for is memory usage. Resizing large images before saving them can spike memory consumption, especially if you are processing many uploads in a single request loop. Always ensure your GD configuration limits (gd.jpeg_ignore_warning, etc.) are set appropriately for your server environment. Additionally, verify that the resulting image format matches your storage strategy; resizing a JPEG to PNG without compression might bloat file sizes unnecessarily.

If you prefer a zero-dependency solution and need fine-grained control over interpolation methods or quality parameters, stick with the GD functions but remember to validate inputs. Always check if the GD extension is installed on the target server before attempting to manipulate images, as many shared hosting environments disable it by default. For high-traffic applications where raw PHP performance is critical, consider offloading image processing to a queue worker using Laravel's built-in queue system or a dedicated service like ImageMagick via CLI commands executed asynchronously.

Our network