php 致命错误:未捕获的异常“ImagickException”,消息为“无法打开图像

64jmpszr  于 2023-04-04  发布在  PHP
关注(0)|答案(2)|浏览(442)

我使用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);
}
?>

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

如何解决这个问题?

zc0qhyus

zc0qhyus1#

这通常是由dir不存在引起的。
添加此代码:

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

之前

$imagick->writeImage($destination);

递归创建路径。

insrf1ej

insrf1ej2#

imagick需要具有write权限
因此,在安装imagick之后,使用以下命令:

sudo apt install php-imagick

你需要去这个文件:

/etc/ImageMagick-6/policy.xml

并编辑这一行:

< policy domain="coder" rights="none" pattern="PDF" / >

改为:

< policy domain="coder" rights="read|write" pattern="PDF" / >

在此之后,您将给予imagick写入权限
source

相关问题