我尝试使用 imagick 将水印应用到图像上,但无法使用 imagick 加载图像
这就是我正在尝试的
<?php
// Open the original image
$image = new Imagick();
$image->readImage("lion.jpg");
// Open the watermark
$watermark = new Imagick();
$watermark->readImage("1.png");
// Overlay the watermark on the original image
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, 0, 0);
// send the result to the browser
header("Content-Type: image/" . $image->getImageFormat());
echo $image;
?>
每当我运行此代码时,我都会收到此错误
Fatal error: Uncaught exception 'ImagickException' with message 'unable to open image `lion.jpg': No such file or directory @ error/blob.c/OpenBlob/2617' in C:\xampp\htdocs\imagick.php:4 Stack trace: #0 C:\xampp\htdocs\imagick.php(4): Imagick->readimage('lion.jpg') #1 {main} thrown in C:\xampp\htdocs\imagick.php on line 4
代码和图像都在同一目录中。
我正在 Windows 上使用 ImageMagick 6.7.7-4 2012-05-29 Q16
try this
<?php
// Open the original image
$image = new Imagick();
$image->readImage("C:/xampp/htdocs/projectname/lion.jpg");
// Open the watermark
$watermark = new Imagick();
$watermark->readImage("C:/xampp/htdocs/projectname/1.png");
// Overlay the watermark on the original image
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, 0, 0);
// send the result to the browser
header("Content-Type: image/" . $image->getImageFormat());
echo $image;
?>
Imagick::mergeImageLayers()
:
$img1 = new Imagick(realpath('lion.jpg'));
$img2 = new Imagick(realpath('1.png'));
// in case you need to add opacity...
$img2->setImageOpacity(.5);// between 0.0 and 1.0
$img2->setImagePage($img2->getImageWidth(), $img2->getImageHeight(), 0, 0);
$img1->addImage($img2);
$result = $img1->mergeImageLayers(Imagick::LAYERMETHOD_COMPOSITE);
$format = 'jpg';
$result->setImageFormat($format);
header('Content-Type: image/'.$format);
echo $result->getImageBlob();
为了简化,假设图像宽度相同