Rotate an image with a transparent color

I was going to rotate an image with transparent color using php gd. However, after rotation, the transparent color in the image is no longer transparent, and the background is also not transparent. Here is my code.

$im = imagecreatefromgif('/images/80-2.gif');
$rotate = imagerotate($im,30,imagecolorallocatealpha($im, 0, 0, 0, 127));
imagegif($rotate,'/images/rotate.gif');
imagedestroy($im);
imagedestroy($rotate);

      

Can anyone help me get it to work? Thank.

+3


source to share


2 answers


to preserve transparency in your images you need to use two settings that can be done by calling these functions immediately after creating the gd resource



imagealphablending( $im, false );
imagesavealpha( $im, true );

      

+3


source


The work suggested by alex.michel doesn't work for me with gif: the background is transparent, but not the alpha of my original gif. It is blue like graphic paper. About mishu's solution, it won't work for gifs (quote from php.net manual):

imagesavealpha () sets a flag to try to preserve the full alpha channel (as opposed to transparency with one color) when saving a PNG image.

For png, I use this, it works great:



    $source = imagecreatefrompng($image);
    imagealphablending($source, false);
    imagesavealpha($source, true);
    $rotated = imagerotate($source, $angle, imageColorAllocateAlpha($source, 0, 0, 0, 127));
    imagealphablending($rotated, false);
    imagesavealpha($rotated, true);
    imagepng($rotated, $image);

      

I'm still looking for something that works for a gif ...

0


source







All Articles