2008-09-22 141 views
31

我遇到了一些問題,將我的圖像縮放到正確預定義的大小。我想知道 - 因爲它純粹是數學,如果有某種常用的邏輯算法可以在每種語言(PHP,ActionScript,Javascript等)中按比例縮放圖像。比例圖像調整大小

我使用這個此刻:

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(寬度或高度)。這有點合理,但我想知道是否有一種按比例縮放圖像的簡單方便的方法。

回答

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

確保所有除法都是浮點數。

2

我建議不要自己編寫此代碼;有無數的像素級細節需要一段時間才能正確對待。使用ImageMagick,它是那裏最好的圖形庫。

4

黑Shikari有它。如問題中所述的解決方案失敗,因爲您不是第一個確定哪個dimenson的尺寸最大化比率更大,然後然後將這兩個尺寸減少該較大的比例。

您當前的解決方案使用串行條件分析一個潛在的維度違規,然後另一個不起作用。

還請注意,如果你想要高檔圖像,你目前的解決方案將不會飛,黑暗Shikari的將再次。

0

這是我爲我的網站開發的一個功能,您可能想要使用。它基於上面的答案。

它做的其他事情不僅圖像處理 - 請刪除所有不必要的東西

<?php 

$thumb_width = 500; 
$thumb_height = 500; 

if ($handle = opendir('to-do')) { 
    echo "Directory handle: $handle<br />"; 
    echo "Files:<br /><br />"; 

    /* This is the correct way to loop over the directory. */ 
    while (false !== ($file = readdir($handle))) { 

     if (($file != ".") && ($file != "..")){ 
      echo "$file"; 

      $original_path = "to-do/" . $file; 

      $source_image = ImageCreateFromJPEG($original_path); 
      $thumb_width = $thumb_width; 
      $thumb_height = $thumb_height; 

      // Create the image, of the required size 
      $thumbnail = imagecreatetruecolor($thumb_width, $thumb_height); 
      if($thumbnail === false) { 
       //creation failed -- probably not enough memory 
       return null; 
      } 

      // Fill the image with a white color (this will be visible in the padding around the image, 
      // if the aspect ratios of the image and the thumbnail do not match) 
      // Replace this with any color you want, or comment it out for black. 
      // I used grey for testing =) 
      $fill = imagecolorallocate($thumbnail, 255, 255, 255); 
      imagefill($thumbnail, 0, 0, $fill); 

      // Compute resize ratio 
      $hratio = $thumb_height/imagesy($source_image); 
      $wratio = $thumb_width/imagesx($source_image); 
      $ratio = min($hratio, $wratio); 

      // If the source is smaller than the thumbnail size, 
      // Don't resize -- add a margin instead 
      // (that is, dont magnify images) 
      if ($ratio > 1.0) 
       $ratio = 1.0; 

      // Compute sizes 
      $sy = floor(imagesy($source_image) * $ratio); 
      $sx = floor(imagesx($source_image) * $ratio); 

      // Compute margins 
      // Using these margins centers the image in the thumbnail. 
      // If you always want the image to the top left, set both of these to 0 
      $m_y = floor(($thumb_height - $sy)/2); 
      $m_x = floor(($thumb_width - $sx)/2); 

      // Copy the image data, and resample 
      // If you want a fast and ugly thumbnail, replace imagecopyresampled with imagecopyresized 
      if (!imagecopyresampled($thumbnail, $source_image, 
       $m_x, $m_y, //dest x, y (margins) 
       0, 0, //src x, y (0,0 means top left) 
       $sx, $sy,//dest w, h (resample to this size (computed above) 
       imagesx($source_image), imagesy($source_image)) //src w, h (the full size of the original) 
      ) { 
       //copy failed 
       imagedestroy($thumbnail); 
       return null; 
      } 

      /* Set the new file name */ 
      $thumbnail_file_name = $file; 

      /* Apply changes on the original image and write the result on the disk */ 
      ImageJPEG($thumbnail, $complete_path . "done/" . $thumbnail_file_name); 
      unset($source_image); 
      unset($thumbnail); 
      unset($original_path); 
      unset($targeted_image_size); 

      echo " done<br />"; 

     } 

    } 

    closedir($handle); 
} 

?> 
+0

所以請記住,它必須清潔,有可移動部分的負載。 – 2011-10-12 20:49:47

0

以及我在此功能是按比例的比例,它使用給定的寬度,高度,和任選的最大寬度/高度ü希望(依賴於給定的寬度和高度)

function scaleProportional($img_w,$img_h,$max=50) 
    { 
     $w = 0; 
     $h = 0; 

     $img_w > $img_h ? $w = $img_w/$img_h : $w = 1; 
     $img_h > $img_w ? $h = $img_h/$img_w : $h = 1; 

     $ws = $w > $h ? $ws = ($w/$w) * $max : $ws = (1/$h) * $max; 
     $hs = $h > $w ? $hs = ($h/$h) * $max : $hs = (1/$w) * $max; 

     return array(
      'width'=>$ws, 
      'height'=>$hs 
     ); 
    } 

用法:

  $getScale = scaleProportional(600,200,500); 
      $targ_w = $getScale['width']; //returns 500 
      $targ_h = $getScale['height']; //returns 16,6666667 
1

這是我如何做到這一點:

+ (NSSize) scaleHeight:(NSSize)origSize 
      newHeight:(CGFloat)height { 

    NSSize newSize = NSZeroSize; 
    if (origSize.height == 0) return newSize; 

    newSize.height = height; 
    CGFloat factor = (height/origSize.height); 
    newSize.width = (origSize.width * factor); 

    return newSize; 
} 

+ (NSSize) scaleWidth:(NSSize)origSize 
      newWidth:(CGFloat)width { 

    NSSize newSize = NSZeroSize; 
    if (origSize.width == 0) return newSize; 

    newSize.width = width; 
    CGFloat factor = (width/origSize.width); 
    newSize.height = (origSize.height * factor); 

    return newSize; 
}