|
Function Auth_code ($ width = 80, $ height = 20, $ num = 4, $ line = 4, $ dot = 150 ){
$ Length = floor ($ width/$ num); // floor: integer part length: average length of each segment
For ($ I = 0; $ I <$ num; $ I ++)
@ $ Rand. = dechex (mt_rand (1, 15 ));
$ _ SESSION ['code'] = $ rand;
// Create a canvas with a black background
$ Im = imagecreatetruecolor ($ width, $ height );
// Color
$ Green = imagecolorallocate ($ im, 0,255, 0 );
$ Red = imagecolorallocate ($ im, 255, 0, 0 );
$ White = imagecolorallocate ($ im, 255,255,255 );
$ Black = imagecolorallocate ($ im, 0, 0 );
// Fill the canvas
// Imagefill ($ im, 0, 0, $ green );
// Draw a line
For ($ I = 0; $ I <$ line; $ I ++ ){
$ Color = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
Imageline ($ im, rand (10, $ width-10), 0, rand (0, $ width), $ height, $ color );
}
// Draw point
For ($ I = 0; $ I <$ dot; $ I ++ ){
$ Color = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ im, rand (0, $ width), rand (0, $ height), $ color );
}
// Draw numbers
For ($ I = 0; $ I <$ num; $ I ++ ){
Imagestring ($ im, 5, mt_rand ($ I * $ length + 1, ($ I + 1) * $ length-8), mt_rand (0, $ height-14 ), $ _ SESSION ['code'] [$ I], $ white );
}
// Output image
Header ("content-type: image/jpeg ");
Imagejpeg ($ im );
} |