如何按比例调整图像大小/保持纵横比?

kob*_*obe 146 javascript jquery resize image

我的图像尺寸相当大,我想用jQuery缩小它们,同时保持比例受限,即相同的宽高比.

有人能指点我一些代码,还是解释逻辑?

Jas*_*han 403

我认为这是一个非常酷的方法:

 /**
  * Conserve aspect ratio of the original region. Useful when shrinking/enlarging
  * images to fit into a certain area.
  *
  * @param {Number} srcWidth width of source image
  * @param {Number} srcHeight height of source image
  * @param {Number} maxWidth maximum available width
  * @param {Number} maxHeight maximum available height
  * @return {Object} { width, height }
  */
function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) {

    var ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight);

    return { width: srcWidth*ratio, height: srcHeight*ratio };
 }
Run Code Online (Sandbox Code Playgroud)

  • 非常优越的答案!如果高度和宽度都较大,则正确的答案会平放在其表面上.真的,好,也很好的小胡子. (30认同)
  • 这是处理这个问题的绝佳方式!我稍微调整了一下img元素+防止放大图像:`function imgSizeFit(img,maxWidth,maxHeight){var ratio = Math.min(1,maxWidth/img.naturalWidth,maxHeight/img.naturalHeight); img.style.width = img.naturalWidth*ratio +'px'; img.style.height = img.naturalHeight*ratio +'px'; }` (3认同)
  • 该算法的优点在于它可以很容易地在其他编程语言中重用。 (2认同)

Moi*_*man 174

http://ericjuden.com/2009/07/jquery-image-resize/看一下这段代码

$(document).ready(function() {
    $('.story-small img').each(function() {
        var maxWidth = 100; // Max width for the image
        var maxHeight = 100;    // Max height for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height

        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image
        }

        // Check if current height is larger than max
        if(height > maxHeight){
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
            height = height * ratio;    // Reset height to match scaled image
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 不知道为什么需要jQuery.在客户端上按比例缩小图像可以用CSS完成,这很简单:只需将其"max-width"和"max-height"设置为"100%"即可.http://jsfiddle.net/9EQ5c/ (11认同)
  • 由于IF STATEMENT,因此无法通过CSS完成此操作.我相信重点是填写缩略图.如果图像太高,则必须是最大宽度,如果图像太宽,则必须是最大高度.如果您使用CSS max-width,max-height,您将获得带有空格的缩略图,而不是完全填充 (10认同)
  • 这可以单独使用CSS吗?(最大宽度,高度:自动等?) (4认同)

Dan*_*scu 65

如果我正确理解了这个问题,你甚至不需要jQuery.在客户机上按比例缩小图像,可以单独用CSS来实现:刚刚成立的max-width,并max-height100%.

<div style="height: 100px">
<img src="http://www.getdigital.de/images/produkte/t4/t4_css_sucks2.jpg"
    style="max-height: 100%; max-width: 100%">
</div>?
Run Code Online (Sandbox Code Playgroud)

这是小提琴:http://jsfiddle.net/9EQ5c/

  • 这比上面的答案容易得多.谢谢.顺便问一下你是如何得到"我的答案"链接向下滚动到你的帖子? (2认同)

Ric*_*ick 11

为了确定纵横比,您需要有一个目标比率.

高度

function getHeight(length, ratio) {
  var height = ((length)/(Math.sqrt((Math.pow(ratio, 2)+1))));
  return Math.round(height);
}
Run Code Online (Sandbox Code Playgroud)

宽度

function getWidth(length, ratio) {
  var width = ((length)/(Math.sqrt((1)/(Math.pow(ratio, 2)+1))));
  return Math.round(width);
}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我使用了16:10这个典型的监视器宽高比.

var ratio = (16/10);
var height = getHeight(300,ratio);
var width = getWidth(height,ratio);

console.log(height);
console.log(width);
Run Code Online (Sandbox Code Playgroud)

上面的结果将是147300


小智 7

这个问题有4个参数

  1. 当前图像宽度 iX
  2. 当前图像高度 iY
  3. 目标视口宽度cX
  4. 目标视口高度 cY

并且有3个不同的条件参数

  1. CX > CY ?
  2. iX > cX ?
  3. iY > cY ?

解决方案

  1. 找到目标视口 F 的较小边
  2. 找到当前视口 L 的较大边
  3. 求两者的因子 F/L = 因子
  4. 将当前端口两边乘以因子即fX = iX *因子;fY = iY * 因子

这就是你需要做的。

//Pseudo code


iX;//current width of image in the client
iY;//current height of image in the client
cX;//configured width
cY;//configured height
fX;//final width
fY;//final height

1. check if iX,iY,cX,cY values are >0 and all values are not empty or not junk

2. lE = iX > iY ? iX: iY; //long edge

3. if ( cX < cY )
   then
4.      factor = cX/lE;     
   else
5.      factor = cY/lE;

6. fX = iX * factor ; fY = iY * factor ; 
Run Code Online (Sandbox Code Playgroud)

这是一个成熟的论坛,我不会给你代码:)


Win*_*ker 6

实际上我刚遇到这个问题,我发现的解决方案非常简单和奇怪

$("#someimage").css({height:<some new height>})
Run Code Online (Sandbox Code Playgroud)

奇迹般地,图像被调整到新的高度并保持相同的比例!