Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get a transparent background after rotaing a png image with php?

Tags:

php

rotation

So I have png image and I rotate it but i get a black background.. or if i do the color code ofr white i get white.. I tried doing this..

$trans = imagecolorallocatealpha(image, 0, 0, 0, 127);
imagerotate($image, $degree, $trans)

i have also tried..

$trans = imagecolorallocatealpha($image, 255, 255, 255, 127);

Can someone help me out?

here is my code.. if i change allocatealpha to 0, 0, 255, 0 then it goes blue. but with 0, 0, 0, 127 its still black.

function rotate($degrees) {
$image = $this->image;
imagealphablending($image, false);
$color = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($this->image, 0, 0, $color);
$rotate = imagerotate($image, $degrees, $color);
imagesavealpha($image, TRUE);
$this->image = $rotate;

like image 821
Chris Avatar asked Nov 10 '10 20:11

Chris


People also ask

How do I make the background of a PNG transparent in HTML?

Transparency is not done in HTML, but is a part of the image itself. The browser will see the image as a PNG and display it as a PNG automatically. To add transparency to the image, you will have to edit the file with a graphics editor like Photoshop. Save this answer.

How do I remove a black background from a PNG?

When a PNG image with a transparent background is selected from the Recent FIle selector and appears with a black background, the black background can be removed by re-uploading the image as a New File each time you use the image.


1 Answers

$destimg = imagecreatefromjpeg("image.png");
$transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127);
$rotatedImage = imagerotate($destimg, 200, $transColor);
imagesavealpha($rotatedImage, true);
imagepng($rotatedImage,"rotated.png");
like image 170
Michael L Watson Avatar answered Nov 03 '22 18:11

Michael L Watson