A border appears when rotating image with gd + php

情到浓时终转凉″ 提交于 2020-01-05 04:57:13

问题


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 :-/


回答1:


Pass a transparent background color for imagerotate:

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


来源:https://stackoverflow.com/questions/41205029/a-border-appears-when-rotating-image-with-gd-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!