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();
Related
We're using a CMS with WideImage built in and no scope to change this, however we've come up against a situation where we need to produce 300x300 images.
However the images that are uploaded at approx 100x100 in jpg and png format with various levels of transparency.
I'm trying to create a 300x300 canvas, and place the 100x100 image inside of it - however I want both the canvas to remain transparent, and the image placed on top to keep its transparency.
I've got
$image = WideImage::loadFromFile( $this->local_path );
$canvas = WideImage::createTrueColorImage(300, 300);
$canvas_bg = $canvas->allocateColor(255, 255, 255);
$canvas->fill(0, 0, $canvas_bg);
$resized_image = $canvas->merge($image);
However this obviously just adds a white background to the canvas, I cant figure out from their documentation how to make it transparent.
Thanks
How about:
$image = WideImage::loadFromFile($this->local_path);
$resized_image = $image->resizeCanvas(300, 300, 0, 0);
You can specify the X and Y location of the image within the new canvas as well as its width and height. $image->resizeCanvas(300, 300, "center", "center") works quite well.
I am trying to make background of image transparent.Using following code
$strInputFile = 'test.jpg';
$target = 'test_transparent.png';
$im = new Imagick($strInputFile);
$im->paintTransparentImage($im->getImageBackgroundColor(), 0, 7000);
$im->setImageFormat('png');
$im->writeImage($target);
$im->destroy();
This code is working great for few images.But for most of the images output doesn't come out well.Wanted result come from script.
Unwanted result come from script.
Is it possible to detect edges of model and remove color before model?
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 wonder if it is possible to have "opacity" effect when drawing images on top of other images with the PHP GD extension? Are there any built-in functions that can get the results I want or do I have to go for my own implementation using imagesetpixel way?
A pseudo-code to illustrate what I am trying to do right now:
// Background image
$image_bg = imagecreatetruecolor(100, 100);
imagesavealpha($image_bg, true);
// Making background image fully transparent
imagefill($image_bg, 0, 0, imagecolorallocatealpha($image_bg, 0, 0, 0, 127));
// Now the actual image I want to draw with opacity (true color PNG)
$image = imagecreatefrompng(...);
// Drawing with 50 "merge" value
imagecopymerge($image_bg, $image, ..., 50);
The trouble with above code is that imagecopymerge will not respect background image alpha value and will merge the image with the background as if it was opaque black color (the resulting image will not be 50% transparent).
Edit: I ended up implementing my own function using imagecolorat and imagesetpixel way.
Take a look at imagecolortransparent() and imagealphablending() (or this question).
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());