1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php
session_start(); $arr =
array
(
‘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‘
,
‘0‘
,
‘1‘
,
‘2‘
,
‘3‘
,
‘4‘
,
‘5‘
,
‘6‘
,
‘7‘
,
‘8‘
,
‘9‘ ); $rand =
""
; for
(
$i
=1;
$i
<=4;
$i
++){ <span style=
"background-color: rgb(192, 192, 192);"
>
//随机产生4个由不同字母与数字组合成的验证码</span>
$rand .=
$arr
[rand(0,
count
(
$arr
)-1)]; } $_SESSION
[
‘check_pic‘
] =
$rand
; <span style=
"background-color: rgb(192, 192, 192);"
>
//生成图片</span> $im = <span style=
"color: rgb(255, 0, 0);"
>imagecreatetruecolor</span>(100,30); <span style=
"background-color: rgb(192, 192, 192);"
>
//生成颜色,当第一次调用生成颜色的方法,是生成背景颜色</span> $bg = <span style=
"color: rgb(255, 0, 0);"
>imagecolorallocate</span>(
$im
,0,0,0); <span style=
"background-color: rgb(192, 192, 192);"
>
//第二次调用这个方法,是可以生成图片上面的文字或其他样式的颜色 </span>
$te =<span style=
"color: rgb(255, 0, 0);"
> imagecolorallocate</span>(
$im
,255,255,255);
<span style=
"background-color: rgb(192, 192, 192);"
>
//在图片上面生成文字</span> <span style=
"color: rgb(255, 0, 0);"
>imagestring</span>(
$im
,rand(1,5),rand(3,70),rand(3,15),
$rand
,
$te
); <span style=
"background-color: rgb(192, 192, 192);"
>
//要把php当成图片输出,必须给文件一个头申明</span>
<span style=
"color: rgb(255, 0, 0);"
>ob_clean()</span>; <span style=
"color: rgb(255, 0, 0);"
>header</span>(
"Content-type:image/jpeg"
); <span style=
"background-color: rgb(192, 192, 192);"
>
//最终生成图片</span> <span style=
"color: rgb(255, 0, 0);"
>imagejpeg</span>(
$im
);
?>
|