# Calculating image size ratio for resizing

Viewed   56 times

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.

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.

Monday, October 17, 2022
4

You can do it in PHP without any problem, simulating threads instead of using them directly. Actually, PHP doesn't have native threads (you can eventuall¥ use libraries but that's not very useful in your case).

``````static::Crop(\$file,\$destination,\$tn_w = 300,\$tn_h =200,\$quality = 100,\$wmsource = false);
``````

Why not doing :

``````\$array = array(\$file, \$destination, \$tn_w = 300, \$tn_h = 200, \$quality = 100, \$wmsource = 0);
\$command = "/usr/bin/php crop.php";
foreach (\$array as \$arg)
{
\$command .= ' ' . escapeshellarg(\$arg);
}
exec("\$command &"); // note the & which release your execution
usleep(100000);
``````

And you put your cropping function inside `crop.php`, and then call it like :

``````list(\$exec, \$file, \$destination, \$tn_w, \$tn_h, \$quality, \$wmsource) = \$argv;
static::Crop(\$file,\$destination,\$tn_w = 300,\$tn_h =200,\$quality = 100,\$wmsource = false);
``````

This will do the job.

You can also simulate mutexes using a file if you want to avoid usleep and control how many crops are running at once, that's really up to you. You definitely can do such work in PHP.

Wednesday, August 10, 2022
5

I doubt that your `/assets` directory is located on the root of the file-system. It is probably in the root of the web-server, so you could use something like:

``````\$pic2Path = \$_SERVER['DOCUMENT_ROOT'] . '/assets/images/reviews/' . \$_FILES['TheImage'];
``````
Saturday, December 10, 2022
1

use directly https://romannurik.github.io/AndroidAssetStudio/

For example, two devices that both report a screen size of normal might have actual screen sizes and aspect ratios that are slightly different when measured by hand. Similarly, two devices that report a screen density of hdpi might have real pixel densities that are slightly different. Android makes these differences abstract to applications, so you can provide UI designed for the generalized sizes and densities and let the system handle any final adjustments as necessary

You have to create different dimen , layout , images and icon files to support all devices.

changes in screen density.

``````xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
``````

Make this layout files, so that it will be same for all devices.

Give padding ,margin ,font and all properties as per devices.

``````res/layout/main_activity.xml           # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml   # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml   # For 10” tablets (720dp wide and bigger)
``````

For Layout ,

``````res/layout/my_layout.xml              // layout for normal screen size ("default")
res/layout-large/my_layout.xml        // layout for large screen size
res/layout-xlarge/my_layout.xml       // layout for extra-large screen size
res/layout-xlarge-land/my_layout.xml  // layout for extra-large in landscape orientation
``````

For Images

``````res/drawable-mdpi/graphic.png         // bitmap for medium-density
res/drawable-hdpi/graphic.png         // bitmap for high-density
res/drawable-xhdpi/graphic.png        // bitmap for extra-high-density
res/drawable-xxhdpi/graphic.png       // bitmap for extra-extra-high-density
``````

For Icon

``````res/mipmap-mdpi/my_icon.png         // launcher icon for medium-density
res/mipmap-hdpi/my_icon.png         // launcher icon for high-density
res/mipmap-xhdpi/my_icon.png        // launcher icon for extra-high-density
res/mipmap-xxhdpi/my_icon.png       // launcher icon for extra-extra-high-density
res/mipmap-xxxhdpi/my_icon.png      // launcher icon for extra-extra-extra-high-density
``````

For Launcher icon

``````36x36 (0.75x) for low-density
48x48 (1.0x baseline) for medium-density
72x72 (1.5x) for high-density
96x96 (2.0x) for extra-high-density
180x180 (3.0x) for extra-extra-high-density
192x192 (4.0x) for extra-extra-extra-high-density (launcher icon only; see note above)
``````

Checkout Dimension and Supporting Multiple Screens Official Documentaion.

Wednesday, August 17, 2022
4

Well thank you @Erick Robertson Changed a litte bit, but now it works!

Here is the changed code:

``````if (bwidth / swidth <  bheight / sheight) {
new_width = swidth;
new_height = (int) Math.floor((double) bheight
* (double) swidth / (double) bwidth);
} else {
new_height = sheight;
new_width = (int) Math.floor((double) bwidth
* (double) sheight / (double) bheight);
}
``````
Wednesday, December 7, 2022