zend framework驗證碼

  1. /** 
  2.  * 重載captcha類 
  3.  * 
  4.  */  
  5. class MyImage extends Zend_Captcha_Image {  
  6.     protected $_dotNoiseLevel = 10;   
  7.      protected function _generateImage($id$word)  
  8.     {  
  9.         if (!extension_loaded("gd")) {  
  10.             require_once 'Zend/Captcha/Exception.php';  
  11.             throw new Zend_Captcha_Exception("Image CAPTCHA requires GD extension");  
  12.         }  
  13.         if (!function_exists("imagepng")) {  
  14.             require_once 'Zend/Captcha/Exception.php';  
  15.             throw new Zend_Captcha_Exception("Image CAPTCHA requires PNG support");  
  16.         }  
  17.         if (!function_exists("imageftbbox")) {  
  18.             require_once 'Zend/Captcha/Exception.php';  
  19.             throw new Zend_Captcha_Exception("Image CAPTCHA requires FT fonts support");  
  20.         }  
  21.         $font = $this->getFont();  
  22.         if (emptyempty($font)) {  
  23.             require_once 'Zend/Captcha/Exception.php';  
  24.             throw new Zend_Captcha_Exception("Image CAPTCHA requires font");  
  25.         }  
  26.         $w     = $this->getWidth();  
  27.         $h     = $this->getHeight();  
  28.         $fsize = $this->getFontSize();  
  29.         $img_file   = $this->getImgDir() . $id . $this->getSuffix();  
  30.         if(emptyempty($this->_startImage)) {  
  31.             $img        = imagecreatetruecolor($w$h);  
  32.         } else {  
  33.             $img = imagecreatefrompng($this->_startImage);  
  34.             if(!$img) {  
  35.                 require_once 'Zend/Captcha/Exception.php';  
  36.                 throw new Zend_Captcha_Exception("Can not load start image");  
  37.             }  
  38.             $w = imagesx($img);  
  39.             $h = imagesy($img);  
  40.         }  
  41.         $text_color = imagecolorallocate($img, 0, 0, 0);  
  42.         $bg_color   = imagecolorallocate($img, 255, 153, 0);  
  43.         imagefilledrectangle($img, 0, 0, $w-1, $h-1, $bg_color);  
  44.         $textbox = imageftbbox($fsize, 0, $font$word);  
  45.         $x = ($w - ($textbox[2] - $textbox[0])) / 2;  
  46.         $y = ($h - ($textbox[7] - $textbox[1])) / 2;  
  47.         imagefttext($img$fsize, 0, $x$y$text_color$font$word);  
  48.        // generate noise  
  49.        for ($i=0; $i<$this->_dotNoiseLevel; $i++) {  
  50.           imagefilledellipse($img, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);  
  51.        }  
  52.         for($i=0; $i<$this->_lineNoiseLevel; $i++) {  
  53.            imageline($img, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);  
  54.         }  
  55.         // transformed image  
  56.         $img2     = imagecreatetruecolor($w$h);  
  57.         $bg_color = imagecolorallocate($img2, 255, 255, 255);  
  58.         imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bg_color);  
  59.         // apply wave transforms  
  60.         $freq1 = $this->_randomFreq();  
  61.         $freq2 = $this->_randomFreq();  
  62.         $freq3 = $this->_randomFreq();  
  63.         $freq4 = $this->_randomFreq();  
  64.         $ph1 = $this->_randomPhase();  
  65.         $ph2 = $this->_randomPhase();  
  66.         $ph3 = $this->_randomPhase();  
  67.         $ph4 = $this->_randomPhase();  
  68.         $szx = $this->_randomSize();  
  69.         $szy = $this->_randomSize();  
  70.         for ($x = 0; $x < $w$x++) {  
  71.             for ($y = 0; $y < $h$y++) {  
  72.                 $sx = $x + (sin($x*$freq1 + $ph1) + sin($y*$freq3 + $ph3)) * $szx;  
  73.                 $sy = $y + (sin($x*$freq2 + $ph2) + sin($y*$freq4 + $ph4)) * $szy;  
  74.                 if ($sx < 0 || $sy < 0 || $sx >= $w - 1 || $sy >= $h - 1) {  
  75.                     continue;  
  76.                 } else {  
  77.                     $color    = (imagecolorat($img$sx$sy) >> 16)         & 0xFF;  
  78.                     $color_x  = (imagecolorat($img$sx + 1, $sy) >> 16)     & 0xFF;  
  79.                     $color_y  = (imagecolorat($img$sx$sy + 1) >> 16)     & 0xFF;  
  80.                     $color_xy = (imagecolorat($img$sx + 1, $sy + 1) >> 16) & 0xFF;  
  81.                 }  
  82.                 if ($color == 255 && $color_x == 255 && $color_y == 255 && $color_xy == 255) {  
  83.                     // ignore background  
  84.                     continue;  
  85.                 } elseif ($color == 0 && $color_x == 0 && $color_y == 0 && $color_xy == 0) {  
  86.                     // transfer inside of the image as-is  
  87.                     $newcolor = 0;  
  88.                 } else {  
  89.                     // do antialiasing for border items  
  90.                     $frac_x  = $sx-floor($sx);  
  91.                     $frac_y  = $sy-floor($sy);  
  92.                     $frac_x1 = 1-$frac_x;  
  93.                     $frac_y1 = 1-$frac_y;  
  94.                     $newcolor = $color    * $frac_x1 * $frac_y1  
  95.                               + $color_x  * $frac_x  * $frac_y1  
  96.                               + $color_y  * $frac_x1 * $frac_y  
  97.                               + $color_xy * $frac_x  * $frac_y;  
  98.                 }  
  99.                 imagesetpixel($img2$x$y, imagecolorallocate($img2$newcolor$newcolor$newcolor));  
  100.             }  
  101.         }  
  102.         // generate noise  
  103.         for ($i=0; $i<$this->_dotNoiseLevel; $i++) {  
  104.             imagefilledellipse($img2, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);  
  105.         }  
  106.         for ($i=0; $i<$this->_lineNoiseLevel; $i++) {  
  107.            imageline($img2, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);  
  108.         }  
  109.         imagepng($img2$img_file);  
  110.         imagedestroy($img);  
  111.         imagedestroy($img2);  
  112.     }  
  113.       
  114. }  

