Php 使用alpha颜色旋转图像无效

Php 使用alpha颜色旋转图像无效,php,image,gd,dynamic-image-generation,Php,Image,Gd,Dynamic Image Generation,我有一个奇怪的情况 看起来背景并不总是透明的,但在某种程度上它是破碎的 代码如下: $angle = !empty($_GET['a']) ? (int)$_GET['a'] : 0; $im = imagecreatefromgif(__DIR__ . '/track/direction1.gif'); imagealphablending($im, false); imagesavealpha($im, true); $transparency = imagecolorallocat

我有一个奇怪的情况

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

代码如下:

$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;
    }
}
但是现在被困在那个正方形里,我不想变得透明

imagecreatefromfromgif()
创建调色板图像而不是真彩色图像(因为这是GIF格式对图像进行编码的方式)。在使用调色板的图像上,透明度的工作方式不同于真彩色图像,并且为
$transparency
计算的值没有帮助

解决方案是在旋转前将
$im
转换为真彩色。该函数执行此操作。它是从PHP5.5开始提供的。如果您被旧版本卡住了,那么您需要自己实现它。查看页面上的最后一个示例,它已经在那里实现了,并且它还负责透明度(运行它时会遇到一些小错误)。

实现得很差;我经常注意到舍入错误/切边。如果必须,您可以使用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);