【问题标题】:Divide photo to X and Y pieces将照片分成 X 和 Y 部分
【发布时间】:2012-04-25 05:21:00
【问题描述】:

我有一个 96x96 的图像,我需要将它分成 36 个 16x16 的图像,下面给出的脚本在我的 localhost 上可以正常工作,但在我的网络主机上无法正常工作。

    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

我的主人说:

Imagick 和 image magic 都是一样的,可惜我们没有 它们作为一个 PHP 模块,我们有像 ImageMagick 二进制和 图片魔术路径是/usr/local/bin。将此功能包含在您的 编写脚本并从您的终端检查网站功能。

我不知道如何解决这个错误。

【问题讨论】:

  • 我不确定,但可能是包含 Imagick 类路径的包含语句?

标签: php image fatal-error imagick


【解决方案1】:

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

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

oxoy 应替换为正确的偏移值。

这里有几个链接可能会有所帮助:

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

【讨论】:

  • 现在我的主人说It is not possible to run the script on our servers :( 有没有其他方法可以在没有imagic的情况下将图像分割成碎片
  • 是的,当然有,您可以简单地使用标准 GD 函数,如 imagecreatefromjpg()imagecopy() 等。查看 GD 和图像功能:php.net/manual/en/ref.image.php
  • 您应该尝试与您的托管服务提供商交谈,询问他们如何在您的 php 脚本中使用 imagick。第一条消息似乎表明它应该确实有效。
  • 当我尝试上面给定的代码时出现此错误。 $inputFile = '2_thumb.jpg'; $newFilename = '2_thumb16.jpg'; exec('/usr/local/bin/convert '.$inputFile.' -crop 96x96+16+16 '.$newFilename); The server encountered an unexpected condition which prevented it from fulfilling the request. The script had an error or it did not produce any output. If there was an error, you should be able to see it in the error log.
猜你喜欢
  • 2019-10-13
  • 1970-01-01
  • 2019-12-20
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多