比例图像调整 [英] Proportional image resize

查看:106
本文介绍了比例图像调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将图像缩放到适当的预定义大小时遇到​​一点问题.我想知道-因为它纯粹是数学,所以是否存在某种通用逻辑算法可以在每种语言(PHP,ActionScript,Javascript等)中使用,以按比例缩放图像.

I'm having a little bit of a problem scaling my images to a properly predefined size. I was wondering - since it is purely mathematics, if there's some sort of common logical algorithm that works in every language (PHP, ActionScript, Javascript etc.) to scale images proportionally.

我目前正在使用它:

var maxHeight   = 300;
var maxWidth    = 300;

var ratio:Number    =   height / width;

if (height > maxHeight) {
    height = maxHeight;
    width = Math.round(height / ratio);
} 

else if(width > maxWidth) {
    width = maxWidth;
    height = Math.round(width * ratio);
}

但是它不能正常工作.图像会按比例缩放,这是足够确定的,但是大小未设置为300(无论是宽度还是高度).有点道理,但是我想知道是否有一种简单有效的方法来按比例缩放图像.

But it doesn't work properly. The images scales proportionately, sure enough, but the size isn't set at 300 (either in width or in height). It kind of makes sense, but I was wondering if there's a fool-proof, easy way to scale images proportionally.

推荐答案

ratio = MIN( maxWidth / width, maxHeight/ height );
width = ratio * width;
height = ratio * height;

确保所有分隔线均为浮点数.

Make sure all divides are floating-point.

这篇关于比例图像调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