user3678248
user3678248

Reputation: 53

imagick image watermark transparent

I want to set the transparent on a picture, and below is not work when the imagick's version is 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; 

Is there another way to solve the problem about transparent?

Upvotes: 0

Views: 1406

Answers (1)

user3678248
user3678248

Reputation: 53

I have found the solution.

$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.

I realized it with Go. Below code works well and run successfully.

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()

Upvotes: 2

Related Questions