In PHP 5.0.1, gd2.0.28 supports creating GIF images. [an image verification program is provided.]

Source: Internet
Author: User

In PHP 5.0.1, gd2.0.28 supports creating GIF images. I tested it. I have attached an image verification code that I wrote. I encapsulated it into a class in disorder. Actually, I can use the function. Modify it by yourself.

<? PHP

/**
* @ Author shenkong <shenkong@openphp.cn>
* @ Version $ ID
* @ Package SPB forum system
* @ Copyright: All rights reserved for shenkong. This Copyright must be reserved for any modification or use.
*/
Class checkimage
{
Public $ bglength;
Public $ bgwidth;
Public $ bgcolor;
Public $ fontsize;
Public $ fonttype;
Public $ length;
Public $ image;
Public $ string;

Function _ construct ($ info)
{
$ This-> bglength = $ info ["bglength"];
$ This-> bgwidth = $ info ["bgwidth"];
$ This-> fontsize = $ info ["fontsize"];
$ This-> fonttype = $ info ["fonttype"];
$ This-> length = $ info ["length"];
$ This-> image = $ this-> createimage ();
// $ Black = imagecolorallocate ($ this-> image, 0x00, 0x00, 0x00 );
$ White = imagecolorallocate ($ this-> image, 0xff, 0xff, 0xff );
Imagefilledrectangle ($ this-> image, 0, 0, $ this-> bglength, $ this-> bgwidth, $ white );
$ This-> miximage (500, 10 );
$ This-> write ();
$ This-> miximage (50, 5 );

// Set white to transparent
Imagecolortransparent ($ this-> image, $ white );
Header ("Content-Type: image/GIF ");
Imagegif ($ this-> image );
}

Function createimage ()
{
Return imagecreatetruecolor ($ this-> bglength, $ this-> bgwidth );
}

Function randcolor ()
{
Return imagecolorallocate ($ this-> image, mt_rand (0,200), mt_rand (0,100), mt_rand (0,150 ));
}

Public Function write ()
{
$ STR = MD5 (mt_rand ());
$ STR = strtoupper ($ Str );
$ STR = str_replace (Array ("I", "1", "5", "S", "4", "A", "8", "B ", "0", "D"), "", $ Str );
$ STR = substr ($ STR, 0, $ this-> length );
$ This-> string = $ STR;
$ W = ($ this-> bglength-($ this-> length-1) * 3)/$ this-> length;
For ($ I = 0; $ I <$ this-> length; $ I ++)
{
Imagettftext ($ this-> image, $ this-> fontsize, mt_rand (-20, 20), mt_rand ($ I * $ W + $ I * 3, ($ I + 1) * $ W + $ I * 3-$ this-> fontsize), mt_rand ($ this-> fontsize, $ this-> bgwidth-$ this-> bgwidth/10), $ this-> randcolor (), $ this-> fonttype, substr ($ STR, $ I, 1 ));
}
}

Function setstring ()
{
Require_once "session. Class. php ";
$ S = new session ();
$ S-> setpath ();
}

Function miximage ($ num= 500, $ fontsize = 10)
{
For ($ I = 0; $ I <$ num; $ I ++)
{
Imagettftext ($ this-> image, $ fontsize, mt_rand (-180,180), mt_rand (1, $ this-> bglength), mt_rand (1, $ this-> bgwidth ), imagecolorallocate ($ this-> image, mt_rand (150,255), mt_rand (150,255), mt_rand (150,255), $ this-> fonttype, "| ");
}
}
}

$ Info = array (
"Bglength" => 90,
"Bgwidth" => 30,
"Fontsize" => 18,
"Fonttype" => "Arial ",
"Length" => 5
);
New checkimage ($ info );
?>

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.