php – 调整图像大小并保持宽高比以适合iPhone的算法
发布时间:2020-12-13 13:42:31 所属栏目:PHP教程 来源:网络整理
导读:我正在为iPhone应用程序创建一个Web服务来进行交互. 当我的客户端在服务器端上传图像时,我希望我的php脚本调整图像的大小,同时保持宽高比,以便它适合iPhone屏幕. (即最长边为 = 960而最短的 = 640 我在JS中创建了一个模型,只是因为我发现它更容易做得很快.
我正在为iPhone应用程序创建一个Web服务来进行交互.
当我的客户端在服务器端上传图像时,我希望我的php脚本调整图像的大小,同时保持宽高比,以便它适合iPhone屏幕. (即最长边为< = 960而最短的< = 640 我在JS中创建了一个模型,只是因为我发现它更容易做得很快. 我很确定,尽管我可能错了,但这不是最有效的方式.有人可以用更好的逻辑(特别是开始时的位置)纠正我,还是更接近数学的方法? 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); } } }
我认为以下几点应该给你这个想法.它不是任何特定的语言,而是一个类似C的伪代码.
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. } 这个代码的效率或者你所拥有的效率不会是一个问题.实际调整图像大小所需的时间将缩短进行两次比较,两次除法和两次乘法所需的时间. 这是一个“更数学”的方法吗?我想,因为它把你的四个案件分解为两个.但是这种做法基本上是一样的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |