PHP Verification Code Class

Source: Internet
Author: User

<?php

Class Ctl_admin_captcha extends ssc_controler{

Public Function Index ()
{
Load the Captcha class and set the session
$captcha = $this->load->library (' Validatecode ');
$captcha->gencodeimage ();
$_session[' captcha '] = $captcha->getcode ();

}
}


/*
* Verification code class, need to load font file
*
* */
Class Ssc_validatecode
{
Private $charset = "abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ3456789";
Private $code;
Private $codelen = 4;
Private $width = 150;
Private $height = 50;
Private $img;
Private $font;
Private $fontsize = 18;
Private $fontcolor;

Public Function __construct ()
{
if (!function_exists ("Imagecreatetruecolor") | | |!function_exists ("imagecolorallocate") | |!function_exists (" Imagefilledrectangle ") | | !function_exists ("Imagettftext") | | !function_exists ("Imageline") | | !function_exists ("imagestring")) {
return false;
}

$this->font = "/latinwd. TTF ";

if (!file_exists ($this->font)) {
return false;
}
}

Public Function __destruct ()
{

if (Isset ($this->img)) {
unset ($this->img);
$this->img = NULL;
}

if (Isset ($this->font)) {
unset ($this->font);
$this->font = NULL;
}
}

Private Function Createcode ()
{
$_len = strlen ($this->charset)-1;

for ($i = 0; $i < $this->codelen; $i + +) {
$this->code. = $this->charset[mt_rand (0, $_len)];
}
}

Private Function CREATEBG ()
{
$this->img = Imagecreatetruecolor ($this->width, $this->height);
$color = Imagecolorallocate ($this->img, 255, 255, 255);
Imagefilledrectangle ($this->img, 0, $this->height, $this->width, 0, $color);
}

Private Function CreateFont ()
{
$_x = $this->width/$this->codelen;

for ($i = 0; $i < $this->codelen; $i + +) {
$this->fontcolor = imagecolorallocate ($this->img, 0, 0, 0);
Imagettftext ($this->img, $this->fontsize, Mt_rand ( -25), ($_x * $i) + mt_rand (1, 3), $this->height/1.4, $thi S->fontcolor, $this->font, Strtoupper ($this->code[$i]));
}
}

Private Function Createline ()
{
for ($i = 0; $i < $i + +) {
$color = Imagecolorallocate ($this->img, 0, 0, 0);
Imageline ($this->img, Mt_rand (0, $this->width), Mt_rand (0, $this->height), Mt_rand (0, $this->width), Mt_ Rand (0, $this->height), $color);
}

for ($i = 0; $i < 6; $i + +) {
$color = Imagecolorallocate ($this->img, 0, 0, 0);
Imagestring ($this->img, Mt_rand (1, 5), Mt_rand (0, $this->width), Mt_rand (0, $this->height), "*", $color);
}
}

Private Function OutPut ()
{
Ob_clean ();
Header ("Content-type:image/png");
Imagepng ($this->img);
Imagedestroy ($this->img);
}

Public Function Gencodeimage ()
{

$this->CREATEBG ();
$this->createcode ();
$this->createline ();
$this->createfont ();
$this->output ();
return true;
}

Public Function GetCode ()
{
Return Strtolower ($this->code);
}
}


?>

PHP Verification Code Class

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.