php – 使用alpha颜色的imagerotate不起作用

我有一个奇怪的情况.

它看起来像背景并不总是透明的,但在某种程度上它打破了……

这是代码:

$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');

只是不明白发生了什么…我错过了什么?

EDIT1

添加了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;
    }
}

但现在坚持这个广场不想透明….

解决方法:

imagerotate执行不力;我经常注意到错误/削减边缘.如果必须,您可以使用24位透明PNG图像而不是透明GIF(PNG支持Alpha透明度,这意味着边缘将与HTML背景颜色很好地融合).

该功能有透明度问题,解决方法是添加两行:

<?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);

结果:

作为替代,我可以建议CSS transform吗?

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">
上一篇:php – 让imagegrabscreen工作


下一篇:使用PHP GD Image Library着色和交换颜色?