添加插件類

  1. /** 
  2.  * Zend_captcha驗證碼 
  3.  * 
  4.  */  
  5. class Custom_Controller_Plugin_Imgcode extends Zend_Controller_Plugin_Abstract {  
  6.     public $word;  
  7.     public $imgdir;  
  8.     public $imgname;  
  9.     public $suffix;  
  10.     public function __construct(){  
  11.     Zend_Loader::loadClass('MyImage');  
  12.     $codeSession = new Zend_Session_Namespace('imgcode'); //在默認構造函數裏實例化   
  13.     $captcha = new MyImage(array('font'=>'./public/images/arial.ttf'//字體文件路徑  
  14.                'fontsize'=>24, //字號  
  15.                'imgdir'=>'./public/images/code/'//驗證碼圖片存放位置  
  16.                'session'=>$codeSession//驗證碼session值  
  17.                'width'=>120, //圖片寬  
  18.                'height'=>50,   //圖片高  
  19.                'wordlen'=>5 )); //字母數  
  20.       $captcha->setExpiration(5);  //每5秒  
  21.       $captcha->setGcFreq(1);  //百分百刪除舊文件  
  22.       $captcha->generate(); //生成圖片  
  23.       $codeSession->word = $captcha->getWord();  
  24.       $this->imgdir = $captcha->getImgdir(); //圖像路徑  
  25.       $this->imgname = $captcha->getId(); //獲取文件名,md5編碼  
  26.       $this->word = $captcha->getWord(); //認證碼  
  27.       $this->suffix= $captcha->getSuffix();  
  28.     }  
  29.     public function getImgurl(){  
  30.         return $this->imgdir.$this->imgname.$this->suffix;  
  31.     }  
  32.     public function getImgname(){  
  33.         return $this->imgname;  
  34.     }  
  35.     public function getWord(){  
  36.         return $this->word;  
  37.     }  
  38. }  

發佈了5 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章