2011-07-11 119 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); 
     } 
    } 
}