从最近2天开始,我试图在使用imagerotate()PHP-GD函数旋转图像后为后台添加transperancy.
但是,令我非常失望的是,它根本不起作用.
它只是在它后面发出黑色背景.
这是我的代码 -
$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');
我试图将函数中传递的参数更改为
imagerotate($ patchImage,23,5,0);
imagerotate($ patchImage,23,0,5);
任何帮助将受到高度赞赏.
经过多次99%的答案后,我找到了解决方案:
// 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);
Run Code Online (Sandbox Code Playgroud)
这里的关键是在imagerotate()调用中包含你的imagecolorallocatealpha()...