# Resizing Images

Quite often, in programming and dealing with images, it becomes necessary to resize the image without distorting its aspect ratio. Since the width and height of an image is a ratio, we can use the fundamental rule of proportions to resize the image.

This is a basic proportion:

${\frac{i_w}{i_h} = \frac{t_w}{t_h}}$

Where ${i_w}$ is the original image width, ${i_h}$ is the original images height, ${t_w}$ is the new width, and ${t_h}$ is the new height. Since decreasing or increasing the values directly affect the other, this is a direct proportion, as written above.

The fundamental rule of proportions says that we can cross-multiply a proportion.

${i_w \times t_h = i_h \times t_w}$

This allows us to create algebraic equations for solving problems.

Example: If ${t_w}$ is unknown. After cross-multiplying we need to move ${i_h}$ to the other side of the formula, so we can get ${t_w}$ by itself. We do this by dividing both sides by ${i_h}$.

${\frac{i_w \times t_h}{i_h} = \frac{i_h \times t_w}{i_h}}$

The ${i_h}$(s) on the right side of our formula cancel giving us:

${\frac{i_w \times t_h}{i_h} = t_w}$

Now, let’s work it out the other way around and find ${t_h}$. again, our cross-multiply formula:

${i_w \times t_h = i_h \times t_w}$

We need to get ${t_h}$ on one side by itself, so this time we will divide both sides by ${i_w}$:

${\frac{i_w \times t_h}{i_w} = \frac{i_h \times t_w}{i_w}}$

The ${i_w}$(s) on the left side cancel, leaving us with:

${ t_h = \frac{i_h \times t_w}{i_w} }$

So, our formulas for calculating a new width or height for an image while maintaining the aspect ratio is

When the width (${t_w}$) is known and the height (${t_h}$) is unknown:

${ t_h = \frac{i_h \times t_w}{i_w} }$

And when the height (${t_w}$) is known and the width (${t_w}$) is unknown:

${ t_w = \frac{i_w \times t_h}{i_h} }$

NOTE: Keep in mind that the formula will not always give you a whole number, and you will need to round to the nearest whole number.