PHP standard build Verification Code program

Source: Internet
Author: User
Tags php tutorial rand
The code is as follows Copy Code

<?php Tutorial

Session_Start ();

$ENABLEGD = 1;
To determine whether an image processing function exists
$funcs = Array (' Imagecreatetruecolor ', ' imagecolorallocate ', ' Imagefill ', ' imagestring ', ' imageline ', ' imagerotate ', ' Imagedestroy ', ' imagecolorallocatealpha ', ' imageellips tutorial e ', ' imagepng ');
foreach ($funcs as $func)
{
if (!function_exists ($func))
{
$ENABLEGD = 0;
Break
}
}

Ob_clean (); Clean buffer

if ($ENABLEGD)
{
Create Captcha
$consts = ' cdfgkmnpqrstwxyz23456 ';
$vowels = ' aek23456789 ';
for ($x = 0; $x < 6; $x + +)
{
$const [$x] = substr ($consts, Mt_rand (0,strlen ($consts)-1), 1); Gets a random number in the $consts
$vow [$x] = substr ($vowels, Mt_rand (0,strlen ($vowels)-1), 1); Gets a random number in the $vowels
}
$radomstring = $const [0]. $vow [0]. $const [2]. $const [1]. $vow [1]. $const [3]. $vow [3]. $const [4];
$_session[' checkcode ' = $string = substr ($radomstring, 0,4); Display 4 characters

$imageX = strlen ($radomstring) *8; The width of the image
$imageY = 20; The height of the image
$im = Imagecreatetruecolor ($imageX, $imageY); Create a new True color image

 //creates two variables to store color
  $background = Imagecolorallocate ($im, rand (180,), Rand (180, ), rand (180, 250)); Background color
  $FOREGROUNDARR = Array (imagecolorallocate ($im, rand (0), rand (0), rand (0)),
  Imageco Lorallocate ($im, rand (0), rand (0,), rand (245, 255)),
  Imagecolorallocate ($im, rand (245, 255), rand (0, 20) , rand (0)),
  Imagecolorallocate ($im, rand (245, 255), rand (0), rand (245, 255))
 
  $FO Reground2 = Imagecolorallocatealpha ($im, Rand, Rand (a), Rand (20, 100), 80); Assign a color and describe the transparency
  $middleground = Imagecolorallocate ($im, rand (160), rand (160), Rand (200, 160));//Intermediate background br>   $middleground 2 = Imagecolorallocatealpha ($im, rand (180, 140), Rand (180, 140), Rand (180, 140), 80); Middle Background 2

The same color as the upper-left corner will be filled
Imagefill ($im, 0, 0, imagecolorallocate ($im, 250, 253, 254));
Write text to the image
Imagettftext ($im, Rand ( -30), 5, Rand (a), $foregroundArr [Rand (0,3)], ' C:windowsfontsarial.ttf ', $string [0]) ;
Imagettftext ($im, Rand ( -50), Rand (a), $foregroundArr [Rand (0,3)], ' C:windowsfontsarial.ttf ', $string [1] );
Imagettftext ($im, Rand ( -50), Rand (a), $foregroundArr [Rand (0,3)], ' C:windowsfontsarial.ttf ', $string [2] );
Imagettftext ($im, Rand ( -30), Rand (a), $foregroundArr [Rand (0,3)], ' C:windowsfontsarial.ttf ', $string [3] );

Draw a border
$border = Imagecolorallocate ($im, 133, 153, 193);
Imagerectangle ($im, 0, 0, $imageX-1, $imageY-1, $border);

Draw some random dots.
$pointcol = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));
For ($i =0 $i <80; $i + +)
{
Imagesetpixel ($im, rand (2, $imageX-2), Rand (2, $imageX-2), $pointcol);
}
Draw a randomly appearing line
For ($x =0 $x <9; $x + +)
{
if (Mt_rand (0, $x)%2==0)
{
Imageline ($im, rand (0), rand (0,), rand (0), rand (0,), rand (0, 999999)); Draw Line
Imageellipse ($im, rand (0), rand (0,), rand (0), rand (0,), $middleground 2); Draw an Ellipse
}
Else
{
Imageline ($im, rand (0), rand (0,), rand (0), rand (0,), rand (0, 999999));
Imageellipse ($im, rand (0), rand (0,), rand (0), rand (0,), $middleground);
}
}
Output to Browser
Header ("Content-type:image/pngrn");
Imagepng ($im);
Imagedestroy ($im);
}
Else
{
$files = Glob (xincheng_root. ' Images/checkcode/*.jpg ');
if (!is_array ($files)) Die (' Please check file directory integrity:/images/checkcode/');

$checkcodefile = $files [Rand (0, COUNT ($files)-1)]; Random one of the files
$_session[' checkcode '] = substr (basename ($checkcodefile), 0, 4); Get file name

Header ("Content-type:image/jpegrn");
Include $checkcodefile;
}
?>

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.