Copy codeThe Code is as follows: <? Php
Function make_rand ($ length = "32") {// Verification Code text generation function
$ Str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ";
$ Result = "";
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Num [$ I] = rand (0, 61 );
$ Result. = $ str [$ num [$ I];
}
Return $ result;
}
$ Checkcode = make_rand (5 );
$ Im_x = 160;
$ Im_y = 32;
Function make_crand ($ length = "5 "){
$ String = '';
For ($ I = 0; $ I <$ length; $ I ++ ){
$ String. = chr (rand (0xB0, 0xF7). chr (rand (0xA1, 0xFE ));
}
Return $ string;
}
Function getAuthImage ($ text, $ im_x = 230, $ im_y = 32 ){
$ Im = imagecreatetruecolor ($ im_x, $ im_y );
$ Text_c = ImageColorAllocate ($ im, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100 ));
$ TmpC0 = mt_rand (100,255 );
$ TmpC1 = mt_rand (100,255 );
$ TmpC2 = mt_rand (100,255 );
$ Buttum_c = ImageColorAllocate ($ im, $ tmpC0, $ tmpC1, $ tmpC2 );
Imagefill ($ im, 16, 13, $ buttum_c );
// Echo $ text;
$ Font = 'C: // WINDOWS // Fonts // simsun. ttc ';
// Echo strlen ($ text );
$ Text = iconv ("gb2312", "UTF-8", $ text );
// Echo mb_strlen ($ text, "UTF-8 ");
For ($ I = 0; $ I <mb_strlen ($ text); $ I ++)
{
$ Tmp = mb_substr ($ text, $ I, 1, "UTF-8 ");
$ Array = array (-1, 0, 1 );
$ P = array_rand ($ array );
$ An = $ array [$ p] * mt_rand (1, 9); // Angle
$ Size = 20;
Imagettftext ($ im, $ size, $ an, 10 + $ I * $ size * 2, 25, $ text_c, $ font, $ tmp );
}
$ Distortion_im = imagecreatetruecolor ($ im_x, $ im_y );
Imagefill ($ distortion_im, 16, 13, $ buttum_c );
For ($ I = 0; $ I <$ im_x; $ I ++ ){
For ($ j = 0; $ j <$ im_y; $ j ++ ){
$ Rgb = imagecolorat ($ im, $ I, $ j );
If (int) ($ I + 20 + sin ($ j/$ im_y * 2 * M_PI) * 10) <= imagesx ($ distortion_im) & (int) ($ I + 20 + sin ($ j/$ im_y * 2 * M_PI) * 10)> = 0 ){
Imagesetpixel ($ distortion_im, (int) ($ I + 10 + sin ($ j/$ im_y * 2 * M_PI-M_PI * 0.5) * 3), $ j, $ rgb );
}
}
}
// Add interference pixels;
$ Count = 600; // Number of interfering pixels
For ($ I = 0; $ I <$ count; $ I ++ ){
$ Randcolor = ImageColorallocate ($ distortion_im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagesetpixel ($ distortion_im, mt_rand () % $ im_x, mt_rand () % $ im_y, $ randcolor );
}
$ Line_c = 5;
// Imageline
For ($ I = 0; $ I <$ line_c; $ I ++ ){
$ Linecolor = imagecolorallocate ($ distortion_im, 17,158, 20 );
$ Lefty = mt_rand (1, $ im_x-1 );
$ Righty = mt_rand (1, $ im_y-1 );
Imageline ($ distortion_im, 0, $ lefty, imagesx ($ distortion_im), $ righty, $ linecolor );
}
Header ("Content-type: image/PNG ");
// Output the image to a browser or file in PNG format;
// ImagePNG ($ im );
ImagePNG ($ distortion_im );
// Destroy an image and release the memory associated with the image;
ImageDestroy ($ distortion_im );
ImageDestroy ($ im );
}
?>
Example:Copy codeThe Code is as follows: <? Php
GetAuthImage (make_crand (5 ));
?>