Background transperancy in imagerotate()

后端 未结 2 765
庸人自扰
庸人自扰 2021-01-19 04:58

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, i

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-19 05:48

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

提交回复
热议问题