Imagerotate with alpha color not working

I have a strange situation.

it looks like the background is not always transparent, but to some extent it is broken ...

good one 30 degree

not good

here is the code:

$angle = !empty($_GET['a']) ? (int)$_GET['a'] : 0;

$im = imagecreatefromgif(__DIR__ . '/track/direction1.gif');
imagealphablending($im, false);
imagesavealpha($im, true);

$transparency = imagecolorallocatealpha($im, 0, 0, 0, 127);
$rotated = imagerotate($im, $angle, $transparency);

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

imagepng($rotated);
imagedestroy($rotated);

imagedestroy($im);
header('Content-Type: image/png');

I just can’t understand what is happening ... did I miss somth?

EDIT1

added that func:

if(!function_exists('imagepalettetotruecolor'))
{
    function imagepalettetotruecolor(&$src)
    {
        if(imageistruecolor($src))
        {
            return true;
        }

        $dst = imagecreatetruecolor(imagesx($src), imagesy($src));
        $black = imagecolorallocate($dst, 0, 0, 0);
        imagecolortransparent($dst, $black);

        $black = imagecolorallocate($src, 0, 0, 0);
        imagecolortransparent($src, $black);

        imagecopy($dst, $src, 0, 0, 0, 0, imagesx($src), imagesy($src));
        imagedestroy($src);

        $src = $dst;

        return true;
    }
}

but now stuck with the fact that the square does not want to be transparent ....

almost

+4
source share
2 answers

imagerotatepoorly implemented; I notice that often rounds errors / cuts edges. If necessary, you can use a transparent 24-bit PNG image instead of a transparent GIF (PNG supports alpha transparency, which means that the edges will mix well with the HTML background color).

, :

<?php
$angle = (int) $_GET['a'];
$source = imagecreatefrompng(__DIR__ . DIRECTORY_SEPARATOR . 'direction1.png');
$rotation = imagerotate($source, $angle, imageColorAllocateAlpha($source, 0, 0, 0, 127));
imagealphablending($rotation, false); // handle issue when rotating certain angles
imagesavealpha($rotation, true);      // handle issue when rotating certain angles
header('Content-type: image/png');
imagepng($rotation);
imagedestroy($source);
imagedestroy($rotation);

:

result 0 to 359 degree

, CSS-?

img:nth-child(2) {
  transform: rotate(45deg);
}
img:nth-child(3) {
  transform: rotate(90deg);
}
img:nth-child(4) {
  transform: rotate(135deg);
}
<img src="http://i.stack.imgur.com/oZlZ9.png">
<img src="http://i.stack.imgur.com/oZlZ9.png">
<img src="http://i.stack.imgur.com/oZlZ9.png">
<img src="http://i.stack.imgur.com/oZlZ9.png">
+3

imagecreatefromgif() , ( GIF ). , , , $transparency, .

$im . imagepalettetotruecolor() . PHP 5.5. , . documentation, , ( ).

+1

All Articles