1

A border appears when rotating image with gd + php and I do not know what I did wrong.

<?php
$size = 900;
$img = imagecreate($size, $size);

imagesavealpha($img, true);
$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $color);

$lines_color = imagecolorallocate($img, 0, 0, 0);

imageline($img, ($size * 0.50), ($size * 0.00), ($size * 0.50), ($size * 1.00), $lines_color);
imageline($img, ($size * 0.20), ($size * 0.00), ($size * 0.80), ($size * 1.00), $lines_color);
imageline($img, ($size * 0.00), ($size * 0.20), ($size * 1.00), ($size * 0.80), $lines_color);
imageline($img, ($size * 0.00), ($size * 0.50), ($size * 1.00), ($size * 0.50), $lines_color);
imageline($img, ($size * 0.00), ($size * 0.80), ($size * 1.00), ($size * 0.20), $lines_color);
imageline($img, ($size * 0.20), ($size * 1.00), ($size * 0.80), ($size * 0.00), $lines_color);

$img = imagerotate($img, 45, 1);
imagesavealpha($img, true);
$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $color);

header("Content-Type: image/png");
imagepng($img, 'demo.png');

The result: a magic border added :-/

Image png result

e-info128
  • 3,727
  • 10
  • 40
  • 57
  • Why are you using black for all your backgrounds? Do you need it to be black? if do, then why are you setting the transparency to full(127) ? Why do you fill the background again after rotating the image? – EhsanT Dec 18 '16 at 03:59

1 Answers1

0

Pass a transparent background color for imagerotate:

$transparent = imagecolortransparent($img, $color);
$img = imagerotate($img, 45, $transparent);
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60