Php 致命错误:未捕获异常';ImagickException';带有消息';无法打开图像

Php 致命错误:未捕获异常';ImagickException';带有消息';无法打开图像,php,imagick,Php,Imagick,我正在使用php Imagick使用“cropImage”上传裁剪图像,之后我想使用“resizeImage”调整裁剪图像的大小。在这里图像裁剪工作,但调整图像大小显示异常我的代码如下 upload.php move_uploaded_file($_FILES["file"]["tmp_name"], $newUploadDir. $newfilename); cropImage($newUploadDir.$newfilename,$newUploadDirSmall.$newfilena

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

upload.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);
}
?>

如何解决这个问题?

这通常是由于目录不存在造成的

添加此代码:

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

$imagick->writeImage($destination);

以递归方式创建路径。

/home/a/public_html/img/images//b6d767d2f8ed5d21a44b0e5886680cb9/user/sm/此目录是否存在且www数据用户对此路径具有x权限这是我的完整代码,请参阅一次$newUploadDirLarge=$uploadDir./“$userFolder./cover/large/”$newUploadDirSmall=$uploadDir.“/”$userFolder./cover/fit/”$newUploadDirSm=$uploadDir./“$userFolder./profile/sm/”;删除用于上载的“/”并在结尾处使用/是它正在工作,但显示以下警告。如何删除此警告
warning:Cannot modify header information-第34行/home/a/public_html/img/imagikMethods.php中的/home/a/public_html/img/imagikMethods.php中已发送的标题(输出开始于/home/a/public_html/img/imagikMethods.php:7)
remove header(“内容类型:image/jpg”);在函数resizeimage中,因为它已在cripimage函数中设置。正如上面的注释一样,您使用标题(“内容类型:image/jpg”);两次,删除标题(“内容类型:image/jpg”);in resizeimage函数
$imagick->writeImage($destination);