2009-04-14 256 views
8

我需要將圖片大小調整爲固定大小。但它必須保持寬度和高度之間的因素。調整大小/裁剪/將圖片填充到固定大小

說我要調整圖片大小從238 (w) X 182 (h)210/150

我現在要做的是:

Original width/target width = 1.333333 
Original Height/target Height = 1.213333 

現在我佔用最小的因素。

現在我總是有正確的寬度,因爲238/1.333333 = 210。 但高度仍然爲160

如何在不損壞圖片的情況下將高度降至160

我需要裁剪嗎?如果是這樣如何?

回答

22

該解決方案與Can BerkGüder的基本相同,但花了一些時間寫作和評論之後,我覺得自己喜歡張貼。

此功能創建的縮略圖與您提供的尺寸完全一樣大。 圖像大小調整爲最適合縮略圖的大小。如果它不適合兩個方向,則它以thumnail爲中心。廣泛的評論解釋了這些事情。

function thumbnail_box($img, $box_w, $box_h) { 
    //create the image, of the required size 
    $new = imagecreatetruecolor($box_w, $box_h); 
    if($new === false) { 
     //creation failed -- probably not enough memory 
     return null; 
    } 


    //Fill the image with a light grey 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($new, 200, 200, 205); 
    imagefill($new, 0, 0, $fill); 

    //compute resize ratio 
    $hratio = $box_h/imagesy($img); 
    $wratio = $box_w/imagesx($img); 
    $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($img) * $ratio); 
    $sx = floor(imagesx($img) * $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(($box_h - $sy)/2); 
    $m_x = floor(($box_w - $sx)/2); 

    //Copy the image data, and resample 
    // 
    //If you want a fast and ugly thumbnail, 
    //replace imagecopyresampled with imagecopyresized 
    if(!imagecopyresampled($new, $img, 
     $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($img), imagesy($img)) //src w, h (the full size of the original) 
    ) { 
     //copy failed 
     imagedestroy($new); 
     return null; 
    } 
    //copy successful 
    return $new; 
} 

用法示例:

$i = imagecreatefromjpeg("img.jpg"); 
$thumb = thumbnail_box($i, 210, 150); 
imagedestroy($i); 

if(is_null($thumb)) { 
    /* image creation or copying failed */ 
    header('HTTP/1.1 500 Internal Server Error'); 
    exit(); 
} 
header('Content-Type: image/jpeg'); 
imagejpeg($thumb); 
2

您有Imagick?如果是這樣,你可以加載它的圖像,並做一些像thumbnailimage()

在那裏你可以跳過任何參數(高度或寬度),它會正確調整大小。

+0

以及我更喜歡使用GD因爲那是我在用的一切。不知道我是否有imagemagick – sanders 2009-04-14 11:24:25

+0

下面是imagick php中的一個例子: http:// arturito。net/2011/01/08/imagemagick-php-image-resizing-imagick-with-canvas/ – 2011-01-10 08:18:48

0

你必須作物5 PX關閉頂部和底部到你的目標的大小,但是這可能會破壞畫面。

真的,你應該有一個目標的寬度或高度,然後調整其他維度相同的比例。

+0

是的,然後高度或寬度可能不會響應其所需的值 – sanders 2009-04-14 11:33:53

+0

具有特定圖像大小不是一個好主意,當你沒有完全控制圖像的來源。你只能將它們縮放以適應。 – cjk 2009-04-14 11:48:33

10

這不會裁剪照片,但如果有必要,會在新圖像周圍留下空間,在創建縮略圖時我認爲這是更好的方法(而不是裁剪)。

$w = 210; 
$h = 150; 

$orig_w = imagesx($original); 
$orig_h = imagesy($original); 

$w_ratio = $orig_w/$w; 
$h_ratio = $orig_h/$h; 

$ratio = $w_ratio > $h_ratio ? $w_ratio : $h_ratio; 

$dst_w = $orig_w/$ratio; 
$dst_h = $orig_h/$ratio; 
$dst_x = ($w - $dst_w)/2; 
$dst_y = ($h - $dst_h)/2; 

$thumbnail = imagecreatetruecolor($w, $h); 

imagecopyresampled($thumbnail, $original, $dst_x, $dst_y, 
        0, 0, $dst_w, $dst_h, $orig_w, $orig_h); 
+0

你有沒有測試過,因爲在我的例子中,heigth仍然是160像素,而不是150,因爲它應該是 – sanders 2009-04-14 11:43:18

1

我寧願調整大小,以便圖像包含在您的限制內,然後填寫空白部分。所以在上面的例子中,你將調整大小,使高度正常,然後用背景色填充(每個末端7像素,我認爲)左右。

1

