Background transperancy in imagerotate()

筅森魡賤 提交于 2019-12-01 18:32:07

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...

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);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!