Jump to content

Resizing an image without losing aspect ratio


Recommended Posts

Hey, I know how to use the PHP GD functions but how would I resize an image > 1000 width to 1000 width and just resizing the height to w/e needed to keep aspect ratio if the image is set to 1000 width in pixels of course. I don't mess with images much so idk the whole ratio thing.

It's simple math, and has little to do with 'images'

 

If you want the set width to always be 300

 

<?php

// Source is a 12MP picture
$source_width = 4000;
$source_height = 3000;

$target_width = 300;

$ratio = $target_width / $source_width;

echo "Our ratio is: $ratio<br>";

$final_width = round($source_width * $ratio);
$final_height = round($source_height * $ratio);

echo "Our final image will be $final_width x $final_height";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.