調整來自PHP源網頁內的圖像大小可能會有問題。較大的圖像(在磁盤上接近2 + MB)可能非常大,以至於需要超過32MB的內存才能處理。

出於這個原因,我傾向於從基於CLI的腳本開始,它具有高達128MB的可用內存,或者標準命令行,它也儘可能多地使用它。

# where to put the original file/image. It gets resized back 
# it was originally found (current directory) 
SAFE=/home/website/PHOTOS/originals 
# no more than 640x640 when finished, and always proportional 
MAXSIZE=640 
# the larger image is in /home/website/PHOTOS/, moved to .../originals 
# and the resized image back to the parent dir. 
cd $SAFE/.. && mv "$1" "$SAFE/$1" && \ 
    convert "$SAFE/$1" -resize $MAXSIZE\x$MAXSIZE\> "$1" 

'convert'是ImageMagick命令行工具的一部分。

1

是這些縮略圖嗎?如果是的話,種植不是一個大問題。我們一直這樣做。如果它看起來不錯,我甚至不會迴避將任意比例裁剪成殘缺的二次縮略圖,完全搞亂圖像(是的,我是,硬核)。這是設計師對技術問題的回答,但仍然如此。不要害怕莊稼!

1

我覺得有點混亂.. 如果你只是想調整它,保持原有比例,正確的操作方法是:

$ratio = $originalWidth/$originalHeight; 
if(//you start from the width, and want to find the height){ 
$newWidth = $x; 
$newHeight = $x/$ratio; 
}else if(//you start from the height, and want to find the width){ 
$newHeight = $x; 
$newWidth = $x * $ratio; 
} 

否則,如果前綴newWidth和newHeight不能被改變,和拇指比從原來的比例是不同的,唯一的辦法是裁剪或添加邊框的拇指。

如果傻冒上採取的剪裁方式,這個功能可以幫助你(我幾年前寫了5分鐘,也許需要一些改進。它僅適用於JPG格式,例如;):

function thumb_cut($nomeimage, $source_path, $destination_path, $new_width, $new_height){ 
     list($width, $height, $type, $attr) = getimagesize($source_path.$nomeimage); 
     if($type == 2){ 
     if($width > $new_width){ 
      $new_width = $width; 
      $new_height = $height; 
     } 
     $compression = 100; 
     $destimg = imagecreatetruecolor($new_width,$new_height) or die("Problems creating the image"); 
     $srcimg = ImageCreateFromJPEG($source_path.$nomeimage) or die("problem opening the image"); 
     $w = ImageSX($srcimg); 
     $h = ImageSY($srcimg); 
     $ro = $new_width/$new_height; 
     $ri = $w/$h; 
     if($ro<$ri){ 
      $par = "h"; 
     }else{ 
      $par = "w"; 
     } 
     if($par == "h"){ 
      $ih = $h; 
      $conv = $new_width/$new_height; 
      $iw = $conv*$ih; 
      $cw = ($w/2)-($iw/2); 
      $ch = ($h/2)-($ih/2); 
     }else if($par == "w"){ 
      $iw = $w; 
      $conv = $new_height/$new_width; 
      $ih = $conv*$iw; 
      $cw = ($w/2)-($iw/2); 
      $ch = ($h/2)-($ih/2); 
     } 
     ImageCopyResampled($destimg,$srcimg,0,0,$cw,$ch,$new_width,$new_height,$iw,$ih) or die("problems with resize"); 
     ImageJPEG($destimg,$destination_path.$nomeimage,$compression) or die("problems with storing new image"); 
     } 
    } 
2

剛剛從大圖像的高品質快速生成縮略圖提示:(from the php.net site

如果你做的縮略圖生成在階段:

  1. 從原始圖像到中間圖像具有兩倍最終尺寸,使用快速使用高品質的再取樣

那麼這可以更快調整

  • 從中間圖像到最終縮略圖;步驟1中的調整大小對於其大小而言質量相對較差,但具有足夠的額外分辨率,因此在步驟2中質量很好,並且中間圖像足夠小以至於高質量重採樣(其在2:1調整大小時很好地工作)收益非常快。

  • 1

    的技術是:

    1. 調整圖像的大小,使得一個維度,而另一個超過所需尺寸
    2. 從調整大小後的圖像的中心取出所需尺寸的圖像相匹配。

    最後,如果你不明白的是如何做調整大小的數學,要記住,如果源和目標圖像的比例是一樣的,這關係成立:

    SourceWidth/SourceHeight = DestinationWidth/DestinationHeight 
    

    如果你知道三個參數,即可輕鬆計算第四個。

    我寫了一篇文章關於這一點:
    Crop-To-Fit an Image Using ASP/PHP