2009-11-15 31 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; 
}