本文介绍了将照片分为X和Y片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个96x96图像,我需要将它分成36个16x16并且我有一个下面给出的脚本在我的本地主机上正常工作但不能在我的虚拟主机上工作。

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);
        }
    }
}

致命错误

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

我的主持人说:

我不知道如何解决此错误。

I dont know how to fix this error.

推荐答案

如果我理解正确,你将不得不调用 imagick 使用 exec()的脚本中的二进制文件:

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);

ox oy 应替换为正确的偏移值。

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

这篇关于将照片分为X和Y片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 09:56