php簡單驗證碼

來源:互聯網
上載者:User
<!--

*@copyright(c)2011

*@author:Ernest

*@time:2011

*@version:new

-->


<?php


class VerifyImg {

public $fontSize = 15; //定義字型大小

public $length = 4; //定義字串長度

public $width = 70; //定義圖片寬度

public $height = 30; //定義圖片高度

public $im = null; //產生一張指定寬高的圖片

public $font = 'C:/Windows/Fonts/Arial.TTF';

public $strNum = "";

public function Build() {

$strings = Array ('1', '2','3', '4', '5', '6', '7', 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y' );

//$strNum = "";

$count = count ( $strings );

for($i = 1; $i <= $this->length; $i ++) { //迴圈隨機取字元產生字串

$strNum .= $strings [rand ( 0, $count - 1 )];

}

session_start ();

$_SESSION ["verifycode"] = $strNum;

$this->im = imagecreate ( $this->width, $this->height );

$backgroundcolor = imagecolorallocate ( $this->im, 255, 255, 255 ); //產生背景色

$frameColor = imageColorAllocate ( $this->im, 0, 255, 0 );

for($i = 0; $i < $this->length; $i ++) {

$charY = ($this->height + 9) / 2 + rand ( - 1, 1 ); //定義字元Y座標

$charX = $i * 15 + 8; //定義字元X座標

// $text_color = imagecolorallocate($this->im, 255, 0, 0); //產生字元顏色

$text_color = imagecolorallocate ( $this->im, mt_rand ( 50, 255 ), mt_rand ( 50, 128 ), mt_rand ( 50, 200 ) );

$angle = rand ( - 20, 20 ); //產生字元角度

//寫入字元

imagettftext ( $this->im, $this->fontSize, $angle, $charX, $charY, $text_color, $this->font, $strNum [$i] );

}

for($i = 0; $i <= 5; $i ++) { //迴圈畫背景線

$linecolor = imagecolorallocate ( $this->im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );

$linex = mt_rand ( 1, $this->width - 1 );

$liney = mt_rand ( 1, $this->height - 1 );

imageline ( $this->im, $linex, $liney, $linex + mt_rand ( 0, 4 ) - 2, $liney + mt_rand ( 0, 4 ) - 2, $linecolor );

}

for($i = 0; $i <= 32; $i ++) { //迴圈畫背景點,產生麻點效果

$pointcolor = imagecolorallocate ( $this->im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );

imagesetpixel ( $this->im, mt_rand ( 1, $this->width - 1 ), mt_rand ( 1, $this->height - 1 ), $pointcolor );

}

imagerectangle ( $this->im, 0, 0, $this->width - 1, $this->height - 1, $frameColor ); //畫邊框

ob_clean ();

header ( 'Content-type:image/png' );

imagepng ( $this->im );

imagedestroy ( $this->im );

}

}

$img = new VerifyImg ();

$img->Build ();

?>

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.