您的位置:首页 > 编程语言 > PHP开发

使用PHP实现自动生成验证码

2017-01-04 14:09 549 查看
<?php

$img = imagecreatetruecolor(100, 40);

$black = imagecolorallocate($img, 0x00, 0x00, 0x00);

$green = imagecolorallocate($img, 0x00, 0xFF, 0x00);

$white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);

imagefill($img,0,0,$white);

//生成随机的验证码

$code = '';

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

    $code .= rand(0, 9);

}

imagestring($img, 5, 10, 10, $code, $black);

//加入噪点干扰

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

  imagesetpixel($img, rand(0, 100) , rand(0, 100) , $black); 

  imagesetpixel($img, rand(0, 100) , rand(0, 100) , $green);

}

//输出验证码

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

imagepng($img);

imagedestroy($img);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 验证码