COMPOSITE_DISSOLVE is not working in PHP - php

I want to make the mixed image with fade effect from two images.
Here is my PHP code.
$img1 = new Imagick();
$img1->readImage("images/1.jpg");
$img2 = new Imagick();
$img2->setOption('compose:args', '50');
$img2->readImage("images/2.jpg");
$img1->compositeImage($img2, imagick::COMPOSITE_DISSOLVE, 0, 0);
$img1->writeImage("images/3.jpg");
This code works correctly on my Mac OS.
But it doesn't work on remote server (Cent OS 6.5), the result image is same with 2.jpg.
I tried with COMPOSITE_BLEND constant, it works well.
And I tried the command in terminal.
# composite -dissolve 50 2.jpg 1.jpg 3.jpg
It also works well.
Why doesn't COMPOSITE_DISSOLVE constant work in PHP?
Please help me.

You really ought to just figure out what the difference is between your dev environment and your server. It's very likely to be the case that the version of ImageMagick on the dev server is very out of date, unless you are compiling it yourself, as the Centos packages management system seems to take a long time to get new releases.
However....the way you are blending the images is not fantastic. Although it may work for you, it requires the slightly poorly defined $img2->setOption('compose:args', '50'); line.
A much more powerful technique is to control the alpha channel yourself through the COMPOSITE_COPYOPACITY. This gives you full control for how images are blended. e.g.
$img1 = new \Imagick();
$img1->readImage(realpath("../images/Biter_500.jpg"));
$img2 = new \Imagick();
$img2->readImage(realpath("../images/Skyline_400.jpg"));
//Resize images to the same size, to look pretty.
$img1->resizeimage(
$img2->getImageWidth(),
$img2->getImageHeight(),
\Imagick::FILTER_LANCZOS,
1
);
//Create an image that the alpha will be created in.
$opacity = new \Imagick();
if (true) {
//Create a 50% grey image
$opacity->newPseudoImage($img1->getImageWidth(), $img1->getImageHeight(), "CANVAS:gray(50%)");
}
else {
//Create a far more interesting blend.
//Gradients are created top to bottom, so we need to rotate the image
$opacity->newPseudoImage($img1->getImageHeight(), $img1->getImageWidth(), "gradient:gray(10%)-gray(90%)");
$opacity->rotateimage('black', 90);
}
$img2->compositeImage($opacity, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
$img1->compositeImage($img2, \Imagick::COMPOSITE_ATOP, 0, 0);
header("Content-Type: image/jpg");
echo $img1->getImageBlob();
The first way of creating the opacity image with "CANVAS:gray(50%)" does what your existing image code does. The second way of blending creates a image that is blended from 10% to 90% across the width of the image.

Related

Losing transparency of png with php magickwand and imagemagick when rotation is applied

Hi I'm porting a script from a windows environment running MagickWand 0.1.8 and ImageMagick 6.2.9 where transparency worked fine with MagickRotateImage.
New environment is linux running MagickWand 1.0.8 with ImageMagick 6.5.4-7 and transparency gets lost and it shows black as the background as soon as my logo image gets rotated.
From what I've found online, seems that PixelSetColor($bg,"none") doesn't work with the newer versions hence the black. Ultimately, I need to know what to replace PixelSetColor($bg,"none") with. I just don't have a background in image creation so struggling a bit with this.
First my php runs this function which makes a local 60x60 version of 600x600 png image gotten from a url.
function makeThumb($fileContents){
GLOBAL $localImgPath1;
GLOBAL $localImgPath2;
$wand = NewMagickWand();
$lg = MagickReadImageBlob($wand,$fileContents);
$lg_w = MagickGetImageWidth($wand);
$lg_h = MagickGetImageHeight($wand);
$max = max($lg_h,$lg_w);
$scale_factor = 60/$max;
MagickResizeImage($wand,$lg_w*$scale_factor,$lg_h*$scale_factor, MW_GaussianFilter, .7);
MagickWriteImage($wand, $localImgPath1);
if($localImgPath2!="")
MagickWriteImage($wand, $localImgPath2);
$resized_w = MagickGetImageWidth($wand);
$resized_h = MagickGetImageHeight($wand);
DestroyMagickWand($wand);
}
then, I use this to read that locally written png image and rotate it:
$logo = NewMagickWand();
$bg = NewPixelWand();
PixelSetColor($bg,"none");
MagickReadImage($logo, $localImgPath1);
MagickRotateImage($logo, $bg, $r);
header('Content-Type: image/PNG');
MagickEchoImageBlob($logo);
DestroyPixelWand($bg);
DestroyMagickWand($logo);
I've tried things like adding:
$transparent = NewPixelWand("#FFFFFF");
PixelSetAlpha($transparent, 0);
//and then making the rotate call:
MagickRotateImage($logo, $transparent, $r);
Also tried adding MagickSetImageAlphaChannel($logo, MW_SetAlphaChannel); before the rotate step. Saw some posts mentioning that method but possibly that's not the correct way to use it. Not sure.
I also have the same problem in a script that draws a text string with a set font onto the image. Black immediately shows there even before rotation is applied so hoping same fix for the logo script will be useable by the font script.
Any help would be greatly appreciated. Thanks.
You've got a choice of four values for a transparent background. Try each in turn:
"none", "transparent", "#00000000", or "rgba(0, 0, 0, 0.0)"
The problem was with ImageMagick 6.5.4-7. Must have been a buggy release. Upgraded to 6.8.4-10 and above code works fine, PixelSetColor($bg,"none") works for the transparency.

php Imagick rotate and merge

i have 2 images one background and another one is layer first i want to rotate it then i want to place it somewhere using x and y.. it can be done using GD but in gd when rotate the image i get low quality and zigzag border.. so i tried to rotate in imagick it done its job good without zigzag and without loosing quality.. so i want to know how to place that rotated image to background...
Background
Image_2
Output should look like this
EDIT:
The code I have tried so far:
<?php
$imagick = new Imagick();
$imagick->readImage('137.png');
$imagick->rotateImage(new ImagickPixel('none'), -13.55);
$imagick->writeImage('my_rotated.png');
$imagick->clear();
$imagick->destroy();
?>
I was trying to find a solution myself. I guess this should get what you need:
$background = 'background.png';
$image = new Imagick($background);
// you can place the code below in a loop to put more images in order
$imgfile = 'foreground-image.png';
$part1 = new Imagick($imgfile);
$part1->rotateImage(new ImagickPixel('none'), -13.55);
$image->compositeImage($part1, Imagick::COMPOSITE_OVER, $left, $top);
$image->writeImage('my_rotated.png');
//save , flush , destroy etc...

How to merge two images and save in php?

I am creating an application in php in which i upload a image and resize it and place it on a Coffee Mug. Coffee mug is a background image of a div. Now i want to save both images as a single image it would look like a screenshot of a coffee mug with the image printed on it. How to take screenshot of only mug and image on it or if there is an another way to doing it in php.
You need an image library for that. Most PHP versions on many servers are precompiled with GD. This is a limited and not so well performing library, so most people will use Imagemagick.
In GD:
$mug = imagecreatefrompng('mug.jpg');
$overlay = imagecreatefromjpeg('photo.png');
imagecopy($mug, $overlay);
In Imagemagick:
$mug = new Imagick("mug.jpg");
$overlay = new Imagick("logo.png");
$composition = new Imagick();
$composition->compositeImage($mug);
$composition->compositeImage($overlay);
You can do this by using the GD2 library:
<?php
$image = imagecreatefromstring(file_get_contents($image_path);
$coffe_mug= imagecreatefromstring(file_get_contents($coffe_mug_path));
// merge the two
imagecopymerge($image, $coffe_mug, 10, 10, 0, 0, 100, 47, 75);
// save, can also be imagepng, imagegif, etc, etc
imagejpeg($image, '/path/to/save/image.png');
?>
You can also output the image to the browser, for more info check: http://php.net/manual/es/function.imagecopymerge.php.

php Imagemagick jpg black background

I have a php script to create jpg thumbnail of pdf as follows;
<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("jpg");
$im->resizeImage(200,200,1,0);
// start buffering
ob_start();
$thumbnail = $im->getImageBlob();
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,".base64_encode($thumbnail)."' />";
?>
But the resulting jpg have black background instead of white.. How can I fix this??
None of the previously posted answers worked for me however the below did:
$image = new Imagick;
$image->setResolution(300, 300);
$image->readImage("{$originalPath}[0]");
$image->setImageFormat('jpg');
$image->scaleImage(500, 500, true);
$image->setImageAlphaChannel(Imagick::VIRTUALPIXELMETHOD_WHITE);
As I'm using the Laravel framework I then take the converted image and store it using Laravels filesystem.
Storage::put($storePath, $image->getImageBlob());
Update: So I recently changed OS and whereas this previously worked on my Ubuntu machine on my Mac certain images were still coming out black.
I had to change the script to the below:
$image = new Imagick;
$image->setResolution(300, 300);
$image->setBackgroundColor('white');
$image->readImage("{$originalPath}[0]");
$image->setImageFormat('jpg');
$image->scaleImage(500, 500, true);
$image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
Seems important to set the background colour before reading in the image. I also flatten any possible layers and remove the alpha channel. I feel like I tried ALPHACHANNEL_REMOVE on my Ubuntu machine and it didn't work so hopefully between these answers readers can find something that works for them.
If your version of Imagick is not up to date, the setImageBackgroundColor may be wrong.
Swap the following line
$im->setImageBackgroundColor("red");
to this (Imagick version >= 2.1.0)
$im->setBackgroundColor(new ImagickPixel("red"));
or (Imagick version < 2.1.0)
$im->setBackgroundColor("red");
I solved it by;
$im = new imagick(realpath($file).'[0]');
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(100);
$im->setImageFormat("jpeg");
$im->writeImage("imagename.jpg");
Simply adding this prevents the JPG to be created with a black background
-alpha off
change this code $im->setimageformat("jpg"); to this code
$im->setimageformat("png"); if you face a background colour issue.
Just use flattenImages() right after creating a new imagick():
$im = new Imagick('file.pdf[0]');
$im = $im->flattenImages();
Edit: The flattenImages method has been deprecated & removed. Use
$im = $im->mergeImageLayers( imagick::LAYERMETHOD_FLATTEN );
After endless attempts to append a pdf file with a jpeg image without getting black areas, I found the solution: the function transformImageColorspace
Used in this order works perfectly:
$im = new Imagick();
$im->readImage("file.pdf");
$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
$im->setImageFormat('jpeg');
$im->writeImage('image.jpg');
I would like to add to the excellent and helpful answers by saying that a slightly different approach is required if your PDF has multiple pages.
Something I was surprised to discover is that the Imagick class implements Iterable, which means you can run it through a foreach loop and operate on each page in turn (which it turns out is necessary because the layer, colour, and alpha channel changes appear to only take effect on the last page) which will be presented to you as a separate Imagick object:
$im = new Imagick('path/to/file.pdf');
foreach ($im as $c => $page)
{
// do what you need to do with each page and then...
$im->writeImage('path/to/image-'.$c.'.jpg');
}

Imagick roundCorners makes image half-transparant

I'm in the process of writing an image script for a website I'm making, and for one of the image modes I need to both crop the image and make round corners for it. This is the code I'm using now:
$img = new Imagick();
$img->readImageBlob($data);
$img->resizeImage($width, $height, $img->FILTER_GAUSSIAN, 1);
$canvas = new Imagick();
$canvas->newImage($size[0], $size[1], new ImagickPixel('white'));
$canvas->compositeImage($img, imagick::COMPOSITE_OVER, 0, 0);
$canvas->roundCorners(10, 10);
$canvas->setImageFormat('png');
Resulting image
However, the resulting image is about half-transparent, as seen above, except for a 10-ish px border around the edge, created by roundCorner I assume. I've tried a lot of different ways of doing this, including cropping $img directly and roundCorner on it, but then the result is without round corners.
I suspect compositeImage to be the thief in this drama, but I can't say for sure. Anyone?
EDIT: Correction: If I do the crop and round solution, I end up with rounded corners, but they have a black background. This is the code I'm using here. Anyone spot the error/solution?
$img = new Imagick();
$img->readImageBlob($data);
$img->resizeImage($width, $height, $img->FILTER_GAUSSIAN, 1);
$img->cropImage($size[0], $size[1], 0, 0);
$img->roundCorners(10, 10);
EDIT 2:
The solution to the crop and round resolution was to make sure it's a png, so that is has alpha-support. However, the result is the same as the one produced from my original code.
EDIT 3:
Tried this script on my deployment server, and it worked as I wanted it to, so it seems like there's some problem with the version of imagick / php-imagick shipped with macports. Thanks for all the help!
You can tell imagemagick to give you an alpha background with
$img->setImageAlphaChannel ( imagick::ALPHACHANNEL_TRANSPARENT )
or try setting the image format to PNG24 explicitly before doing any transformations.
Order is important here, the image needs an alpha channel before you expose the background.
It was a problem with the version of Imagick / php-imagick shipped with current macports. Haven't solved it for my computer, but the script is working as intended on my deployment server.

Categories