Jump to content

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;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.