php 图形验证码 浏览器,PHP用GD库绘制图片,制作验证码浏览器不能输出

1,代码如下:

function fillRandomString(){

//生成验证码

$char = array_merge(range(0, 9),range("a", "z"),range("A", "Z"));

//讲字符串$char打乱

$luan = str_shuffle(implode("", $char));

//从字符串中随机取四位

return  substr($luan, 0,4 );

}

//创建画布

header(‘content-type:image/png‘);

$height = 20;

$width = 80;

$image = imagecreate($width, $height);

$black = imagecolorallocate($image, 0, 0, 0);

$white = imagecolorallocate($image, 255, 255, 255);

//画图片矩形

imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white);

//填充字符串

imagestring($image, 20, 20,  4, fillRandomString(), $black);

//输出图片

imagepng($image);

//销毁图片

imagedestroy($image);

但是在浏览器上输出显示不了,原因是在向浏览器输出之前没有清理缓存

在header()之前加入代码 ob_clean();即可

原文:http://sourliki.blog.51cto.com/4838492/1557635

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值