I am trying to change the opacity of an image using GD. Almost all the solutions I found are similar to the code below, where you create a white transparent background and combine it with your image.
But this does not make the image transparent, the image becomes brighter, you cannot view it.
So my question is: how do I change the opacity of an image so that you can view it? Or am I doing something wrong with this code?
//Create an image with a white transparent background color $newImage = ImageCreateTruecolor(300, 300); $bg = ImageColorAllocateAlpha($newImage, 255, 255, 255, 127); ImageFill($newImage, 0, 0, $bg); //Get the image $source = imagecreatefrompng('my_image.png'); $opacity = 50; //Merge the image with the background ImageCopyMerge($newImage, $source, 0, 0, 0, 0, 300, 300, $opacity); header('Content-Type: image/png'); imagepng($newImage); imagedestroy($newImage);
Thanks!
source share