imagerotate()中的背景透明性 [英] Background transperancy in imagerotate()
问题描述
最近两天以来,我一直在尝试使用imagerotate()PHP-GD函数旋转图像后向背景添加透明度.
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.
这是我的代码-
$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, 5, 0);
imagerotate($ patchImage,23,0,5);
imagerotate($patchImage, 23, 0, 5);
任何帮助将不胜感激.
推荐答案
在完成99%的回答后,这是我找到的解决方案:
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);
关键是要在imagerotate()调用中包含imagecolorallocatealpha()...
The key here is to include your imagecolorallocatealpha() in your imagerotate() call...
这篇关于imagerotate()中的背景透明性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!