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

1
  • 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
    Commented Dec 18, 2016 at 3:59

1 Answer 1

0

Pass a transparent background color for imagerotate:

$transparent = imagecolortransparent($img, $color);
$img = imagerotate($img, 45, $transparent);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.