php·â×°µÄÑéÖ¤ÂëÀà·ÖÏí

5年以前  |  阅读数:950 次  |  编程语言:PHP 

ÑeÖ¤ÂeÊÇÎÒÃÇ¿ª¢µÄʱºo¾­³£Óõ½µÄ¹¦ÄÜ£¬ËuÒÔÔڴ˱¾ÈË°u×°ÁËÒ»¸oÑeÖ¤ÂeÀa£¬Ó¦¸Ã¿ÉÒÔ×÷ΪphpµÄÀa²a¼þÓã¬ÔÚ´ËÖÏi¸ø¸÷λ¶ÁÓÑ¡£

¡¡¡¡ÊµÏÖµÄÔ­ÀiÒ²ÊǺܼoµ¥£¬¾ÍÊÇÀuÓû­²¼µÄ¼¸¸oº¯Êý£¬ÔÙ¼ÓÉÏÒ»Ð(C)×Ö*u´®µÄ»ñÈ¡£¬¶«´ÕÎ÷´Õ¾Í¹¹³ÉÁË£¬ºÇºÇ¡£

¡¡¡¡ÕaÀi´o¸ÅдһÏÂ˼°ɣ¬ÆaʵÕa¸oÀaÒѾ­×¢Ê͵Ädz£Ça³þÁË£¬²»¹ý£¬¸oÈË»¹ÊÇÔÚÐÐÎÄÇ°†ªaÂһϡ£

¡¡¡¡Ê×ÏÈÊǹØÓÚÒ»Ð(C)º¯ÊýµÄ½aÊÍ£¬ÕaÀiµÄ½aÊÍ´¿Êo¸oÈËÌa»a£¬ÓÐʲô´iÎoµÄµØ*½£¬»¹Çe¶ÁÕßÖ¸Õý¡£

¡¡¡¡1¡¢´´½¨»­²¼º¯Êý£ºimagecreatetruecolor(w,h);

¡¡¡¡¡¡¡¡ËµÃ÷£ºÓÃÓÚ´´½¨Ò»¸o»­²¼¡£

¡¡¡¡¡¡¡¡w »­²¼µÄ¿i

¡¡¡¡¡¡¡¡h »­²¼µÄ¸ß

¡¡¡¡¡¡¡¡´Ëº¯ÊýµÄ*µ»ØÖµ×ÊÔ´Àa(gd)

¡¡¡¡2¡¢Îª»­²¼´´½¨Ò»ÖÖÑÕÉ«£ºimagecolorallocate(img,red,green,blue)

¡¡¡¡¡¡¡¡ËµÃ÷£º

¡¡¡¡¡¡¡¡img»­²¼×ÊÔ´

¡¡¡¡¡¡¡¡red£¬green£¬blue¡¡¡¡ÊÇ0~255µÄ*¶Î§

¡¡¡¡3¡¢Îª»­²¼Ìi¼Ó±³¾°É«

¡¡¡¡¡¡¡¡imagefill(img,x,y,color);

¡¡¡¡¡¡¡¡ËµÃ÷£º

¡¡¡¡¡¡¡¡ÔÚ image ͼÏñµÄ×ø±e x£¬y£¨Í¼Ïñ×oÉϽÇΪ 0, 0£(C)

¡¡¡¡4¡¢»­±ß¿o

¡¡¡¡¡¡¡¡imagerectangle($img,x1,y1,x2,y2,color);

¡¡¡¡¡¡¡¡ËµÃ÷£º

¡¡¡¡¡¡¡¡Æa×oÉϽÇ×ø±eΪ x1, y1£¬ÓÒϽÇ×ø±eΪ x2, y2¡£Í¼ÏñµÄ×oÉϽÇ×ø±eΪ 0, 0¡£

*¡¡¡¡3¡¢»aeÖÆÄÚÈÝ(×Öu)**

¡¡¡¡¡¡¡¡imagestring(img ,size,x,y,string,color);

¡¡¡¡¡¡¡¡ËµÃ÷£º

¡¡¡¡¡¡¡¡img»­²¼

¡¡¡¡¡¡¡¡sizeÊÇ×Ö´oС 1ÖÁ5

¡¡¡¡¡¡¡¡x,yÊÇÆðʼµa

¡¡¡¡¡¡¡¡stringÊÇËuÒª»­µÄÄÚÈÝ

¡¡¡¡¡¡¡¡colorÊÇÑÕÉ«

¡¡¡¡4¡¢¸aeËßa¯ÀÀÆ÷ͼƬ¸ñʽ

¡¡¡¡¡¡¡¡header("Content-type:image/png");¿ÉΪimage/gifµÈµÈ

¡¡¡¡5¡¢Êa³o(»o±£´ae)£¬Ò²¿ÉÒÔʹÓõÚ2¸o²ÎÊýʵÏÖ±£´ae

¡¡¡¡¡¡¡¡imagepng(img¡¾,filename¡¿)

¡¡¡¡¡¡¡¡imagejpeg(img¡¾,filename¡¿)

¡¡¡¡¡¡¡¡imagegif(img¡¾,filename¡¿)

