PHP High Security authentication Code generation program

Source: Internet
Author: User

The code is as follows Copy Code

<?php
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 deflection angle
$angle _r = 10; Right angle
$code _str = "ABCDEFGHJKLMNPQRSTUVWXYZ36";
$word _len = 4; Verify code digits
$padding = 5; Spacing between each two text
$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 miscellaneous point is only $pixel _color color total miscellaneous points for $pixel_num* $pixel _color
$noise _font_size = 1; Miscellaneous point font Size
$session _key= "My.xoyo_captcha"; Custom session key Name

Header ("Cache-control:no-cache, must-revalidate");
Header ("Expires:mon, June June 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 Miscellaneous points
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), Mt_rand (a), Mt_rand (50, 150));
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
}

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.