2011-07-11 114 views
1

給定一個JPG圖像,切斷它在水平方向上,使得穗的大小是100K左右剪切1大圖像分成小圖像具有相同的大小

任何提示嗎?

+0

您是否嘗試過的東西?你有沒有發現任何問題? –

+0

我恐怕不可能按尺寸裁剪*,因爲壓縮比取決於數據類型。換句話說,一個100KB的圖像將比另一個100KB的圖像具有更大的尺寸(寬度,高度)。您可以使用一些未壓縮的格式(例如bmp)來避免這種情況,但這通常不是一個好主意。 – binaryLV

+0

通過'切'你的意思,偶然,調整? –

回答

0

已經使用這個功能,在過去很適合我......

這將需要一個輸入文件,並創建指定大小的片...

function makeTiles($name, $imageFileName, $crop_width, $crop_height) 
{ 
    $dir = "source dir"; 
    $slicesDir = "target dir"; 

    // might be good to check if $slicesDir exists etc if not create it. 

    $inputFile = $dir . $imageFileName; 

    $img = new Imagick($inputFile); 
    $imgHeight = $img->getImageHeight(); 
    $imgWidth = $img->getImageWidth(); 

     $cropWidthTimes = ceil($imgWidth/$crop_width); 
    $cropHeightTimes = ceil($imgHeight/$crop_height); 
    for($i = 0; $i < $cropWidthTimes; $i++) 
    { 
     for($j = 0; $j < $cropHeightTimes; $j++) 
     { 
      $img = new Imagick($inputFile); 
      $x = ($i * $crop_width); 
      $y = ($j * $crop_height); 
      $img->cropImage($crop_width, $crop_height, $x, $y); 
      $data = $img->getImageBlob(); 
      $newFileName = $slicesDir . $name . "_" . $x . "_" . $y . ".jpg"; 
      $result = file_put_contents ($newFileName, $data); 
     } 
    } 
}