【问题标题】:Fatal error: Uncaught exception 'ImagickException' with message 'unable to open image致命错误:未捕获的异常“ImagickException”,消息“无法打开图像”
【发布时间】:2016-11-25 07:59:39
【问题描述】:

我正在使用 php Imagick 上传使用 `cropImage' 的裁剪图像,之后我想使用 'resizeImage' 调整裁剪图像的大小。此处图像裁剪工作正常,但调整图像大小显示异常,我的代码如下。

上传.php

 move_uploaded_file($_FILES["file"]["tmp_name"], $newUploadDir. $newfilename);
 cropImage($newUploadDir.$newfilename,$newUploadDirSmall.$newfilename,$x,$y,$w,$h);
 resizeImage($newUploadDirSmall.$newfilename,$newUploadDirSm.$newfilename,211, 50, 0, 0, true, false);

MagikLib.php

 <?php
function cropImage($source,$destination, $startX, $startY, $width, $height)
{
    $imagick = new \Imagick(realpath($source));
    $imagick->cropImage($width, $height, $startX, $startY);
    header("Content-Type: image/jpg");
    echo $imagick->getImageBlob();
    $imagick->writeImage($destination);
}
function resizeImage($source,$destination, $width, $height, $filterType, $blur, $bestFit, $cropZoom)
{
    //The blur factor where > 1 is blurry, < 1 is sharp.
    $imagick = new \Imagick(realpath($source));
    $imagick->resizeImage($width, $height, $filterType, $blur, $bestFit);
    $cropWidth = $imagick->getImageWidth();
    $cropHeight = $imagick->getImageHeight();

    if ($cropZoom) {
        $newWidth = $cropWidth / 2;
        $newHeight = $cropHeight / 2;

        $imagick->cropimage(
            $newWidth,
            $newHeight,
            ($cropWidth - $newWidth) / 2,
            ($cropHeight - $newHeight) / 2
        );

        $imagick->scaleimage(
            $imagick->getImageWidth() * 4,
            $imagick->getImageHeight() * 4
        );
    }
    header("Content-Type: image/jpg");
    $imagick->writeImage($destination);
}
?>

MagicLib.php 引用自 http://phpimagick.com/Imagick/cropImagehttp://phpimagick.com/Imagick/resizeImage

cropImage 工作正常并将图像保存到文件夹但 resizeImage 显示以下错误

  Warning: Cannot modify header information - headers already sent by (output started at /home/a/public_html/img/MagikLib.php:7) in /home/a/public_html/img/MagikLib.php on line 34

    Fatal error: Uncaught exception 'ImagickException' with message 'unable to open image `/home/a/public_html/img/images//b6d767d2f8ed5d21a44b0e5886680cb9/user/sm/image1.jpg': No such file or directory @ error/blob.c/OpenBlob/2709' in /home/a/public_html/img/MagikLib.php:35 Stack trace: #0 /home/a/public_html/img/MagikLib.php(35): Imagick->writeimage('/home/a/pu...') #1 /home/a/public_html/img/upload1.php(74): resizeImage('/home/a/pu...', '/home/a/pu...', 211, 50, 0, 0, true, false) #2 {main} thrown in /home/a/public_html/img/MagikLib.php on line 35

如何解决这个问题?

【问题讨论】:

  • /home/a/public_html/img/images//b6d767d2f8ed5d21a44b0e5886680cb9/user/sm/ 这个目录是否存在并且www-data用户在这个路径上有x权限
  • pastebin.com/EBDjBRRk这是我的完整代码请看一次

标签: php imagick


【解决方案1】:

这通常是dir不存在造成的。

添加此代码:

$destinationPath = dirname($destination);
`mkdir -p $destinationpath`;

之前

$imagick->writeImage($destination);

递归创建路径。

【讨论】:

  • $newUploadDirLarge=$uploadDir."/".$userFolder."/cover/large/"; $newUploadDirSmall=$uploadDir."/".$userFolder."/cover/fit/"; $newUploadDirSm=$uploadDir."/".$userFolder."/profile/sm/";删除上传的“/”,最后有/
  • 是的,它正在工作,但显示以下警告。如何删除此警告Warning: Cannot modify header information - headers already sent by (output started at /home/a/public_html/img/imagikMethods.php:7) in /home/a/public_html/img/imagikMethods.php on line 34
  • 删除标题("Content-Type: image/jpg");在 resizeimage 函数中,因为它已在 cripimage 函数中设置。
  • 就像上面的评论一样,你使用 header("Content-Type: image/jpg");两次,删除标题("Content-Type: image/jpg");在 resizeimage 函数中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-04
相关资源
最近更新 更多