PHP High Security Verification Code generator _php Tutorial

Source: Internet
Author: User
This generator needs to call some font libraries Oh, you can call some of your system's own fonts, of course, you can also like Dedecms to put the font into a directory, so it can be used on the server.

The code is as follows Copy Code

Public Function Captcha () {
$font _dir = $_server ["Document_root"]. "Your_ttf_file.ttf"; Font Library
$img _w = 58; Set Picture width
$img _h = 20; Set Picture height
$font _size = 11; Font size
$angle _l =-10; Left angle
$angle _r = 10; Right angle
$code _str = "ABCDEFGHJKLMNPQRSTUVWXYZ36";
$word _len = 4; Verify number of code bits
$padding = 5; intervals between two words
$margin = 2; Left margin
$base _line = 15; Text Baseline position
$base _line_offset = 2; Baseline offset
$pixel _num = 3; Number of miscellaneous points base
$pixel _color= 8; The clutter is only $pixel _color color total number of $pixel_num* $pixel _color
$noise _font_size = 1; Clutter Font Size
$session _key= "My.xoyo_captcha"; Custom session key Name

Header ("Cache-control:no-cache, must-revalidate");
Header ("Expires:mon, Jul 1997 05:00:00 GMT");
Header ("Pragma:no-cache");
Header ("Cache-control:private");
Header (' content-type:image/png ');

Session_Start ();
$word = "";
$code _str_len = strlen ($code _str)-1;
for ($i = 0; $i < $word _len; $i + +) {
$word. = $code _str[rand (0, $code _str_len)];
}
$_session [$session _key] = Strtolower ($word);
$image = Imagecreatetruecolor ($img _w, $img _h);
Imagefilledrectangle ($image, 0, 0, $img _w-1, $img _h-1, Imagecolorallocate ($image, Mt_rand (235, 255), Mt_rand (235, 255) , Mt_rand (235, 255));

Draw a noise point
for ($i = 0; $i < $pixel _color; $i + +) {
$noise _color = imagecolorallocate ($image, Mt_rand (150,225), Mt_rand (150,225), Mt_rand (150,225));
for ($j = 0; $j < $pixel _num; $j + +) {
Imagestring ($image, $noise _font_size, Mt_rand ( -10, $img _w), Mt_rand ( -10, $img _h), $code _str[mt_rand (0, $code _str_len) ], $noise _color);
}
}

Draw text
for ($i = 0; $i < $word _len; + + $i) {
$color = Imagecolorallocate ($image, Mt_rand (0, 150), Mt_rand (+), Mt_rand (50,));
Imagettftext ($image, $font _size, Mt_rand ($angle _l, $angle _r), $margin, Mt_rand ($base _line-$base _line_offset, $base _ line+ $base _line_offset), $color, $font, Mb_substr ($word, $i, 1, ' utf-8 '));
$margin + = (imagefontwidth ($font _size) + $padding);
}

Imagepng ($image);
Imagedestroy ($image);
Exit
}

http://www.bkjia.com/PHPjc/629688.html www.bkjia.com true http://www.bkjia.com/PHPjc/629688.html techarticle This generator needs to call some font libraries Oh, you can call some of your system's own fonts, of course, you can also like Dedecms to put the font into a directory, so on the server ...

  • 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.