Background transperancy in imagerotate()

后端 未结 2 766
庸人自扰
庸人自扰 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...

    0 讨论(0)
  • 2021-01-19 05:52

    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);
    
    0 讨论(0)
提交回复
热议问题