Since last 2 days, I was trying to add transperancy to the background after rotating an image using imagerotate() PHP-GD function.
But, to my great disappointment, it's not working at all.
It's just giving out a black background behind it.
Here's 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 parameters being passed in function to
imagerotate($patchImage, 23, 5, 0);
imagerotate($patchImage, 23, 0, 5);
Any help would be highly appreciated.
After a number of 99% finished answers, here's the solution I've 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 your imagerotate() call...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With