zf驗證碼類(Zend_Captcha_Image)

來源:互聯網
上載者:User

可先重再captcha類,減少噪點

 /**<br /> * 重載captcha類<br /> *<br /> */<br />class MyImage extends Zend_Captcha_Image {<br />protected $_dotNoiseLevel = 10;<br /> protected function _generateImage($id, $word)<br /> {<br /> if (!extension_loaded("gd")) {<br /> require_once 'Zend/Captcha/Exception.php';<br /> throw new Zend_Captcha_Exception("Image CAPTCHA requires GD extension");<br /> }<br /> if (!function_exists("imagepng")) {<br /> require_once 'Zend/Captcha/Exception.php';<br /> throw new Zend_Captcha_Exception("Image CAPTCHA requires PNG support");<br /> }<br /> if (!function_exists("imageftbbox")) {<br /> require_once 'Zend/Captcha/Exception.php';<br /> throw new Zend_Captcha_Exception("Image CAPTCHA requires FT fonts support");<br /> }<br /> $font = $this->getFont();<br /> if (empty($font)) {<br /> require_once 'Zend/Captcha/Exception.php';<br /> throw new Zend_Captcha_Exception("Image CAPTCHA requires font");<br /> }<br /> $w = $this->getWidth();<br /> $h = $this->getHeight();<br /> $fsize = $this->getFontSize();<br /> $img_file = $this->getImgDir() . $id . $this->getSuffix();<br /> if(empty($this->_startImage)) {<br /> $img = imagecreatetruecolor($w, $h);<br /> } else {<br /> $img = imagecreatefrompng($this->_startImage);<br /> if(!$img) {<br /> require_once 'Zend/Captcha/Exception.php';<br /> throw new Zend_Captcha_Exception("Can not load start image");<br /> }<br /> $w = imagesx($img);<br /> $h = imagesy($img);<br /> }<br /> $text_color = imagecolorallocate($img, 0, 0, 0);<br /> $bg_color = imagecolorallocate($img, 255, 153, 0);<br /> imagefilledrectangle($img, 0, 0, $w-1, $h-1, $bg_color);<br /> $textbox = imageftbbox($fsize, 0, $font, $word);<br /> $x = ($w - ($textbox[2] - $textbox[0])) / 2;<br /> $y = ($h - ($textbox[7] - $textbox[1])) / 2;<br /> imagefttext($img, $fsize, 0, $x, $y, $text_color, $font, $word);<br /> // generate noise<br /> for ($i=0; $i<$this->_dotNoiseLevel; $i++) {<br /> imagefilledellipse($img, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);<br /> }<br /> for($i=0; $i<$this->_lineNoiseLevel; $i++) {<br /> imageline($img, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);<br /> }<br /> // transformed image<br /> $img2 = imagecreatetruecolor($w, $h);<br /> $bg_color = imagecolorallocate($img2, 255, 255, 255);<br /> imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bg_color);<br /> // apply wave transforms<br /> $freq1 = $this->_randomFreq();<br /> $freq2 = $this->_randomFreq();<br /> $freq3 = $this->_randomFreq();<br /> $freq4 = $this->_randomFreq();<br /> $ph1 = $this->_randomPhase();<br /> $ph2 = $this->_randomPhase();<br /> $ph3 = $this->_randomPhase();<br /> $ph4 = $this->_randomPhase();<br /> $szx = $this->_randomSize();<br /> $szy = $this->_randomSize();<br /> for ($x = 0; $x < $w; $x++) {<br /> for ($y = 0; $y < $h; $y++) {<br /> $sx = $x + (sin($x*$freq1 + $ph1) + sin($y*$freq3 + $ph3)) * $szx;<br /> $sy = $y + (sin($x*$freq2 + $ph2) + sin($y*$freq4 + $ph4)) * $szy;<br /> if ($sx < 0 || $sy < 0 || $sx >= $w - 1 || $sy >= $h - 1) {<br /> continue;<br /> } else {<br /> $color = (imagecolorat($img, $sx, $sy) >> 16) & 0xFF;<br /> $color_x = (imagecolorat($img, $sx + 1, $sy) >> 16) & 0xFF;<br /> $color_y = (imagecolorat($img, $sx, $sy + 1) >> 16) & 0xFF;<br /> $color_xy = (imagecolorat($img, $sx + 1, $sy + 1) >> 16) & 0xFF;<br /> }<br /> if ($color == 255 && $color_x == 255 && $color_y == 255 && $color_xy == 255) {<br /> // ignore background<br /> continue;<br /> } elseif ($color == 0 && $color_x == 0 && $color_y == 0 && $color_xy == 0) {<br /> // transfer inside of the image as-is<br /> $newcolor = 0;<br /> } else {<br /> // do antialiasing for border items<br /> $frac_x = $sx-floor($sx);<br /> $frac_y = $sy-floor($sy);<br /> $frac_x1 = 1-$frac_x;<br /> $frac_y1 = 1-$frac_y;<br /> $newcolor = $color * $frac_x1 * $frac_y1<br /> + $color_x * $frac_x * $frac_y1<br /> + $color_y * $frac_x1 * $frac_y<br /> + $color_xy * $frac_x * $frac_y;<br /> }<br /> imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newcolor, $newcolor, $newcolor));<br /> }<br /> }<br /> // generate noise<br /> for ($i=0; $i<$this->_dotNoiseLevel; $i++) {<br /> imagefilledellipse($img2, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);<br /> }<br /> for ($i=0; $i<$this->_lineNoiseLevel; $i++) {<br /> imageline($img2, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);<br /> }<br /> imagepng($img2, $img_file);<br /> imagedestroy($img);<br /> imagedestroy($img2);<br /> }</p><p>}

添加外掛程式類

/**<br /> * Zend_captcha驗證碼<br /> *<br /> */<br />class Custom_Controller_Plugin_Imgcode extends Zend_Controller_Plugin_Abstract {<br />public $word;<br />public $imgdir;<br />public $imgname;<br />public $suffix;<br />public function __construct(){<br />Zend_Loader::loadClass('MyImage');<br />$codeSession = new Zend_Session_Namespace('imgcode'); //在預設建構函式裡執行個體化<br /> $captcha = new MyImage(array('font'=>'./public/images/arial.ttf', //字型檔路徑<br /> 'fontsize'=>24, //字型大小<br /> 'imgdir'=>'./public/images/code/', //驗證碼圖片存放位置<br /> 'session'=>$codeSession, //驗證碼session值<br /> 'width'=>120, //圖片寬<br /> 'height'=>50, //圖片高<br /> 'wordlen'=>5 )); //字母數<br /> $captcha->setExpiration(5); //每5秒<br /> $captcha->setGcFreq(1); //百分百刪除舊檔案<br /> $captcha->generate(); //產生圖片<br /> $codeSession->word = $captcha->getWord();<br /> $this->imgdir = $captcha->getImgdir(); //映像路徑<br /> $this->imgname = $captcha->getId(); //擷取檔案名稱,md5編碼<br /> $this->word = $captcha->getWord(); //認證碼<br /> $this->suffix= $captcha->getSuffix();<br />}<br />public function getImgurl(){<br />return $this->imgdir.$this->imgname.$this->suffix;<br />}<br />public function getImgname(){<br />return $this->imgname;<br />}<br />public function getWord(){<br />return $this->word;<br />}<br />}<br />

 

 

 

 

聯繫我們

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