Cropping animation with imagick results in blue background - php

I have compiled ImageMagick 6.8.7-10 Q8 x86_64 on Ubuntu 13.04 64-bit.
I also installed imagick 3.2.0RC1 for PHP 5.5.7.
I am using the following to crop an animated gif:
$imagick = new \Imagick('anim2.gif');
$imagick = $imagick->coalesceImages();
foreach ($imagick as $frame) {
$frame->cropImage(80, 80, 0, 0);
$frame->thumbnailImage(80, 80);
$frame->setImagePage(80, 80, 0, 0);
}
$imagick = $imagick->deconstructImages();
$imagick->writeImages('test.gif', true);
This is the original image:
And the cropped image becomes:
Notice that the cropped image gains a blue background in the second frame.
Why is this happening?

The problem is that coalesce introduces the background. This may or may not be a bug in imagemagick.
If running from the command line, you need to set the background to none before coalescing:
convert anim2.gif -background none -coalesce temporary.gif
From PHP's imagick library, you need to set all the frames' backgrounds to none:
$imagick = new \Imagick('anim2.gif');
foreach ($imagick as $frame) {
$frame->setImageBackgroundColor('none'); //This is important!
}
$imagick = $imagick->coalesceImages();
foreach ($imagick as $frame) {
$frame->cropImage(80, 80, 0, 0);
$frame->thumbnailImage(80, 80);
$frame->setImagePage(80, 80, 0, 0);
}
$imagick = $imagick->deconstructImages();
$imagick->writeImages('test.gif', true);
The result:

Related

Image background color not fully removed in PHP [duplicate]

I want to remove the white background of any image uploaded on the site working on PHP platform. The uploading function is done but messed up with this functionality.
Here is the link I found here:
Remove white background from an image and make it transparent
But this is doing reverse. I want to remove the colored background and make it image with transparent background.
Since you only need single-color transparency, the easiest way is to define white with imagecolortransparent(). Something like this (untested code):
$img = imagecreatefromstring($your_image); //or whatever loading function you need
$white = imagecolorallocate($img, 255, 255, 255);
imagecolortransparent($img, $white);
imagepng($img, $output_file_name);
function transparent_background($filename, $color)
{
$img = imagecreatefrompng('image.png'); //or whatever loading function you need
$colors = explode(',', $color);
$remove = imagecolorallocate($img, $colors[0], $colors[1], $colors[2]);
imagecolortransparent($img, $remove);
imagepng($img, $_SERVER['DOCUMENT_ROOT'].'/'.$filename);
}
transparent_background('logo_100x100.png', '255,255,255');
Try ImageMagick it did the trick for me. You can also control the amount of color that needs to be removed. Just pass image path, bgcolor as an array of RGB, and fuzz in percent. As long as you have ImageMagick installed on your system/hosting. I had my hosting provider install it for me as a module.
I'm using ImageMagick version 6.2.8
Example:
$image = "/path/to/your/image.jpg";
$bgcolor = array("red" => "255", "green" => "255", "blue" => "255");
$fuzz = 9;
remove_image_background($image, $bgcolor, $fuzz);
protected function remove_image_background($image, $bgcolor, $fuzz)
{
$image = shell_exec('convert '.$image.' -fuzz '.$fuzz.'% -transparent "rgb('.$bgcolor['red'].','.$bgcolor['green'].','.$bgcolor['blue'].')" '.$image.'');
return $image;
}
get the index of white color in the image and set it to transparent.
$whiteColorIndex = imagecolorexact($img,255,255,255);
$whiteColor = imagecolorsforindex($img,$whiteColorIndex);
imagecolortransparent($img,$whiteColor);
you can alternatively use imagecolorclosest() if you don't know the exact color.
The function from #geoffs3310 should be the accepted answer here, but note, that the png saved does not contain an alpha channel.
To remove the background and save the new png as a transparent png with alpha the following code works
$_filename='/home/files/IMAGE.png';
$_backgroundColour='0,0,0';
$_img = imagecreatefrompng($_filename);
$_backgroundColours = explode(',', $_backgroundColour);
$_removeColour = imagecolorallocate($_img, (int)$_backgroundColours[0], (int)$_backgroundColours[1], (int)$_backgroundColours[2]);
imagecolortransparent($_img, $_removeColour);
imagesavealpha($_img, true);
$_transColor = imagecolorallocatealpha($_img, 0, 0, 0, 127);
imagefill($_img, 0, 0, $_transColor);
imagepng($_img, $_filename);
Use the php image processing and GD, read the image pixel by pixel
if the RGB components are all 255 (The pixel is white), set the alpha
channel to 255 (transparent). You may have to change the filetype of the image
depending if the uploaded filetype supports an alpha channel.
Version to conversion from URL and return on page:
$img = imagecreatefromjpeg('http://mypage.com/image.jpg');
$remove = imagecolorallocate($img, 255, 255, 255); // Define color rgb to remove
imagecolortransparent($img, $remove);
ob_start();
imagepng($img);
$imgData = ob_get_clean();
imagedestroy($img);
$data_img = 'data:image/png;base64,'.base64_encode($imgData);
echo '<body style="background: #f00;"><img src="'.$data_img.'"></body>';

