First, I resize the image while saving transparency:
/*
all the classic routine etcetera:
$canvas = imagecreatefrom[png|gif|jpeg]();
$resize = imagecreatetruecolor();
*/
if($blending){
$transparentIndex = imagecolortransparent($canvas);
if($transparentIndex >= 0){
#GIF
imagepalettecopy($canvas, $resize);
imagefill($resize, 0, 0, $transparentIndex);
imagecolortransparent($resize, $transparentIndex);
imagetruecolortopalette($resize, true, 256);
}else{
#PNG
imagealphablending($resize, false);
imagesavealpha($resize, true);
$transparent = imagecolorallocatealpha($resize, 255, 255, 255, 127);
imagefill($resize, 0, 0, $transparent);
}
}
imagecopyresampled($resize, $canvas, 0, 0, 0, 0, $nx, $ny, $x, $y);
// image[png|gif|jpeg]... (image gets saved)
Then, I want to apply grayscale filter to that previously saved image (within a new function):
/*
classic routine again:
$canvas = imagecreatefrom[png|gif|jpeg]()
*/
if($blending){
imagealphablending($canvas, false);
imagesavealpha($canvas, true);
}
imagefilter($canvas, IMG_FILTER_GRAYSCALE);
/*
This fully filters PNG's to Grayscale while saving transparency,
but for GIF, black background is added to my grayscaled picture,
plus, the picture isn't fully grayscale (more like gets high-contrasted with acidic colors).
*/
// image[png|gif|jpeg]
What would be the fix, to preserve transparency when applying IMG_FILTER_GRAYSCALE to gif
?
What would be the fix, in order to transform gif
to grayscale while saving the transparency? (Question revised due to answer provided by @Pierre)
Thanks in advance!