Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating image size ratio for resizing

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.

like image 839
sunjie Avatar asked Jul 06 '11 09:07

sunjie


People also ask

How do you find the ratio of an image size?

Calculate the relationship between the width (the first number) and the height (the second number). For example: Images that are 1,600 pixels x 900 pixels or 3,200 pixels x 1,800 pixels are in the 16:9 aspect ratio. Images that are 1,600 pixels x 1,600 pixels or 3,200 pixels x 3,200 pixels are in the 1:1 aspect ratio.

How do you calculate resizing?

Applying the aspect ratio calculator is very easy. You need to divide the original height by the original width and then multiply this number by the new width to get the new height.

How do you keep a ratio when resizing?

Press-and-hold the Shift key, grab a corner point, and drag inward to resize the selection area. Because you're holding the Shift key as you scale, the aspect ratio (the same ratio as your original photo) remains exactly the same.

How do I resize an image in a ratio?

Click Upload an image and select the image you want to crop. Under step 2, click the Fixed Aspect Ratio button, then enter that ratio, such as 5 and 2, and click Change. Drag a rectangle over the image to select the area you want.


2 Answers

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.

like image 134
deceze Avatar answered Oct 11 '22 09:10

deceze


$ratio = $originalWidth / $originalHeight 

if you want to change the Height:

$targetWidth = $targetHeight * $ratio 

if you want to change the Width:

$targetHeight = $targetWidth / $ratio 
like image 28
Mehran - Persian Avatar answered Oct 11 '22 07:10

Mehran - Persian