PHP Imagick transparency issue with PNG

I am using imagemagick 6.8.4-6 and i'm having an issue with setting the PNG on rotate to have a transparent background. My code is below and i am using imagick through php
$base = new Imagick("images/Champs-43.png");
$layer = new Imagick("images/coporate1.png");
$base->setFormat("png32");
$layer->setFormat("png32");
$layer->rotateImage(new ImagickPixel("none"), 45);
$base->compositeImage($layer, imagick::COMPOSITE_DEFAULT, 20, 20);
header('Content-Type: image/png');
echo $base;
the above gives a black background to the area rotated. i have tried "none", "transparent", "#00000000", and "rgba(0, 0, 0, 0.0)" and none of them seem to work.
Any help appreciated
Try explicitly setting the ImagickPixel.
You can try these:
$layer->rotateImage(new ImagickPixel('#FFFFFF'), 45);
or
$layer->rotateImage(new ImagickPixel('#00000000', 45);

Remove Image background with php and save transparent png

I want to remove the white background of any image uploaded on the site working on PHP platform. The uploading function is done but messed up with this functionality.
Here is the link I found here:
Remove white background from an image and make it transparent
But this is doing reverse. I want to remove the colored background and make it image with transparent background.
Since you only need single-color transparency, the easiest way is to define white with imagecolortransparent(). Something like this (untested code):
$img = imagecreatefromstring($your_image); //or whatever loading function you need
$white = imagecolorallocate($img, 255, 255, 255);
imagecolortransparent($img, $white);
imagepng($img, $output_file_name);
function transparent_background($filename, $color)
{
$img = imagecreatefrompng('image.png'); //or whatever loading function you need
$colors = explode(',', $color);
$remove = imagecolorallocate($img, $colors[0], $colors[1], $colors[2]);
imagecolortransparent($img, $remove);
imagepng($img, $_SERVER['DOCUMENT_ROOT'].'/'.$filename);
}
transparent_background('logo_100x100.png', '255,255,255');
Try ImageMagick it did the trick for me. You can also control the amount of color that needs to be removed. Just pass image path, bgcolor as an array of RGB, and fuzz in percent. As long as you have ImageMagick installed on your system/hosting. I had my hosting provider install it for me as a module.
I'm using ImageMagick version 6.2.8
Example:
$image = "/path/to/your/image.jpg";
$bgcolor = array("red" => "255", "green" => "255", "blue" => "255");
$fuzz = 9;
remove_image_background($image, $bgcolor, $fuzz);
protected function remove_image_background($image, $bgcolor, $fuzz)
{
$image = shell_exec('convert '.$image.' -fuzz '.$fuzz.'% -transparent "rgb('.$bgcolor['red'].','.$bgcolor['green'].','.$bgcolor['blue'].')" '.$image.'');
return $image;
}
get the index of white color in the image and set it to transparent.
$whiteColorIndex = imagecolorexact($img,255,255,255);
$whiteColor = imagecolorsforindex($img,$whiteColorIndex);
imagecolortransparent($img,$whiteColor);
you can alternatively use imagecolorclosest() if you don't know the exact color.
The function from #geoffs3310 should be the accepted answer here, but note, that the png saved does not contain an alpha channel.
To remove the background and save the new png as a transparent png with alpha the following code works
$_filename='/home/files/IMAGE.png';
$_backgroundColour='0,0,0';
$_img = imagecreatefrompng($_filename);
$_backgroundColours = explode(',', $_backgroundColour);
$_removeColour = imagecolorallocate($_img, (int)$_backgroundColours[0], (int)$_backgroundColours[1], (int)$_backgroundColours[2]);
imagecolortransparent($_img, $_removeColour);
imagesavealpha($_img, true);
$_transColor = imagecolorallocatealpha($_img, 0, 0, 0, 127);
imagefill($_img, 0, 0, $_transColor);
imagepng($_img, $_filename);
Use the php image processing and GD, read the image pixel by pixel
if the RGB components are all 255 (The pixel is white), set the alpha
channel to 255 (transparent). You may have to change the filetype of the image
depending if the uploaded filetype supports an alpha channel.
Version to conversion from URL and return on page:
$img = imagecreatefromjpeg('http://mypage.com/image.jpg');
$remove = imagecolorallocate($img, 255, 255, 255); // Define color rgb to remove
imagecolortransparent($img, $remove);
ob_start();
imagepng($img);
$imgData = ob_get_clean();
imagedestroy($img);
$data_img = 'data:image/png;base64,'.base64_encode($imgData);
echo '<body style="background: #f00;"><img src="'.$data_img.'"></body>';

Rotated an PNG image with PHP. How to remove the black lines around the original?

I am using PHP to rotate an PNG image, with a transparant background. But whatever I try, there are still some black lines around the original image.
How do I remove the black lines. Everything else works fine. The image is transparant, the image is rotated, the new corners are also transparant. Just the black lines around the original square (which is rotated) are annoying me.
I use this code:
$angle = -100;
header('Content-type: image/png');
$image = 'http://mapning.com/img/plane.png';
$file = imagecreatefrompng($image);
$rotate = imagerotate($file, $angle, 0);
imageSaveAlpha($rotate, true);
ImageAlphaBlending($rotate, false);
$transparentColor = imagecolorallocatealpha($rotate, 200, 200, 200, 127);
imagefill($rotate, 0, 0, $transparentColor);
imagepng($rotate);
I found my answer here:
http://ru2.php.net/manual/en/function.imagerotate.php#47985
I think better use imagick
Here is an extension for PHP
Or if you want with GD see here
http://ru2.php.net/manual/en/function.imagerotate.php#46338

Why cant I make the background of a png transparent after rotating it with php?

I tried literally all day yesterday trying to figure this out. I rotate an image via imagerotate(). I get a black background where the image isn't covering anymore. I have tried everything i cant think of to make that background transparent..
here is my current code..
function rotate($degrees) {
$image = $this->image;
imagealphablending($image, false);
$color = imagecolorallocatealpha($image, 0, 0, 0, 127);
$rotate = imagerotate($image, $degrees, $color);
imagecolortransparent($rotate, $color);
imagesavealpha($image, true);
$this->image = $rotate;
}
I am really starting to get ticked off. Can someone show me some working code? please?
Could it be something wrong with my WAMP server and dreamweaver? because i even tried this.. http://www.exorithm.com/algorithm/view/rotate_image_alpha and it still puts out either a black or white background..
Try setting imagesavealpha() on your rotated image.
Currently you are running imagesavealpha() on your original image. [ eg. imagesavealpha($image, true); ]
Instead you want to run imagesavealpha() on the rotated image and then set $this->image... try:
...
$rotate = imagerotate($image, $degrees, $color);
imagecolortransparent($rotate, $color);
imagesavealpha($rotate, true); // <- using $rotate instead of $image
$this->image = $rotate;
}

Categories