Divide photo to X and Y pieces

巧了我就是萌 提交于 2019-12-25 05:08:46

问题


I have a 96x96 image and i need to divide it in 36 pieces of 16x16 and i have a script given below works fine on my localhost but not working on my webhost.

    function makeTiles($name, $imageFileName, $crop_width, $crop_height)
    {
    $dir = "/pic";
    $slicesDir = "/pic/16X16";

    // might be good to check if $slicesDir exists etc if not create it.
    $ImgExt = split(".",$imageFileName);
    $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 . ".".$ImgExt[1];
            $result = file_put_contents ($newFileName, $data);
        }
    }
}

Getting fatal error

Fatal error: Class 'Imagick' not found in myfile.php line number

My host saying:

Imagick and image magic both are same unfortunately, we do not have them as a PHP module we have binaries like ImageMagick binary and Image magic path is /usr/local/bin. Include this function in your script and check the website functionality from your end.

I dont know how to fix this error.


回答1:


If I understand it correctly you will have to invoke the imagick binary from your script using exec():

 exec('/usr/local/bin/convert '.$inputFile.' -crop 96x96+ox+oy '.$newFilename);

The ox and oy should be replaced with the correct offset values.

Here is are a couple of links that might help:

  1. How do I use Imagick binaries from php
  2. Imagick - Convert Command-line tool


来源:https://stackoverflow.com/questions/10309761/divide-photo-to-x-and-y-pieces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!