PHP Picture Verification Code

Source: Internet
Author: User
Tags cos rand sin

PHP Verification Code Program Verification Code code PHP PHP picture Verification Code

<?php
/*
@date 20080514
@author Hluan

Session_Start ();
Class Icode
{
function __construct () {
Header (' content-type:image/png ');
}
function __destruct () {
Imagedestroy ($png);
}

function GetCode ($len, $type) {
Switch ($type) {
Case 1;
$str = "1234567890";
Break
Case 2;
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Break
Case 0;
$str = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Break
}
$result = "";
$length = strlen ($str)-1;
$num = 0;
for ($i =0; $i < $len; $i + +) {
$num = rand (0, $length);
$a = $str [$num];
$result = $result. $a;
}
/*before using it,you should use Session_Start () * *
$_session[' icode ' = $result;//store in session.
echo "session:". $_session[' Icode '];
Die ();
$png = Imagecreate (60,30);
$white = Imagecolorallocate ($png, 255,255,255);//background
$red = Imagecolorallocate ($png, 255,0,0);
$blue = Imagecolorallocate ($png, 0,0,255);
$brown = Imagecolorallocate ($png, 100,0,0);
$black = Imagecolorallocate ($png, 0,0,0);//identifying Code
Imagefill ($png, 0,0, $white);
$k = rand (0,3);
if (0 = $k) {
For ($n =0 $n <60; $n + +) {
$y = 15*sin ($n/30*pi ());
Imagesetpixel ($png, $n, 15+ $y, $red);
}
}else if (1 = $k) {
For ($n =0 $n <60; $n + +) {
$y = 15*cos ($n/60*pi ());
Imagesetpixel ($png, $n, 15+ $y, $red);
}
}else if (2 = $k) {
For ($n =0 $n <60; $n + +) {
$y = 15*sin ($n/45*pi ());
Imagesetpixel ($png, $n, 15+ $y, $brown);
}
}else if (3 = $k) {
For ($n =0 $n <60; $n + +) {
$y = 15*cos ($n/30*pi ());
Imagesetpixel ($png, $n, 15+ $y, $brown);
}
}
For ($k =0 $k <10; $k + +) {
$i = rand (3,60);//width
$j = rand (3,15);//height
Imageline ($png, $i -3, $j -3, $i, $j, $black);
}
Imagestring ($png, 5,3,5, $result, $blue);
$icode = Imagepng ($png);
return $icode;
}
}
/* Test Code
$icode = new Icode ();
$ICP = $icode->getcode (6,0);
Echo $ICP;
*/

Related Article

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.