我使用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
如何解决此问题?
答案 0 :(得分:1)
这通常是由dir不存在引起的。
添加以下代码:
$destinationPath = dirname($destination);
`mkdir -p $destinationpath`;
前
$imagick->writeImage($destination);
以递归方式创建路径。