2009-11-15 195 views
2

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

例如,如果图像需要调整大小的空间是100宽度和100高度,图像是150乘以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); 
} 

我编辑的代码再次,由于在进一步的检查既不是我更新的版本也不丹的似乎正常工作。宽高比失去了,所以这里又是另一个。我已经在一张图片上尝试过,并且非常棒。这是,

 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); 

      } 

     } 

注意AGAIN:经过进一步的使用上面的代码工作很奇怪,试试这个,而不是 - http://plugins.jquery.com/project/kepresize

+0

不要忘记删除这一行:* if(this.aspectRatio == undefined)* – 2009-11-15 21:21:20

回答

3

下面的代码将确定哪一边需要缩放(适用于非方形框,它只是简单地检查宽度与高度不适用),并根据此进行缩放。

如果它小于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); 
    } 
} 
+0

我编辑了错误,但仍然无效。 – usertest 2009-11-15 18:32:35

+0

即将修复:永远不要在为妻子做事情时尝试编码。它不工作:) – 2009-11-15 19:13:02

+0

没关系,我看到你做到了。 :)无论如何,我会更新 – 2009-11-15 19:13:53

1

你不得不检查$(this).width() > $(this).height() 为真时,调用宽度 - 该代码的版本,否则请致电该确切版本。

0

有没有会计副本和贴纸的数量呃!我也想知道这一点,我看到的所有缩放宽度或高度都是无穷无尽的例子。谁会希望其他的溢出?!对于宽度,所以无论图像

  • 调整大小的宽度和高度,而不需要为一个循环
  • 不超过图像原始尺寸
  • 用途可以正常工作,即宽度/方面的高度的数学和高度*方面实际上适当放大和缩小:/

应该是直线前进足够的转换为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; 
}