想象图像水印透明

时间:2015-11-20 02:05:35

标签: image transparent imagick watermark

我想在图片上设置透明度,当imagick的版本为6.8.9时,下面不起作用。

<?php
// Open the original image
$image = new Imagick();
$image->readImage(3.jpg);

// Open the watermark
$watermark = new Imagick();
$watermark->readImage(2.png);
$watermark->setImageOpacity(0.4);

// Overlay the watermark on the original image
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, 20, 20);

// send the result to the browser
header("Content-Type: image/" . $image->getImageFormat());
echo $image; 

还有另一种解决透明问题的方法吗?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。

$watermark->setImageOpacity(0.4);
//It wouldn't work well because it would have a uniform effect On the picture.

$watermark->evaluateImage(Imagick::EVALUATE_MULTIPLY, 0.4, Imagick::CHANNEL_ALPHA);
//It works well and have a satisfactory result.

我用Go意识到了这一点。下面的代码运行良好并且运行成功。

mw = imagick.NewMagickWand()
mw.ReadImageBlob(src_image) // src_image is the source image 

water_mw = imagick.NewMagickWand()
water_mw.ReadImageBlob(water_image) // water_image is the watermark image

// Set the transparent with 0.4
water_mw.EvaluateImageChannel(imagick.CHANNEL_ALPHA, imagick.EVAL_OP_MULTIPLY, 0.4)
//if water image has no alpha channel, replace with water_mw.EvaluateImage(imagick.EVAL_OP_MULTIPLY, 0.4)

// Composite the water image on source image, x, y are the coordinate u would composite
mw.CompositeImage(water_mw, imagick.COMPOSITE_OP_DISSOLVE, 20, 20)
dst_image = mw.GetImageBlob()