PHP類的運用解決方案

來源:互聯網
上載者:User
PHP類的運用
我是新手剛接觸類的概念,現在我在網上找到了一個產生驗證碼的類,要怎麼才能在需要輸出驗證碼的地方輸出類中產生的驗證碼呢?類中的代碼是這樣的:
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, 100), rand(0, 100), 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()//設定噪點
{
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);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.