PHP-generated distorted and support for Chinese image verification code function-PHP Tutorial

Source: Internet
Author: User
PHP generates image verification code functions that support Chinese characters with distorted angles. Does PHP generate a function that supports image verification codes with an angle of Chinese characters? Phpfunctionmake_rand ($ length32) {verification code text generation function $ strabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV PHP generates a distorted image verification code function that supports Chinese characters.

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: WINDOWSFontssimsun. ttc ';
// Echo strlen ($ text );

$ Text = iconv ("gb2312", "UTF-8", $ text );
// Echo mb_strlen ($ text, "UTF-8 ");
For ($ I = 0; $ 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 );
}

GetAuthImage (make_crand (5 ));

?>


Why? Php function make_rand ($ length = 32) {// verification code text generation function $ str = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.