简体   繁体   English

带有alpha颜色的imagerotate不起作用

[英]imagerotate with alpha color not work

I have a strange situation. 我有一个奇怪的情况。

it looks like background not always transparent, but on some degree it broken... 它看起来像背景并不总是透明的,但在某种程度上它打破了......

好一个30度

不好

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

just can`t understand what is going on... am i missed somth? 只是不明白发生了什么...我错过了什么?

EDIT1 EDIT1

added that func: 添加了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 withthat square do not want to be transparent.... 但现在坚持这个广场不想透明....

几乎

The imagerotate is poorly implemented; imagerotate很难实现; I notice rounding off errors/cut edges often. 我经常注意到错误/削减边缘。 If you must, you can use a 24bit transparent PNG image instead of transparent GIF (PNG supports alpha transparency which means the edges will be blended nicely with HTML background color). 如果必须,您可以使用24位透明PNG图像而不是透明GIF(PNG支持Alpha透明度,这意味着边缘将与HTML背景颜色很好地融合)。

The function has transparency issues and the workaround is to add two extra lines: 该功能有透明度问题,解决方法是添加两行:

<?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到359度

As an alternate, may I suggest CSS transform ? 作为替代,我可以建议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"> 

imagecreatefromgif() creates a paletted image and not a true color image (because this is how the GIF format encodes the image). imagecreatefromgif()创建一个调色板图像而不是真彩色图像(因为这是GIF格式对图像进行编码的方式)。 On images with palette the transparency work different than on true color images and the value you computed for $transparency doesn't help. 在具有调色板的图像上,透明度与真彩色图像不同,并且您为$transparency计算的值无济于事。

A solution is to convert $im to true color before rotating it. 解决方案是在旋转之前将$im转换$im真彩色。 The function imagepalettetotruecolor() does this. 函数imagepalettetotruecolor()执行此操作。 It is available since PHP 5.5. 从PHP 5.5开始提供。 If you are stuck with an older version then you need to implement it yourself. 如果您遇到旧版本,那么您需要自己实现它。 Check the last example on the documentation page, it is already implemented there and it takes care of the transparency too (it has a couple of minor bugs you will encounter when you run it). 检查文档页面上的最后一个示例,它已经在那里实现,它也负责透明度(它有一些你在运行它时会遇到的小错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM