How to create watermark with imagemagick - php

I'm trying to create a watermark with ImageMagick however, the guides on layering are pretty daunting. Basically I'll have one base image say "base.jgp" and an overlay image say "overlay.jpg". Overlay.jpg would be smaller than base.jpg. Which exec command would I run to place overlay centered on top of base.jpg?
Thanks!

shell_exec("composite -gravity center ./images/watermark_horizontal.png {$this->path} {$this->path}");
Here we go

Check out ImageMagick examples, especially the Compositing Images chapter. It has a number of ready-made real-world examples.

$image = new Imagick();
$image->readImage("image.jpg");
// Open the watermark
$watermark = new Imagick();
$watermark->readImage("watermark.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());

Related

Is it possible to draw a trasparent circle in Imagick?

I build graphic editor for specific tasks. Users upload scanned coins and trim background. Frontend part works great, but I have an issue with backend part. I use php Imagick with followong algo:
Create a transparent circle with size equal with coin and white background as a mask
Positioning this circle
Use compositeImage for merge coin image and mask
My current version use external png mask file, because I can't find a solution how to draw a transparent circle. All examples at SO use reverse method - they draw a white circle with transparent background.
UPD: this is working example.
$draw = new \ImagickDraw();
$draw->setFillColor(new ImagickPixel('#ffff00'));
$draw->circle(101, 101, 200, 100);
$imagick = new \Imagick();
$imagick->newImage(202, 202, new ImagickPixel('#ffffff'));
$imagick->setImageFormat("png");
$imagick->despeckleimage();
$imagick->drawImage($draw);
$imagick->transparentPaintImage('#ffff00', 0, 1, false);
header("Content-Type: image/png");
echo $imagick->getImageBlob();

Add watermark to image with imagick PHP

I'm adding watermark.png to original.png, but I have 3 problems:
I want to set watermatk.png on the bottom right
I want to resize watermark.png and make it smaller or bigger
Is there any way to make watermark transparent if background was white?
<?php
// Open the original image
$image = new Imagick();
$image->readImage("./man/original.png");
// Open the watermark
$watermark = new Imagick();
$watermark->readImage("./man/watermark.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;
There is actually a "watermark" command which will handle your opacity issue. Combine it with gravity + geometry to get the size and position that you want.
composite -watermark 30% -gravity north -geometry 150x150+100+50 watermark.png input.jpg output.png
Something like that. I have not tried this, but hopefully it gets you on your way. Let us know how it works out.

How to merge two graphics files? (PHP, Imagick)

I have two graphics file.
The first image - a background image in JPG format
The second file - PNG file with the figure in the center filled with white, with a black border on a path. The main background of transparent PNG file.
Question:
How to merge two files with transparency (see image example)? Background of the first file should be placed inside the figure in the second file.
Scheme:
Images:
PNG file - profiles.in.ua/tmp/sample2.jpg
JPG file - profiles.in.ua/tmp/sample1.png
PHP code:
$mask = new Imagick(realpath('mask.png'));
$pattern = new Imagick(realpath('pattern.jpg'));
$pattern->resizeImage($mask->width, $mask->height, Imagick::FILTER_LANCZOS, 1);
$pattern->compositeImage($mask, Imagick::COMPOSITE_ATOP, 0, 0);
header("Content-Type: image/png");
echo $pattern->getImageBlob();
$mask->destroy();
$pattern->destroy();
Assuming the mask image is always made exclusively of white pixels (which should be overwritten with the pattern), black pixels (which should overwrite the pattern), and transparent pixels (which should remain transparent), you can get this effect by compositing the pattern into the non-transparent pixels in the mask, then darkening the result with the mask.
The PNG file you provided did not have a transparent background as specified; instead it was white and grey hatching. I had to edit it first to add a transparent background before this code worked.
$mask = new Imagick(realpath('sample1.png'));
$pattern = new Imagick(realpath('sample2.jpg'));
$pattern->resizeImage($mask->width, $mask->height, Imagick::FILTER_LANCZOS, 1);
$image = clone($mask);
$image->compositeImage($pattern, Imagick::COMPOSITE_IN, 0, 0);
$image->compositeImage($mask, Imagick::COMPOSITE_DARKEN, 0, 0);
header("Content-Type: image/png");
echo $image;
$image->destroy();
$mask->destroy();
$pattern->destroy();
You need to fix the end of your code. All good until the end.
$base->writeImage('output.png');
header("Content-Type: image/png");
echo $base;
Update me :)

change opacity of image using imagick

I want to change opacity of multiple images when i used setImageOpacity it work fine for all images but not with png images and when i used evaluateImage it is work fine for transparent images but not for other images. how can i used same method for all types of images if image is transparent or not this is the code
<?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.444);
//$watermark->evaluateImage(Imagick::EVALUATE_MULTIPLY, 0.0, Imagick::CHANNEL_ALPHA);
$watermark->rotateImage(new ImagickPixel('transparent'), 90);
// 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;
use if condition using getImageAlphaChannel() function
to detect if the image have any transparent
note :
This method is available if Imagick has been compiled against
ImageMagick version 6.4.0 or newer.

How to change the background black color to transparent?

I am rotating an image using Image Magick (as PHP-GD scales down image).
But it leaves the background to be black. Also, the image doesn't look good at all(but better than PHP-GD).
Any suggestions?
#oren , #razzed
Here's the code
$patchImageS = 'kapeels.png'; // the image to be patched over the final bg
$imagick = new Imagick(); $imagick->readImage($patchImageS);
$imagick->rotateImage(new ImagickPixel(), 355);
$imagick->transparentPaintImage('black', 0.0,0,false);
header('content-type:image/png');
$fp=fopen('tts.png','w+');
fwrite($fp,$imagick->getImage());
fclose($fp);
And this's the image which I am trying to rotate -
http://www.lilpirate.net/kapeels.png
Thanks for the reply guys :-)
After create Imagick object, in him arguments set a background transparent:
$imagick->newimage($width, $height, "rgba(0, 0, 0, 0)");
Rotate the image like this:
$im = new Imagick('kapeels.png');
$im->rotateImage( new ImagickPixel('none'), 7 );
$im->trimImage ( 0 );
$im->resetImagePage( '216x174+0+0' );
$im->writeImage('rotateImage.png');
$im->destroy();
You need thr repage to center the image back on the canvas but I do not know why you need values as Imagemagick does not use them. The only way I can think of automatical setting the values is to get the image size after the trim and use that in the reset.
With the quility all I could suggest is start with a larger image and reduce it after the rotate and see if that helps; perhaps use some sharpening. But I do not think much will help as the lines are so thin and close together.

Categories