¡¡¡¡6¡¢Ìi¼Ó¸ÉÈÅÏߣ¬±¾ÖʾÍÊÇÖ±Ïß

¡¡¡¡¡¡¡¡imageline(img,x1,y1,x2,y2,color);

¡¡¡¡¡¡¡¡ËµÃ÷£º

¡¡¡¡¡¡¡¡¡¡¡¡img »­²¼

¡¡¡¡¡¡¡¡¡¡¡¡x1,y1 Æðµa

¡¡¡¡¡¡¡¡¡¡¡¡x2,y2 ÖÕµa

¡¡¡¡¡¡¡¡¡¡¡¡color ÑÕÉ«

¡¡¡¡7¡¢imagettftext ( img,size, angle, x, y, color, fontfile,text )

¡¡¡¡¡¡¡¡¡¡¡¡ËµÃ÷£º

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡img »­²¼

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡size ×ÖÌa´oС£¬È±Ê¡µ¥Î»ÏñËØ

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡angle ½Ç¶È

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡x,y ×ø±eµa

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡color ÑÕÉ«

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡fontfile ×ÖÌaÎļþ£¬±ØÐeÊÇÖÐÎÄ×ÖÌa

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡text ÄÚÈÝ

¡¡¡¡Ìرð˵Ã÷£ºÕaÀiµÄcolor²ÎÊý¶¼ÊÇimagecolorallocate()º¯Êý´´½¨µÄÑÕÉ«

¡¡¡¡ÏÂÃaeÊÇ˼Â*£º

¡¡¡¡ÕaÀi×iÏÈÉu³É»­²¼£¬Ö®ºo¾ÍÊÇΪ»­²¼Ìi¼Ó×Öu´®£¬Ö±Ïߣ¬Ôeµa£¬±ß¿o£¬À´Éu³ÉÑeÖ¤ÂeµÄ£¬×iºoÀaµ»ØµÄÁ½¸o¹«ÓýӿÚÊÇ£º¿É¹(C)ÍaÃaeµ÷ÓõÄÉu³ÉÑeÖ¤ÂeµÄ»­²¼ºÍÑeÖ¤ÂeµÄ×Öu´®¹¹³É£¬ÎªµÄÊǸøÍa½çÊa³oÑeÖ¤Âe»­²¼£¬ÒÔ¼°´ae´¢×Öu´®£¬×÷ΪÑeÖ¤ÓÃ

