Skip to content

Instantly share code, notes, and snippets.

@QETHAN
Created August 31, 2014 07:51
Show Gist options
  • Save QETHAN/543d9c7655525fcc68ba to your computer and use it in GitHub Desktop.
Save QETHAN/543d9c7655525fcc68ba to your computer and use it in GitHub Desktop.
php 随机验证码
<?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, 40) , $black);
imagesetpixel($img, rand(0, 100) , rand(0, 40) , $green);
}
//输出验证码
header("content-type: image/png");
imagepng($img);
imagedestroy($img);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment