简体   繁体   English

在PHP中使用imagick加载图像

[英]load image using imagick in php

i tried to apply a watermark onto an image using imagick but unable to load image using imagick 我尝试使用imagick将水印应用于图像,但无法使用imagick加载图像

this is what i am trying 这就是我正在尝试的

<?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;

?>

when ever i run this code i got this error 每当我运行此代码时,我都会收到此错误

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

both the code and the images are in the same directory 代码和图像都在同一目录中

and i am using ImageMagick 6.7.7-4 2012-05-29 Q16 on windows 我在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;

?>

You should use Imagick::mergeImageLayers() like this: 您应该像这样使用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();

This assumes the images are the same width for simplification 为简化起见,假设图像的宽度相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM