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.
Related
I am trying to resize a pdf (which i converted from image), and I am trying to resize (increase) the image in its ratio.
$imagick = new \Imagick();
$imagick->readImage($path);
$imagick->resizeImage(595,842,\Imagick::FILTER_CUBIC, 1, true);
// and this:
// $imagick->adaptiveResizeImage(595,842, true);
$imagick->setImageFormat('pdf');
$imagick->writeImage($endpath);
But the image is getting blurry. However, it's not a bad quality image and the image can go that size without getting disturbed. (For example, if I let Twilio to do it (via fax api), the same image can get to that scale).
I have also tried with putting blur parameter of resizeImage between '1' and '0.1'
Original pdf (you can also see it here, if you want to try):
My resized pdf (with blur 1):
My "adaptive resized" pdf:
You are starting with a very small pdf if rasterized and enlarging. So that would cause blurring. But if you increase the input density, it works fine for me in ImageMagick as
convert -density 600 input.pdf -resize 595x842 result.png
I do not know Imagick well, but try the following. Reduce the blur value in resizeImage as desired to make it sharper.
$imagick = new \Imagick();
$imagick->readImage($path);
$imagick->Imagick::setImageResolution( 600, 600 );
$imagick->resizeImage(595,842,\Imagick::FILTER_CATROM, 1, true);
$imagick->setImageFormat('pdf');
$imagick->writeImage($endpath);
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();
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 :)
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.
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());