php两款生成图形验证码的代码
验证码在php中是经常会用到了,下面我们为你提供了一些关于php验证码生成类与程序,直接复制下去就可以直接用的,下面看代码.
<?php
session_start();
Header("Content-type: image/gif");
class SecurityCode {
private $codes = '';
function __construct() {
$code = '0-1-2-3-4-5-6-7-8-9-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z';
$codeArray = explode('-', $code);
shuffle($codeArray);
$this->codes = implode('', array_slice($codeArray, 0, 4));
}
public function CreateImg() {
$_SESSION['check_pic'] = $this->codes;
$img = imagecreate(70, 25);
imagecolorallocate($img, 222, 222, 222);
$testcolor1 = imagecolorallocate($img, 255, 0, 0);
$testcolor2 = imagecolorallocate($img, 51, 51, 51);
$testcolor3 = imagecolorallocate($img, 0, 0, 255);
$testcolor4 = imagecolorallocate($img, 255, 0, 255);
for ($i = 0; $i < 4; $i++) {
imagestring($img, rand(5, 6) , 8 + $i * 15, rand(2, 8) , $this->codes[$i], rand(1, 4));
}
imagegif($img);
}
}
$code = new SecurityCode();
$code->CreateImg();
$code = NULL;
?>//代码二
<?php
session_start();
for ($i = 0; $i < 4; $i++) {
$rand.= dechex(rand(1, 15));
} //开源代码phprm.com
$_SESSION[check_pic] = $rand;
//echo $_SESSION[check_pic];
// 设置图片大小">图片大小
$im = imagecreatetruecolor(100, 30);
// 设置颜色
$bg = imagecolorallocate($im, 0, 0, 0);
$te = imagecolorallocate($im, 255, 255, 255);
// 把字符串写在图像左上角
imagestring($im, rand(5, 6) , rand(25, 30) , 5, $rand, $te);
// 输出图像
header("Content-type:image/jpeg");
imagejpeg($im);文章网址:http://www.phprm.com/tuxiang/fs4434.html
随意转载^^但请附上教程地址。