Jquery 调整图像大小

Jquery 调整图像大小,jquery,image,resize,aspect-ratio,Jquery,Image,Resize,Aspect Ratio,下面的代码非常适合按高宽比调整图像大小,我还可以按宽度创建单独的函数。但我并不总是确定图像是否需要按高度或宽度缩小 例如,如果需要调整图像大小的空间为100宽100高,而图像为150 x 90,则其宽度将需要缩小。如果图像是80乘160,则需要缩小高度 所以我想问的是,如何修改下面的代码,使其按纵横比缩小图像,以适应宽度和高度的参数?谢谢 jQuery.fn.resizeHeightMaintainRatio = function(newHeight){ if (this.aspectRatio

下面的代码非常适合按高宽比调整图像大小,我还可以按宽度创建单独的函数。但我并不总是确定图像是否需要按高度或宽度缩小

例如,如果需要调整图像大小的空间为100宽100高,而图像为150 x 90,则其宽度将需要缩小。如果图像是80乘160,则需要缩小高度

所以我想问的是,如何修改下面的代码,使其按纵横比缩小图像,以适应宽度和高度的参数?谢谢

jQuery.fn.resizeHeightMaintainRatio = function(newHeight){
if (this.aspectRatio == undefined) 
this.aspectRatio = parseInt($(this).width() / $(this).height());
$(this).height(newHeight); 
$(this).width(newHeight * this.aspectRatio);
}
我再次编辑了代码,因为经过进一步检查,我的更新版本和Dan的似乎都不能正常工作。纵横比丢失了,所以这里还有一个。我在一张图片上试过,到目前为止效果不错。在这里

        jQuery.fn.resizeMaintainRatio = function(newHeight, newWidth){
            widthRatio = parseInt($(this).width() / newWidth);
            heightRatio = parseInt($(this).height() / newHeight);

            if(heightRatio > widthRatio)
            {

                this.aspectRatio = parseInt($(this).css("width") / $(this).css("height"));

                $(this).css("height", newHeight);
                $(this).css("width", newHeight * this.aspectRatio);

            }
            else{

                this.aspectRatio = parseInt($(this).css("height") / $(this).css("width"));

                $(this).css("width", newWidth);
                $(this).css("height", newWidth * this.aspectRatio);

            }

        }
再次注意:在进一步使用上述代码后,请尝试以下操作-

您必须检查
$(此).width()>$(此).height()

如果为true,则调用该代码的宽度版本,否则调用确切版本。

以下代码将计算出需要缩放的边(适用于非方形框,仅检查宽度与高度不起作用),并根据该边进行缩放

如果图像小于newWidth*newHeight框,它也会放大图像。如果你不想放大,在我切换比例的地方,检查宽度或高度是否大于1,然后才进行缩放

免责声明:代码尚未运行,但概念应该是正确的

编辑使用OP的修复更新

jQuery.fn.resizeHeightMaintainRatio = function(newHeight, newWidth){
   widthRatio = parseInt($(this).width() / newWidth);
   heightRatio = parseInt($(this).height() / newHeight);
   if(widthRatio < 1 && heightRatio < 1){
      widthRatio = heightRatio;
      heightRatio = widthRatio;
   }
   if(widthRatio > heightRatio){
      $(this).width(newWidth); 
      $(this).height(newHeight / widthRatio);
   } else
   {
      $(this).height(newHeight); 
      $(this).width(newWidth / heightRatio);
   }
}
jQuery.fn.resizeHeightMaintaintRatio=函数(newHeight,newWidth){
widthRatio=parseInt($(this).width()/newWidth);
heightRatio=parseInt($(this).height()/newHeight);
if(宽高比<1和高高比<1){
宽度比=高度比;
高度比=宽度比;
}
如果(宽度比>高度比){
$(this).width(newWidth);
$(此).高度(新高度/宽度比);
}否则
{
$(此).height(新高度);
$(此).width(newWidth/heightRatio);
}
}

复制品和贴纸的数量是无法计算的,嗯!我也想知道这一点,我所看到的只是无数缩放宽度或高度的例子。。谁会想要另一个满溢

  • 调整宽度和高度,无需循环
  • 不超过图像的原始尺寸
  • 使用正确的数学方法,即宽度/纵横比表示高度,高度*纵横比表示宽度,因此图像实际上可以正确地上下缩放:/
应该足够直接,可以转换为javascript或其他语言

//////////////

private void ResizeImage(Image img, double maxWidth, double maxHeight)
{
    double srcWidth = img.Source.Width;
    double srcHeight = img.Source.Height;

    double resizeWidth = srcWidth;
    double resizeHeight = srcHeight;

    double aspect = resizeWidth / resizeHeight;

    if (resizeWidth > maxWidth)
    {
        resizeWidth = maxWidth;
        resizeHeight = resizeWidth / aspect;
    }
    if (resizeHeight > maxHeight)
    {
        aspect = resizeWidth / resizeHeight;
        resizeHeight = maxHeight;
        resizeWidth = resizeHeight * aspect;
    }

    img.Width = resizeWidth;
    img.Height = resizeHeight;
}

现在就要解决这个问题:永远不要在为你妻子做事的时候尝试编写代码。它不起作用:)别忘了删除这一行:if(this.aspectRatio==未定义)