The application of PHP class solution

Source: Internet
Author: User
Tags getcolor
Application of PHP class
I am a novice just contact with the concept of class, now I found on the Internet a code to generate a class, how to output a verification code where the output of the code generated in the class? The code in the class is like this:
Class Authcode
{
var $image;
var $sBgcolor;
var $nWidth;
var $nHeight;
var $nLen;
var $bNoise;
var $nNoise;
var $bBorder;
var $aFontlist;
function Authcode ()
{
$this->sbgcolor = "#FFFFFF";
$this->nwidth = 70;
$this->nheight = 25;
$this->nleftmargin = 5;
$this->nrightmargin = 5;
$this->ntopmargin = 3;
$this->nbottommargin = 2;
$this->nlen = 4;
$this->bnoise = true;
$this->nnoisepoint = 50;
$this->nnoiseline = 5;
$this->bborder = true;

$this->afontlist = "Arial.ttf";
}

function outputimg ()
{
$this->image = "";
$this->image = imagecreate ($this->nwidth, $this->nheight);
$back = $this->getcolor ($this->sbgcolor);
Imagefilledrectangle ($this->image, 0, 0, $this->nwidth, $this->nheight, $back);
$size = ($this->nwidth-$this->nleftmargin-$this->nrightmargin)/$this->nlen;
if ($size > ($this->nheight-$this->ntopmargin-$this->nbottommargin))
$size = $this->nheight-$this->ntopmargin-$this->nbottommargin;

$left = ($this->nwidth-$this->nlen* ($size + $size/10)/2 + $this->nleftmargin;
$code = "";
for ($i =0; $i < $this->nlen; $i + +)
{
$randtext = rand (0, 9);
$code. = $randtext;
$textColor = Imagecolorallocate ($this->image, rand (0, +), rand (0, +), rand (0, 100));
$font = $this->afontlist;
$randsize = rand ($size-$size/10, $size + $size/10);
$location = $left + ($i * $size + $size/10);
Imagettftext ($this->image, $randsize, Rand ( -18,18), $location, Rand ($size, $size + $size/5) + $this->ntopmargin, $ TextColor, $font, $randtext);
}
if ($this->bnoise = = True) $this->setnoise ();
$_session[' yzm '] = MD5 ($code);
$bordercolor = $this->getcolor ("#FFFFFF");
if ($this->bborder==true) imagerectangle ($this->image, 0, 0, $this->nwidth-1, $this->nheight-1, $ BorderColor);
Header ("Content-type:image/png");
Imagepng ($this->image);
Imagedestroy ($this->image);
}
function setnoise ()//Set the noise point
{
for ($i =0; $i < $this->nnoiseline; $i +) {
$randColor = Imagecolorallocate ($this->image, rand (0, 255), rand (0, 255), rand (0, 255));
Imageline ($this->image, rand (0, $this->nwidth), rand (0, $this->nheight), rand (0, $this->nwidth), rand (0, $ this->nheight), $randColor);
}

for ($i =0; $i < $this->nnoisepoint; $i +) {
$randColor = Imagecolorallocate ($this->image, rand (0, 255), rand (0, 255), rand (0, 255));
Imagesetpixel ($this->image, rand (0, $this->nwidth), rand (0, $this->nheight), $randColor);
  • 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.