用GD + PHP旋转图像时出现边框 [英] A border appears when rotating image with gd + php

查看:68
本文介绍了用GD + PHP旋转图像时出现边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gd + php旋转图像时出现边框,我不知道我做错了什么.

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

推荐答案

imagerotate传递透明的背景色:

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

这篇关于用GD + PHP旋转图像时出现边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