PHP的验证码:
1:生成图片:imagecreatetruecolor(创建一张增彩色图片)
$img=imagecreatetruecolor(100,30);
2:生产颜色:
(1):当第一次调用生成颜色的方法,是生成背景颜色
$bg=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
(2):第二次调用这个方法,是可以生成图片上面的文字或者其他样式的颜色
$te=imagecolorallocate($img,186,192,220);
//186,192,220 三原色:(红,绿,蓝)
3:在图片上面生成文字(第一个值是图片,第二个值是字体的大小,第三个值是横坐标,第四一个值是纵坐标,第五一个值是生成的字,
第六一个值是颜色)
imagestring($img,rand(5,10),rand(3,70),rand(3,15),$rand,$te);
4:要把php当成图片输出,必须给文件一个头申明
header("Content-type:image/jpeg");
5:最终生成图片(imagejpeg/imagejpng)
imagejpeg($img);
6:rand():(生产随机数)
例:生成四种随机数
<?php
$arr =
array(
"A","B","C","D","E","F","G","H","J","K","L","M",
"N","P","Q","R","S","T","U","V","W","X","Y","Z",
"0",
"1","2","3","4","5","6","7","8","9");
$rand="";
for($i=0;$i<4;$i++){
$rand .= $arr[rand(0,count($arr)-1)];
//生成随机数
}
echo $rand;
?>
7:ob_clean()这个函数的作用就是用来丢弃输出缓冲区中的内容,如果你的网站有许多生成的图片类文件,那么想要访问正确,就要经常清除缓冲区。