I'm creating a web service for an iPhone app to interact with.
When my client uploads images server-side, I want my php script to resize the image, whilst maintaining the aspect ratio, so that it will fit onto the iPhone screen. (i.e. the longest side is <= 960 and the shortest <= 640
I've created a mock-up in JS, simply because I find it easier to do quickly.
I'm pretty sure, though I may be wrong, that this isn't the most efficient way of doing it. Could someone correct me with either better logic (especially the bit at the start), or a more mathematical way of approaching this?
var w = 960, h = 960, new_w, new_h;
if (w >= h && w > 960 || h >= w && h > 960 || w >= h && h > 640 || h >= w && w > 640) {
if (w > h) {
if (w>960) {
new_w = 960;
new_h = h*(new_w/w);
}
if (h>640) {
new_h = 640;
new_w = w*(new_h/h);
}
}
else {
if (h>960) {
new_h = 960;
new_w = w*(new_h/h);
}
if (w>640) {
new_w = 640;
new_h = h*(new_w/w);
}
}
}
Maybe a slightly shorter routine would be:
Obviously if the ratio is > 1, then it's enlarging, if < 1 then it's shrinking.
Similar to DevProd's answer, but I struggled to follow it myself because of the naming convention. Hopefully this is a little clearer:
Below, the simplest way I know to keep proportions. Hope it helps.
Javascript
PHP
Any one coming to this page from Google looking for ASPECT FILL not ASPECT FIT, it is simply a matter of switching the ratio selection code i.e:
Jim's answer:
becomes
DevProd's answer:
becomes
I think the following should give you the idea. It's not in any particular language, but rather a C-like pseudo code.
The efficiency of this code, or what you have, won't be an issue. The time it takes to actually resize the image will dwarf the time it takes to do a couple of comparisons, two divides, and two multiplies.
Is this a "more mathematical" way to do it? I suppose, in that it collapses your four cases into two. But the approach is essentially the same.