A couple of times I have had to deal with images in my code. Recently I had a project that needed the user to upload an image, which then needed to be scaled down to fit an area. Below I detail the calculations that I used.

For this demonstration, we will be using an image with a pixel size of 300 x 320. We want to scale it down to an image size of 155 x 200. This size would allow us to scale an image for a regular passport image of 35mm x 45mm.

The first thing you need to do is work out which side will be scaled down the most. Use the following to work out the percentage of the scale.

Now we can see that the Height is bigger than the width percentage-wise.

We do not want any white spaces, we are happy to crop off unwanted parts of the image, so we will scale down using the height, using the below formula:

If we wanted the whole image to fit into our assigned area, we would scale down using the width instead, because the percentage was smaller.

Hopefully, this will allow you to incorporate this into your calculations if your current programming language doesn’t allow for it.

I used this in Imagik in PHP which has a Scale Image function which was useful.

Other useful tools on the internet when understanding scale and percentages can be found on this Math.com page. Andrew Hedges also has a good Online Aspect Ratio Calculator if you just need it for a one-off.