Jump to content

calculating new size for images


wendu

Recommended Posts

hey

 

I have a PHP script that can reduce the size of an uploaded image to a percent value, but I would like a way to calculate new values for an image of any size that are at or below 1280 x 960.

 

if I use percent values for both the width and the height, it only works on images that are of specific size. sometimes the images are bigger and thus the percent values produce a bigger image than what I said above.

 

thank you all, big hugs! - wendu

 

edit: to be more clear, I want my script to reduce an image's size to 1280x960 or smaller, but maintaining the aspect ratio of the image, so both vertical and horizontal images would work well..

Link to comment
https://forums.phpfreaks.com/topic/172733-calculating-new-size-for-images/
Share on other sites

Not entirely sure but I think this may work:

 

$maxWidth = 800;
$maxHeight = 600;
list($width, $height) = getimagesize('path/to/image');

$ratio = 1;
if ($maxWidth < $width) {
    $ratio = $maxWidth / $width;
} else if ($maxHeight < $height) {
    $ratio = $maxHeight / $height;
}

$newWidth = $width * $ratio;
$newHeight = $height * $ratio;

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.