$img_w
= 70;
$img_h
= 22;
$font
= 5;
$char_len
= 5;
$char
=
array_merge
( range (
'a'
,
'z'
), range (
'A'
,
'Z'
), range (
'1'
,
'9'
) );
$rand_keys
=
array_rand
(
$char
,
$char_len
);
if
(
$char_len
== 1) {
$rand_keys
=
array
(
$rand_keys
);
}
shuffle(
$rand_keys
);
$code
=
''
;
foreach
(
$rand_keys
as
$k
) {
$code
.=
$char
[
$k
];
}
session_start ();
$_SESSION
[
'captcha'
] =
$code
;
$img
= imagecreatetruecolor (
$img_w
,
$img_h
);
$bg_color
= imagecolorallocate (
$img
, 0xcc, 0xcc, 0xcc );
imagefill (
$img
, 0, 0,
$bg_color
);
for
(
$i
= 0;
$i
< 300; ++
$i
) {
$color
= imagecolorallocate (
$img
, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
imagesetpixel (
$img
, mt_rand ( 0,
$img_w
), mt_rand ( 0,
$img_h
),
$color
);
}
for
(
$i
= 0;
$i
<= 10; ++
$i
) {
$color
= imageColorAllocate (
$img
, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
imageline (
$img
, mt_rand ( 0,
$img_w
), mt_rand ( 0,
$img_h
), mt_rand ( 0,
$img_w
), mt_rand ( 0,
$img_h
),
$color
);
}
$rect_color
= imagecolorallocate (
$img
, 0x90, 0x90, 0x90 );
imagerectangle (
$img
, 0, 0,
$img_w
- 1,
$img_h
- 1,
$rect_color
);
$str_color
= imagecolorallocate (
$img
, mt_rand ( 0, 100 ), mt_rand ( 0, 100 ), mt_rand ( 0, 100 ) );
$font_w
= imagefontwidth (
$font
);
$font_h
= imagefontheight (
$font
);
$str_len
=
$font_w
*
$char_len
;
imagestring (
$img
,
$font
, (
$img_w
-
$str_len
) / 2, (
$img_h
-
$font_h
) / 2,
$code
,
$str_color
);