4

I have a strange situation.

it looks like background not always transparent, but on some degree it 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');

just can`t understand what is going on... am i missed 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 withthat square do not want to be transparent....

almost

Salman A
  • 229,425
  • 77
  • 398
  • 489
Subdigger
  • 2,051
  • 3
  • 19
  • 41

2 Answers2

3

The imagerotate is poorly implemented; 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).

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:

result 0 to 359 degree

As an alternate, may I suggest 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">
Salman A
  • 229,425
  • 77
  • 398
  • 489
1

imagecreatefromgif() creates a paletted image and not a true color image (because this is how the GIF format encodes the image). On images with palette the transparency work different than on true color images and the value you computed for $transparency doesn't help.

A solution is to convert $im to true color before rotating it. The function imagepalettetotruecolor() does this. It is available since 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).

axiac
  • 56,918
  • 8
  • 77
  • 110