I have the last few hours tried to get a PNG logo with a transparent background on top of a JPG background. I have tried several ways and with several globals as well, but I do not seem to be able to get the result I want.
"First Attempt":
$overlay = new Imagick('overlay.png');
$image = new Imagick('background.jpg');
$image->compositeImage($overlay, Imagick::COMPOSITE_DEFAULT, 10, 10);
$image->writeImage('background.jpg'); //replace original background
$overlay->destroy();
$image->destroy();
As you can see, the Jaguar logo is all black.
"Second Attempt"
$overlay = new Imagick('overlay.png');
$image = new Imagick('background.jpg');
$image->setImageColorspace($overlay->getImageColorspace() );
$image->compositeImage($overlay, Imagick::COMPOSITE_DEFAULT, 10, 10);
$image->writeImage('background.jpg'); //replace original background
$overlay->destroy();
$image->destroy();
This one the Jaguar logo looks like it should, but the background is all messed up now.
I have tried with Imagick::setImageMatte and tried to add the overlay to a white background (thought I does need to have a transparent background) and still it won't display the image properly. I have tried many other variations of the 2 above snippets but they all seem to make the PNG completely or partial black.
What am I missing or doing wrong? Can anyone give me a nudge in the right direction?
Please note this needs to be done in PHP.
Thank you very much!
I am a huge idiot! Turns out I forgot to convert the images from CMYK to RGB.
For anyone who might encounter this in the future, learn from my incompetence!
I was trying to overlay a png with transparency over the top of another png. I used this line from the PHP docs.
$src1->compositeImage($src2, Imagick::COMPOSITE_MATHEMATICS, 0, 0);
but I was getting the same problem. The overlay came through as black only. Changing it to this seemed to fix the colours.
$src1->compositeImage($src2, Imagick::COMPOSITE_DEFAULT, 0, 0);
Related
I have a script that handles/scales uploaded images. I noticed that some of the images come out significantly darkened, and through a process of elimination tracked the darkening back to this section of code:
$scaled = new IMagick();
$scaled->newPseudoImage($original->getImageWidth(), $original->getImageHeight(), 'xc:white');
$scaled->compositeImage($original, Imagick::COMPOSITE_DEFAULT, 0, 0);
$scaled->flattenImages();
What I'm doing here is trying to eliminate issues with transparent backgrounds in some images coming through as black when I convert to jpg.
Does anyone have any idea which part of this code is darkening the image and what a good way to fix it might be?
Edit: Still haven't figured out the heart of this issue, but I did find that I can avoid doing this to images that don't need it by wrapping it in:
if($original->getImageAlphaChannel()){
I am trying to do a watermark mark with the php code, and everything seems to work fine, until I put a transparent PNG file to a GIF. This what happens:
So instead of transparent black watermark, I get this semi solid green thing on the top.
The watermark is
I use the following php code:
...
$image = imagecreatefromgif($filepath);;
$watermark_image = imagecreatefrompng($wm_filepath);
imagealphablending($watermark_image, false);
imagesavealpha($watermark_image, true);
imagegif($image, $filepath);
imagedestroy($image);
imagecopy($image, $watermark_image, $offset['x'], $offset['y'], 0, 0, imagesx($watermark_image), imagesy($watermark_image) );
p.s. I have to mention that I tried to combine different settings using
imagealphablending()
imagesavealpha()
and got no result
UPD:
Now I am saving image as a png file. I deleted these two rows imagealphablending($watermark_image, false); imagesavealpha($watermark_image, true); and it worked. However, the transparency of PNG is overlapping GIF. imagealphablending($image, true); didn't help. What shall I do?
GIF files are limited to 256 colours and have a single colour designated as transparent. So you can't have antialiased transparency, nor can you have much antialiasing of any kind.
I suggest saving as PNG instead.
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.
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.
On a Magento site, I’m just uploading one picture per product, and using that as the base, small and thumbnail images. However, my pictures aren’t square, so I’m getting a black background added to the thumbnail that’s created. Any ideas how to change that to a white background?
http://www.magentocommerce.com/?ACT=25&fid=10&aid=10785_osKeOnfKFLFvgqVYbr0j&board_id=1
I’ve searched for this but can’t find a decent answer anywhere…
I found a workaround for this. The side effect is that the class used for image resize will fill all images with white (even transparent ones), but this doesn't realy affect Magento since it doesn't use transparent fill anyway. So to solve the issue do this:
Go to lib\Varien\Image\Adapter\Gd2.php in your magento folder
Find this line
$this->_fillBackgroundColor($newImage);
and replace it with this line
$this->_fillBackgroundColor($newImage,$frameWidth,$frameWidth);
Find this line
private function _fillBackgroundColor(&$imageResourceTo)
and replace it with this
private function _fillBackgroundColor(&$imageResourceTo,$w,$h)
Find this code
if (!imagefill($imageResourceTo, 0, 0, $color)) {
throw new Exception("Failed to fill image background with color {$r} {$g} {$b}.");
}
and replace it with this
imagefilledrectangle($imageResourceTo,0,0,$w,$h,$color);
That is it. The problem comes from the PHP function imagefill in Gd2 not working on some setings. This is a workaround using imagefilledrectangle, and worked for me. Hope it solves your problem too.
Correction on point 4 from Adi's answer:
4. Find this code
if (!imagefill($imageResourceTo, 0, 0, $color)) { throw new Exception("Failed to fill image background with color {$r} {$g} {$b}."); }
and replace it with:
if (!imagefilledrectangle($imageResourceTo,0,0,$w,$h,$color)) {
throw new Exception("Failed to fill image background with color {$r} {$g} {$b}.");
Varien_Image which is used in Magento offers a possibility to set background color of scaled image. You can use similar code somewhere before outputting the image (resize method?) to make background white:
$image->backgroundColor(array(255, 255, 255));
Adi's answer led me in the right direction, but I was still getting a black line on my images. I discovered that when an image is cropped, Magento creates a canvas background, then the image on top of it. The canvas was producing the black line on my images.
I managed to fix it by adding Adi's changes, and then additionally, in the crop() function (in the same file), I added:
list($r, $g, $b) = $this->_backgroundColor;
$bg_color = imagecolorallocate($canvas, $r, $g, $b);
imagefill($canvas, 0, 0, $bg_color);
After the function (around line 554, after adi's changes):
imagecopyresampled
Now my images have the background colour they should have!
Another thing to note, instead of editing this file directly in the core, it should be copied to:
app/code/local/Varien/Image/Adapter/Gd2.php
Hope this helps someone struggling with the same issue.