A beautiful PHP image verification code Instance _php instance

Source: Internet
Author: User
First, the display effect

Second, the code is as follows
Copy CodeThe code is as follows:/*
* @Author FY
*/

$imgwidth = 100; Picture width
$imgheight = 40; Picture height
$codelen = 4; Verification Code length
$fontsize = 20; Font size
$charset = ' abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789 ';
$font = ' Fonts/segoesc.ttf ';

$im =imagecreatetruecolor ($imgwidth, $imgheight);

$while =imagecolorallocate ($im, 255,255,255);
Imagefill ($im, 0,0, $while); Fill image

Get string
$authstr = ";
$_len = strlen ($charset)-1;
for ($i =0; $i < $codelen; $i + +) {
$authstr. = $charset [Mt_rand (0,$_len)];
}

Session_Start ();
$_session[' Scode ']=strtolower ($AUTHSTR);//All lowercase, mainly for case insensitive

Random draw points, has been changed to draw the stars
for ($i =0; $i < $imgwidth; $i + +) {
$randcolor =imagecolorallocate ($im, Mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));
Imagestring ($im, Mt_rand (1,5), Mt_rand (0, $imgwidth), Mt_rand (0, $imgheight), ' * ', $randcolor);
Imagesetpixel ($im, Mt_rand (0, $imgwidth), Mt_rand (0, $imgheight), $randcolor);
}
Randomly draw lines, number of lines = number of characters (whatever)
for ($i =0; $i < $codelen; $i + +)
{
$randcolor =imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255));
Imageline ($im, 0,mt_rand (0, $imgheight), $imgwidth, Mt_rand (0, $imgheight), $randcolor);
}

$_x=intval ($imgwidth/$codelen); Calculating character distances
$_y=intval ($imgheight *0.7); The character is displayed in the position of picture 70%
for ($i =0; $i<>< p=""><>

$randcolor =imagecolorallocate ($im, Mt_rand (0,150), Mt_rand (0,150), Mt_rand (0,150));
Imagestring ($im, 5, $j, 5, $imgstr [$i], $color 3);
Imagettftext (Resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, String $text)
Imagettftext ($im, $fontsize, Mt_rand ( -30,30), $i *$_x+3,$_y, $randcolor, $font, $authstr [$i]);

}

Generate images
Header ("Content-type:image/png");
Imagepng ($im);
Imagedestroy ($im);

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