phpパッケージの検証コードツールクラスの完全なインスタンス

4662 ワード

この例では、phpパッケージの検証コードツールクラスについて説明します.皆さんの参考にしてください.具体的には以下の通りです.

width = isset($arr['width']) ? $arr['width'] : $GLOBALS['config']['captcha']['width'];
      $this->height = isset($arr['height']) ? $arr['height'] : $GLOBALS['config']['captcha']['height'];
      $this->fontsize = isset($arr['fontsize']) ? $arr['fontsize'] : $GLOBALS['config']['captcha']['fontsize'];
      $this->pixes = isset($arr['pixes']) ? $arr['pixes'] : $GLOBALS['config']['captcha']['pixes'];
      $this->lines = isset($arr['lines']) ? $arr['lines'] : $GLOBALS['config']['captcha']['lines'];
      $this->str_len = isset($arr['str_len']) ? $arr['str_len'] : $GLOBALS['config']['captcha']['str_len'];
    }
    /*
     *        
    */
    public function generate(){
      //    
      $img = imagecreatetruecolor($this->width,$this->height);
      //     
      $bg_color = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
      imagefill($img,0,0,$bg_color);
      //     
      $this->getLines($img);
      //     
      $this->getPixels($img);
      //       
      $captcha = $this->getCaptcha();
      //    
      $str_color = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
      //    
      //             
      $start_x = ceil($this->width/2) - 25;
      $start_y = ceil($this->height/2) - 8;
      if(imagestring($img,$this->fontsize,$start_x,$start_y,$captcha,$str_color)){
        //  :     
        header('Content-type:image/png');
        imagepng($img);
      }else{
        //  
        return false;
      }
    }
    /*
     *           
     * @return string $captcha,       
    */
    private function getCaptcha(){
      //       
      $str = implode('',array_merge(range('a','z'),range('A','Z'),range(1,9)));
      //   
      $captcha = '';  //       
      for($i = 0,$len = strlen($str);$i < $this->str_len;$i++){
        //         
        $captcha .= $str[mt_rand(0,$len - 1)] . ' ';
      }
      //      session
      $_SESSION['captcha'] = str_replace(' ','',$captcha);
      //   
      return $captcha;
    }
    /*
     *      
     * @param1 resource $img
    */
    private function getPixels($img){
      //     
      for($i = 0;$i < $this->pixes;$i++){
        //    
        $pixel_color = imagecolorallocate($img,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
        //  
        imagesetpixel($img,mt_rand(0,$this->width),mt_rand(0,$this->height),$pixel_color);
      }
    }
    /*
     *      
     * @param1 resource $img,           
    */
    private function getLines($img){
      //     
      for($i = 0;$i < $this->lines;$i++){
        //    
        $line_color = imagecolorallocate($img,mt_rand(150,200),mt_rand(150,200),mt_rand(150,200));
        //  
        imageline($img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$line_color);
      }
    }
    /*
     *      
     * @param1 string $captcha,        
     * @return bool,    true,    false
    */
    public static function checkCaptcha($captcha){
      //         
      return (strtolower($captcha) === strtolower($_SESSION['captcha']));
    }
}


PHPに関する内容についてもっと兴味のある読者は、「PHPグラフィックと画像操作テクニックまとめ」、「PHP基本文法入門チュートリアル」、「PHP演算と演算子の使い方まとめ」、「phpオブジェクト向けプログラム設計入門チュートリアル」、「PHPネットワークプログラミングテクニックまとめ」、「PHP配列(Array)操作テクニック大全」、「php文字列(string)用法総括」、「php+mysqlデータベース操作入門チュートリアル」および「php一般データベース操作テクニック要約」
ここで述べたことが皆さんのPHPプログラム設計に役立つことを願っています.