Calculating image size ratio for resizing

98,907

Solution 1

Here's code from my personal grab bag of image resizing code. First, data you need:

list($originalWidth, $originalHeight) = getimagesize($imageFile);
$ratio = $originalWidth / $originalHeight;

Then, this algorithm fits the image into the target size as best it can, keeping the original aspect ratio, not stretching the image larger than the original:

$targetWidth = $targetHeight = min($size, max($originalWidth, $originalHeight));

if ($ratio < 1) {
    $targetWidth = $targetHeight * $ratio;
} else {
    $targetHeight = $targetWidth / $ratio;
}

$srcWidth = $originalWidth;
$srcHeight = $originalHeight;
$srcX = $srcY = 0;

This crops the image to fill the target size completely, not stretching it:

$targetWidth = $targetHeight = min($originalWidth, $originalHeight, $size);

if ($ratio < 1) {
    $srcX = 0;
    $srcY = ($originalHeight / 2) - ($originalWidth / 2);
    $srcWidth = $srcHeight = $originalWidth;
} else {
    $srcY = 0;
    $srcX = ($originalWidth / 2) - ($originalHeight / 2);
    $srcWidth = $srcHeight = $originalHeight;
}

And this does the actual resizing:

$targetImage = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($targetImage, $originalImage, 0, 0, $srcX, $srcY, $targetWidth, $targetHeight, $srcWidth, $srcHeight);

In this case the $size is just one number for both width and height (square target size). I'm sure you can modify it to use non-square targets. It should also give you an inspiration on what other resizing algorithms you can use.

Solution 2

$ratio = $originalWidth / $originalHeight

if you want to change the Height:

$targetWidth = $targetHeight * $ratio

if you want to change the Width:

$targetHeight = $targetWidth / $ratio

Solution 3

What you want is to maintain the aspect ratio of your original image. This is the ratio between the width and the height of the image. So you calculate the factor by which you have to resize the image in the vertical and horizontal direction and then you keep the higher of the two. In pseudocode:

target_height = 768
target_width = 1024
# v_fact and h_fact are the factor by which the original vertical / horizontal
# image sizes should be multiplied to get the image to your target size.
v_fact = target_height / im_height 
h_fact = target_width / im_width
# you want to resize the image by the same factor in both vertical 
# and horizontal direction, so you need to pick the correct factor from
# v_fact / h_fact so that the largest (relative to target) of the new height/width
# equals the target height/width and the smallest is lower than the target.
# this is the lowest of the two factors
im_fact = min(v_fact, h_fact)
new_height = im_height * im_fact
new_width = im_width * im_fact
image.resize(new_width, new_height)

Solution 4

this is working.

function calculateDimensions($width,$height,$maxwidth,$maxheight)
{

        if($width != $height)
        {
            if($width > $height)
            {
                $t_width = $maxwidth;
                $t_height = (($t_width * $height)/$width);
                //fix height
                if($t_height > $maxheight)
                {
                    $t_height = $maxheight;
                    $t_width = (($width * $t_height)/$height);
                }
            }
            else
            {
                $t_height = $maxheight;
                $t_width = (($width * $t_height)/$height);
                //fix width
                if($t_width > $maxwidth)
                {
                    $t_width = $maxwidth;
                    $t_height = (($t_width * $height)/$width);
                }
            }
        }
        else
            $t_width = $t_height = min($maxheight,$maxwidth);

        return array('height'=>(int)$t_height,'width'=>(int)$t_width);
    }

Solution 5

How about this:

double ratio = imageWidth/imageHeight;
int newHeight = Math.min(displayHeight, displayWidth / ratio); 
int newWidth =  Math.min(displayWidth, displayHeight * ratio); 
Share:
98,907
sunjie
Author by

sunjie

Updated on December 30, 2020

Comments

  • sunjie
    sunjie over 3 years

    I have a defined fixed width and height to resize an image. However, I have problem a with this, because the image can have any kind of size ratio (it can be vertical or the horizontal). In this case, fixed width and height cause a problem. I want to calculate width and height in a smarter way.

    For example lets say I have defined width 1024px and height 768px. And I want to resize an image which is vertical (height 1100px and width 200px). So in my case it will resize to fixed size (1024x768), so the width will be increased from 100px to 768px, and it will be very ugly. Similarly if the image has height less than 768px, it will increase the height by force to 768px.

    Therefore I would like to calculate the new image size based on the original image size ratio. Lets say if the above example image should be resized to maximum height of 768px, but then what about the width? it's already less than my "maximum width", which is 200px, so should the width remain unchanged? or should it be further decreased?

    Similarly, if the image has the height 200px, and the width 1100px. So the width should be decreased to 1024px, but what about the height?

    The third problem is that, let's suppose if both height and width are more than the maximum height and maximum width, let's say width: 1100px and height:4000px. Now since width and height both are more than the maximum width and maximum height but the image is vertical, it will make it horizontal. So how can I check if in this case if I should resize the image according to maximum height, or according to maximum width?

    I appreciate any help with this.

  • jilles de wit
    jilles de wit almost 13 years
    This only works correctly if your target size is square ($w_max=$h_max), otherwise you can get results where the new image is larger than the target size in either width or height depending on the relative sizes of the input image and target size.
  • jilles de wit
    jilles de wit almost 13 years
    For example: if input = 400x300 and target = 300x220 then your code would lead to an image of 300x225 which is larger than target.
  • Kus
    Kus about 11 years
    This is great, here is a shorter PHP version: list($width, $height, $type, $attr) = getimagesize($path); $ratio = min($maxHeight / $height, $maxWidth / $width); $newHeight = ceil($height * $ratio); $newWidth = ceil($width * $ratio);
  • Roopendra
    Roopendra about 9 years
    If there is non-square target,e.g. $expectedWidth = 200;$expectedHeight = 150; then which one in following is correct way, $targetWidth = $targetHeight = min(max($expectedWidth, $expectedHeight), max($originalWidth, $originalHeight)); OR $targetWidth = $targetHeight = min($expectedWidth, $expectedHeight, max($originalWidth, $originalHeight));
  • Heemanshu Bhalla
    Heemanshu Bhalla about 8 years
    @deceze pls clerify what could be value for $size here
  • Gromski
    Gromski about 8 years
    @Heemanshu As written, it's the target image size. $size = 200 means the result will be an image of (max) 200 x 200 pixel.
  • Heemanshu Bhalla
    Heemanshu Bhalla about 8 years
    @deceze ok thank i was using the same let me check one more time
  • Heemanshu Bhalla
    Heemanshu Bhalla about 8 years
    @deceze i am using the code referenced from your code But it not working and not generating the image you can see code below - geeksprogrammings.blogspot.in/2016/05/…
  • J Fabian Meier
    J Fabian Meier almost 8 years
    Could you please add some explanation?
  • LifeIsShort
    LifeIsShort about 6 years
    AWESOME! Thank you!