Background transparency in the image ()

Over the past 2 days, I have been trying to add transversality to the background after rotating the image using the imagerotate () function of PHP-GD.

But, to my great disappointment, it does not work at all.

It just highlights the black background behind it.

Here is my code -

 $ patchImageS = 'image.png';  // the image to be patched over the final bg
 $ patchImage = imagecreatefrompng ($ patchImageS);  // resource of image to be patched
 $ patchImage = imagerotate ($ patchImage, 23, 0, 0);
 imagepng ($ patchImage, 'tt.png');

I tried to change the passed parameters in the function

imagerotate ($ patchImage, 23, 5, 0);

imagerotate ($ patchImage, 23, 0, 5);

Any help would be greatly appreciated.

+4
source share
2 answers

After a few 99% of the ready-made answers, here is the solution I found:

// Create, or create from image, a PNG canvas $png = imagecreatetruecolor($width, $height); // Preserve transparency imagesavealpha($png , true); $pngTransparency = imagecolorallocatealpha($png , 0, 0, 0, 127); imagefill($png , 0, 0, $pngTransparency); // Rotate the canvas including the required transparent "color" $png = imagerotate($png, $rotationAmount, $pngTransparency); // Set your appropriate header header('Content-Type: image/png'); // Render canvas to the browser imagepng($png); // Clean up imagedestroy($png); 

The key here is to include your imagecolorallocatealpha () in the imagerotate () call ...

+5
source

look for imagesavealpha () in the php documentation - I think this is what you are looking for.

EDIT: here's an example:

 $png = imagecreatefrompng('./alphachannel_example.png'); // Do required operations $png = imagerotate($png, 23, 0, 0); // Turn off alpha blending and set alpha flag imagealphablending($png, false); imagesavealpha($png, true); // Output image to browser header('Content-Type: image/png'); imagepng($png); imagedestroy($png); 
+4
source

Source: https://habr.com/ru/post/1307718/


All Articles