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>';
Related
I'm trying to create a function that receives a PNG image and returns it with inverted colors. However, I'm having trouble with the transparency/alpha channel: basically, transparent pixels are coming back as either black or white, and thus transparency is not retained.
This is an example of a source image (which will of course vary), a PNG with a transparent background:
I would like to invert those colors while preserving transparency/alpha channel, like a CTRL+I in Photoshop. I'm using GD imagefilter function with the IMG_FILTER_NEGATE parameter
With the code:
$im = imagecreatefrompng($image_file);
imagefilter($im, IMG_FILTER_NEGATE);
header('image/png');
imagepng($im,NULL,0);
But it yields:
As you can see, transparent pixels turned black.
I then tried adding the alpha channel to the function imagecreatefrompng (like this):
$im = imagecreatefrompng($image_file);
imagealphablending($im, false);
imagesavealpha($im, true);
imagefilter($im, IMG_FILTER_NEGATE);
header('image/png');
imagepng($im,NULL,0);
But now I get white instead of black:
Now the issue seems to happen after the imagefilter function is applied. For example, if I run this code:
$im = imagecreatefrompng($image_file);
imagealphablending($im, false);
imagesavealpha($im, true);
//imagefilter($im, IMG_FILTER_NEGATE);
header('image/png');
imagepng($im,NULL,0);
The output image retains its transparent background, while remaining identical to the original image.
How can I invert colors of transparent PNGs without losing the transparent background?
NOTE: this is not the same question as imagecreatefrompng() Makes a black background instead of transparent? - the inversion step is the tricky part here
Here's what worked for me, dealing with transparent background and inverting image. Note that I'm using base64.
$im = imagecreatefromstring(base64_decode(str_replace('data:image/png;base64,', '', $base64)));
$width = imagesx($im);
$height = imagesy($im);
$dest_image = imagecreatetruecolor($width, $height);
imagealphablending($dest_image, FALSE);
imagesavealpha($dest_image, TRUE);
imagefilter($im, IMG_FILTER_NEGATE);
imagecopyresampled($dest_image, $im, 0, 0, 0, 0, $width, $height, $width, $height);
//optional output back to base64 or use imagepng with destination
ob_start();
imagepng($dest_image);
$contents = ob_get_contents();
ob_end_clean();
$base64 = 'data:image/png;base64,'.base64_encode($contents);
I'm trying to make a image transparent using GD library from PHP but running following code, only a portion will be transparent.
$image = imagecreatefrompng("$second");
imagealphablending($image, false);
$col_transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $col_transparent); // set the transparent colour as the background.
imagecolortransparent ($image, $col_transparent); // actually make it transparent
imagesavealpha($image, TRUE);
header( 'Content-Type: image/png' );
imagepng($image);
Here you have original image: https://postimg.org/image/y68nw57z1/
Here it's the resulting image: https://postimg.org/image/o4n3t6ic7/
As you can see, exists parts from the resulting image that remain white.
How i can resolve this?
You are flood-fill replacing the white pixels of your image but that won't work on pixels that are completely enclosed by non-white pixels (as in any paint program). Instead you can modify the definition of the colour white to make it transparent:
$image = imagecreatefrompng($second);
imagetruecolortopalette($image, false, 255);
$index = imagecolorclosest($image, 255, 255, 255); // find index of white.
imagecolorset($image, $index, 0, 0, 0, 127); // replace white with transparent black.
header('Content-Type: image/png');
imagepng($image);
I am trying to resize image canvas (as in Photoshop) by adding transparency around it. Somehow added part of the image is always black.
if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
{
$create = 'imagecreatetruecolor';
$copy = 'imagecopyresampled';
}
else
{
$create = 'imagecreate';
$copy = 'imagecopyresized';
}
$dst_img = $create($this->width, $this->height);
if ($this->image_type == 3) // png we can actually preserve transparency
{
//teorethicaly image should be transparent?
$trans_colour = imagecolorallocatealpha($dst_img, 0, 0 ,0, 127);
imagefill($dst_img, 0, 0, $trans_colour);
imagealphablending($dst_img, FALSE);
imagesavealpha($dst_img, TRUE);
}
$copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
If I remove $copy and save new image only, it is transparent but if I merge both images the background is always black:
How I can have transparent background in that situation?
Thanks in advance!
http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/ http://www.phpfreaks.com/forums/index.php?topic=232090.0 How to preserve transparency when resizing PNG using Perl and GD.
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>';
I have the following problem.
I have a PNG file (http://meteopage.com/beta/image.png), which is already partially transparent and I would like to make the "pink" border also transparent.
I have read some of the instructions here and came up with a code, which does replace the border with transparent point, but all the rest, which was already transparent, turns into black: http://meteopage.com/beta/radar2.php
My code looks like this:
<?php
$file="image.png";
$im = imagecreatefrompng($file);
imagealphablending($im, false);
$new = imagecolorclosest($im, 255, 0, 255);
imagecolortransparent($im, $new);
imagesavealpha($im, true);
header('Content-type: image/png');
imagepng($im);
?>
I tried adding those imagesavealpha and imagealphablending commands, but still no luck, it is black as you can see in the link Ive attached. Would anyone know how to maintain the original transparency and just "add" those pink pixels to it?
Your image is palette-based, not true colour, so the calls to imagealphablending() and imagesavealpha() aren't needed.
The simplest way to do this is to replace the pink colour in the palette-index with a transparent one, like so:
$file = 'image.png';
$im = imagecreatefrompng($file);
$new = imagecolorclosest($im, 255, 0, 255);
imagecolorset($im, $new, 255, 0, 255, 127);
header('Content-type: image/png');
imagepng($im);
Note that this requires PHP >= 5.4 otherwise imagecolorset() won't accept the alpha parameter.