¡¡¡¡ÏÂÃaeÊÇ´uÂe£º


    <?php
    namespace captcha;
    /*
    *ÑeÖ¤ÂeÀa
    *verify*½*¨Éu³ÉÑeÖ¤Âe×Ö*u´®
    *entry*½*¨Éu³ÉÑeÖ¤Âe
    *ÌرðÌaÐÑ£ºÕaÀiÒªÏÈÓÃentryÉu³ÉÑeÖ¤Âe£¬ÔÙÓÃverifyÉu³ÉÑeÖ¤ÂeµÄ×Ö*u´®£¬Ò²¾ÍÊDZØÐeÏȵ÷ÓÃentry£¬È»ºo²ÅÄܹ»µ÷ÓÃverifyÉu³ÉÑeÖ¤ÂeµÄ×Ö*u´®£¬Ô­Òo´uÂeÒѾ­ËµÃ÷ÎÊÌaÁË£¬ÒoΪÑeÖ¤ÂeµÄ×Ö*u´®ÊÇÔÚentry*½*¨µ÷ÓÃcaptchaImageÉu³ÉµÄ£¬ËuÒÔ±ØÐeÏȵ÷ÓÃËu²ÅÐÐ
    *ÓеĵØ*½¶ÔÖÐÎĵÄ×ÖÌaÒªÇo±È½Ï¸ß£¬ËuÒÔ£¬ÓеĵØ*½²»Ö§³ÖÖÐÎÄÑeÖ¤Âe
    */
    class Captcha{
      //ÅaÖòÎÊý
      private $config = array();
      //ÑeÖ¤Âe
      private $verifyCode = '';
      //»ñÈ¡ÅaÖÃÎļþµÄÅaÖÃÐÅÏ¢£¬¸øÀa´«²ÎÊý¾ÍÐУ¬ÀýÈçnew Captcha($config);$configÊÇÄaµÄÅaÖÃÎļþÐÅÏ¢
      public function __construct($config=array('width'=>100,'height'=>40,'length'=>4,'size'=>7,'lines'=>0,'dots'=>0,'font'=>'simfang.ttf','rectangle'=>array(255,55,122),'charset'=>true,'chinese'=>'À´µ½Ð»u³¡Ö÷º½Õ¾Â¥½¨ÉeÔÚ»eÒo¹Øϵ´aeÐøÆÚ¼aËu¸ºÕ®ÎñËýÔÚÊÕµ½ÒªÇoËý³¥»¹Ç°*oÔÚ»eÒo¹Øϵ´aeÐøÆÚ¼aËuÇ*Õ®ÎñµÄ*¨Ôº´«Æ±ºoÒª¾«ÒaeÇo¾«ÉÆʼÉÆÖÕ')){
        $this->config = $config;
      }
      //´´½¨ÑeÖ¤Âe
      private function captchaImage(){
        //»­²¼
        $img = imagecreatetruecolor($this->config['width'],$this->config['height']);
        //Ìi³a»­²¼ÑÕÉ«
        imagefill($img,0,0,imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
        //ÐeÒª±ß¿oÔo»­±ß¿o
        if($this->config['rectangle'] && is_array($this->config['rectangle']) && count($this->config['rectangle']) == 3){
          $this->tangle($img);
        }
        $this->verifyCode = $this->code($img,$this->config['charset'],$this->config['chinese']);
        //´aeÔÚÔoÌi¼Ó¸ÉÈÅÏß
        if($this->config['lines']){
          $this->codeLines($img);
        }
        //´aeÔÚÔoÌi¼Ó¸ÉÈŵa
        if($this->config['dots']){
          $this->codeDots($img);
        }
        return $img;
      }
      private function codeLines($img){
        //»aeÖƸÉÈÅÏß
        for($i=0;$i<$this->config['lines'];$i++){
          imageline($img,mt_rand(0,$this->config['width'] / 10),mt_rand(0,$this->config['height']),mt_rand($this->config['width'] * 7/ 10,$this->config['width'] * 9/ 10),mt_rand(0,$this->config['height']),imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));
        }
      }
      private function codeDots($img){
        //Ìi¼ÓÔeµa
        for($i=0;$i<$this->config['dots'];$i++){
          //ÔeµaÑÕÉ«
          $color = imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180));
          imagestring($img,mt_rand(1,3),mt_rand(0,170),mt_rand(0,30),'*',$color);

        }
      }
      /*»­²¼±ß¿o*/
      private function tangle($img){
        imagerectangle($img,0,0,$this->config['width']-1,$this->config['height']-1,imagecolorallocate($img,$this->config['rectangle'][0],$this->config['rectangle'][1],$this->config['rectangle'][2]));
      }
      /*Éu³ÉÑeÖ¤Âe,ĬÈÏÓ¢ÎÄ£¬$chΪtrueÔoΪÖÐÎÄ*/
      private function code($img,$ch=false,$set=''){
        $str = "";
        //¼ÆËa¼a¸o
        $span = ceil($this->config['width']/($this->config['length']+1));
        if($ch && !empty($set)){
          //Ëae»u²uÉu×Ö*u
          $set = $this->config['chinese'];
          for($i=0;$i<$this->config['length'];$i++){
            $end = strlen($set)/3;
            $pos = mt_rand(0,$end-1);
            $str .= substr($set,$pos*3,3);
          }
          //ÿ´Î»aeÖÆÒ»¸o×Ö*u
          for($i=1;$i<=$this->config['length'];$i++){
            imagettftext($img,16,mt_rand(-30,60),$i*$span,$this->config['height']*3/5,imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180)),$this->config['font'],substr($str,($i-1)*3,3));
          }
        }else{
          //Ëae»uÉu³É×Öĸ»oÕßÊý×Ö
          for($i=0;$i<$this->config['length'];$i++){
            switch(mt_rand(0,2)){
              case 0:
              $str .= chr(mt_rand(65,90));
              break;
            case 1:
              $str .= chr(mt_rand(97,122));
              break;
            case 2:
              $str .= chr(mt_rand(48,57));
            }
          }
          //ÿ´Î»aeÖÆÒ»¸o×Ö*u
          for($i=1;$i<=$this->config['length'];$i++){
            imagestring($img,$this->config['size'],$i*$span,0,$str[$i-1],imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180)));
          }
        }
        return $str;
      }
      //»ñÈ¡ÑeÖ¤Âe
      public function verify(){
        return $this->verifyCode;
      }
      //Éu³ÉÑeÖ¤Âe
      public function entry(){
        header("content-type:image/png");
        imagepng($this->captchaImage());
      }
    }
    $ob = new Captcha;
    $ob->entry();

×iºo£¬ÎªÁ˲»ÎoÈË×ӵܣ¬»¹ÊÇÔÙÇ¿µ÷Ò»±e£º
ÕaÀi±ØÐeÏÈÓÃentryÉu³ÉÑeÖ¤Âe£¬ÔÙÓÃverifyÉu³ÉÑeÖ¤ÂeµÄ×Öu´®£¬Ò²¾ÍÊDZØÐeÏȵ÷ÓÃentry£¬È»ºo²ÅÄܹ»µ÷ÓÃverifyÉu³ÉÑeÖ¤ÂeµÄ×Öu´®£¬Ô­Òo´uÂeÒѾ­ËµÃ÷ÎÊÌaÁË£¬ÒoΪÑeÖ¤ÂeµÄ×Öu´®ÊÇÔÚentry½¨µÄ½¨captchaImageÖÐÉu³ÉµÄ£¬ËuÒÔ±ØÐeÏȵ÷ÓÃËu²ÅÐÐ Óеĵؽ¶ÔÖÐÎĵÄ×ÖÌaÒªÇo±È½Ï¸ß£¬ËuÒÔ£¬ÓеĵØ*½²»Ö§³ÖÖÐÎÄÑeÖ¤Âe

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例