php gd库好用吗,php gd库运用

1.gd库

php gd库的功能比较强大,主要用来做图片验证码生成,绘制图片,处理图片等等。下面是简单的gd库绘制图片验证码

代码示例

$size_x = 100;

$size_y = 100;

if(!isset($_GET['code'])) {

$code = 'unknown';

}

$space_per_char = $size_x /(strlen($code)+1);

$img = imagecreatetruecolor($size_x,$size_y);

$background = imagecolorallocate($img,255,255,255);//白色

$border = imagecolorallocate($img,128,128,128);

$colors[] = imagecolorallocate($img,128,64,192);

$colors[] = imagecolorallocate($img,192,64,128);

$colors[] = imagecolorallocate($img,108,192,64);

imagefilledrectangle($img, 1, 1,$size_x - 2, $size_y - 2, $background);

imagerectangle($img, 1, 1,$size_x - 2, $size_y - 2, $border);

for($i = 0;$i < strlen($code);$i++) {

$color = $colors[$i % count($colors)];

imagettftext(

$img,

28 +rand(0,8),

-20 +rand(0,40),

($i + 0.3) * $space_per_char,

50 + rand(0,10),

$color,

'font/arial.ttf',

$code{$i}

);

imageantialias($img,true);

}

header('Content-type:image/png');

imagepng($img);

结果

bVQr3z?w=950&h=410

即,known就绘制成图片。可以验证了

2.jpeg图片生成并保存在本地

$size_x = 100;

$size_y = 100;

if(!isset($_GET['code'])) {

$code = 'unknown';

}

$space_per_char = $size_x /(strlen($code)+1);

$img = imagecreatetruecolor($size_x,$size_y);

$background = imagecolorallocate($img,255,255,255);//白色

$border = imagecolorallocate($img,128,128,128);

$colors[] = imagecolorallocate($img,128,64,192);

$colors[] = imagecolorallocate($img,192,64,128);

$colors[] = imagecolorallocate($img,108,192,64);

for ($i = 0;$i <= 1;$i++) {

$x1 = rand(5,$size_x - 5);

$y1 = rand(5,$size_y - 5);

$x2 = $x1 - 4 + rand(0, 8);

$y2 = $y1 - 4 + rand(0, 8);

imageline($img, $x1, $y1, $x2, $y2, $colors[rand(0,count($colors)-1)]);

}

header('Content-Type:image/jpeg');

imagejpeg($img,'test.jpeg');

备注:在例子中还需要字体插件arial.ttf 可以在网上下载,并放到本地文件夹调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值