我使用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/cropImage和http://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
如何解决这个问题?
2条答案
按热度按时间zc0qhyus1#
这通常是由dir不存在引起的。
添加此代码:
之前
递归创建路径。
insrf1ej2#
imagick
需要具有write
权限因此,在安装
imagick
之后,使用以下命令:你需要去这个文件:
并编辑这一行:
改为:
在此之后,您将给予
imagick
写入权限source