dqdfpmmi022763 2011-01-18 22:26
浏览 115
已采纳

IMagick功能将图像切割成大致相同大小的图块

I'm trying to use the IMagick PHP wrapper to assist in chopping a specified image into a set of tiles (the number of which is variable).

In the ImageMagick documentation there is reference to the -crop operator accepting an optional flag of @ that will instruct it to cut an image into "roughly equally-sized divisions" (see here), solving the problem of what to do when the image size is not an exact multiple of the desired tile size.

Does anyone know if there is a way to leverage this functionality in the IMagick PHP wrapper? Is there anything I can use besides cropImage()?

  • 写回答

1条回答 默认 最新

  • duanben1324 2011-05-13 12:07
    关注

    I had to do the same thing (if I'm reading your question correctly). Although it does use cropImage...

    
    function slice_image($name, $imageFileName, $crop_width, $crop_height)
    {
    
    

    $dir = "dir where original image is stored"; $slicesDir = "dir where you want to store the sliced images; mkdir($slicesDir); //you might want to check to see if it exists first....

    $fileName = $dir . $imageFileName;

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

    $crop_width_num_times = ceil($imgWidth/$crop_width); $crop_height_num_times = ceil($imgHeight/$crop_height); for($i = 0; $i < $crop_width_num_times; $i++) { for($j = 0; $j < $crop_height_num_times; $j++) { $img = new Imagick($fileName); $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);
    }
    

    } }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?