Changing the opacity of an image in PHP

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!

+4
source share
3 answers

You just need to use imagefilter with IMG_FILTER_COLORIZE

 $image = imagecreatefrompng('my_image.png'); $opacity = 0.5; imagealphablending($image, false); // imagesavealpha can only be used by doing this for some reason imagesavealpha($image, true); // this one helps you keep the alpha. $transparency = 1 - $opacity; imagefilter($image, IMG_FILTER_COLORIZE, 0,0,0,127*$transparency); // the fourth parameter is alpha header('Content-type: image/png'); imagepng($image); 

imagealphablending , I think, is intended for drawing, so you do not want to use it. Maybe I'm wrong. We must look at him :)

If you want to use a percentage, you can calculate $opacity accordingly.

+8
source

There is no direct way to change the opacity using the DG function (actually there is ). But this can be done using pixel manipulation:

 /** * @param resource $imageSrc Image resource. Not being modified. * @param float $opacity Opacity to set from 0 (fully transparent) to 1 (no change) * @return resource Transparent image resource */ function imagesetopacity( $imageSrc, $opacity ) { $width = imagesx( $imageSrc ); $height = imagesy( $imageSrc ); // Duplicate image and convert to TrueColor $imageDst = imagecreatetruecolor( $width, $height ); imagealphablending( $imageDst, false ); imagefill( $imageDst, 0, 0, imagecolortransparent( $imageDst )); imagecopy( $imageDst, $imageSrc, 0, 0, 0, 0, $width, $height ); // Set new opacity to each pixel for ( $x = 0; $x < $width; ++$x ) for ( $y = 0; $y < $height; ++$y ) { $pixelColor = imagecolorat( $imageDst, $x, $y ); $pixelOpacity = 127 - (( $pixelColor >> 24 ) & 0xFF ); if ( $pixelOpacity > 0 ) { $pixelOpacity = $pixelOpacity * $opacity; $pixelColor = ( $pixelColor & 0xFFFFFF ) | ( (int)round( 127 - $pixelOpacity ) << 24 ); imagesetpixel( $imageDst, $x, $y, $pixelColor ); } } return $imageDst; } $source = imagecreatefrompng( 'my_image.png' ); $newImage = imagesetopacity( $source, 0.5 ); imagedestroy( $source ); header( 'Content-Type: image/png' ); imagepng( $newImage ); imagedestroy( $newImage ); 
+4
source

Have you tried setting the alpha blend to true?

imagealphablending($newImage,true);

0
source

All Articles