我创建了一个iPhone应用程序的Web服务进行交互。
在我的客户端上传图片服务器端,我想我的PHP脚本来调整图像大小,,同时保持纵横比,使之能够适合iPhone屏幕。 (即最长的边为< = 960和最短&其中; = 640
我已经创建了一个实体模型的JS,只是因为我觉得它更容易做快。
我是pretty的肯定,但我可能是错的,这是不这样做的最有效的方式。可能有人纠正我,或者更好的逻辑(尤其是位处开始),或接近这个更数学的方法吗?
变种W = 960,H = 960,new_w,new_h;
如果(并且R w =的H&&安培;并且R w 960 || h取代; = W&安培;&安培h取代; 960 ||并且R w =的H&&安培h取代; 640 || h取代; =瓦特&安培;&安培;并且R w 640){
如果(并且R w; H){
如果(并且R w 960){
new_w = 960;
new_h = H *(new_w / W);
}
如果(h取代; 640){
new_h = 640;
new_w = W *(new_h /小时);
}
}
其他 {
如果(h取代; 960){
new_h = 960;
new_w = W *(new_h /小时);
}
如果(并且R w 640){
new_w = 640;
new_h = H *(new_w / W);
}
}
}
解决方案
我觉得下面的应该给你的想法。它不以任何特定的语言,而是一种类似C的伪code。
shortSideMax = 640;
longSideMax = 960;
功能调整大小(图)
{
如果(image.width> = image.height)
{
如果(image.width< = longSideMax和放大器;&安培; image.height< = shortSideMax)
返回形象; //不需要调整大小
wRatio = longSideMax / image.width;
hRatio = shortSideMax / image.height;
}
其他
{
如果(image.height< = longSideMax和放大器;&安培; image.width< = shortSideMax)
返回形象; //不需要调整大小
wRatio = shortSideMax / image.width;
hRatio = longSideMax / image.height;
}
// hRatio和wRatio现在有调节因子的高度和宽度。
//你想要的最小的两个,以确保所得到的图像
//配合在所需的帧并保持高宽比。
resizeRatio =最小值(wRatio,hRatio);
newHeight = image.Height * resizeRatio;
newWidth = image.Width * resizeRatio;
//现在调用函数来调整原来的图像[newWidth,newHeight]
//并返回结果。
}
这code的效率,或者你有什么,不会是一个问题。所花费的时间实际上调整图像大小将大大超过它需要做两比较,二分裂和两个乘法的时间。
这是一个更数学的方式来做到这一点?我想,在它缩短了您的4箱子一分为二。但是这种方法是基本相同的。
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);
}
}
}
解决方案
I think the following should give you the idea. It's not in any particular language, but rather a C-like pseudo code.
shortSideMax = 640;
longSideMax = 960;
function Resize(image)
{
if (image.width >= image.height)
{
if (image.width <= longSideMax && image.height <= shortSideMax)
return image; // no resizing required
wRatio = longSideMax / image.width;
hRatio = shortSideMax / image.height;
}
else
{
if (image.height <= longSideMax && image.width <= shortSideMax)
return image; // no resizing required
wRatio = shortSideMax / image.width;
hRatio = longSideMax / image.height;
}
// hRatio and wRatio now have the scaling factors for height and width.
// You want the smallest of the two to ensure that the resulting image
// fits in the desired frame and maintains the aspect ratio.
resizeRatio = Min(wRatio, hRatio);
newHeight = image.Height * resizeRatio;
newWidth = image.Width * resizeRatio;
// Now call function to resize original image to [newWidth, newHeight]
// and return the result.
}
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.