溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP怎么實(shí)現(xiàn)隨機(jī)數(shù)字、字母的驗(yàn)證碼功能

發(fā)布時間:2021-08-11 22:31:51 來源:億速云 閱讀:188 作者:chen 欄目:編程語言

本篇內(nèi)容主要講解“PHP怎么實(shí)現(xiàn)隨機(jī)數(shù)字、字母的驗(yàn)證碼功能”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“PHP怎么實(shí)現(xiàn)隨機(jī)數(shù)字、字母的驗(yàn)證碼功能”吧!



廢話不多說,直接上代碼:

1、classgd.class.php

<?php
Class Captcha{
    private $_fontfile='';
    private $_size=36;
    private $_width=200;
    private $_height=100;
    private $_length=4;
    private $_image=null;
    private $_snow=0;
    private $_pixel=0;
    private $_line=0;
  public function __construct($config=array()){
    if(is_array($config)&&count($config)>0){
      if(isset($config['fontfile'])&&is_file($config['fontfile'])&&is_readable($config['fontfile'])){
        $this->_fontfile=$config['fontfile'];
      }else{
        return false;
      }
      if(isset($config['size'])&&$config['size']>0){
        $this->_size=(int)$config['size'];
      }
      if(isset($config['width'])&&$config['width']>0){
        $this->_width=(int)$config['width'];
      }
      if(isset($config['height'])&&$config['height']>0){
        $this->_height=(int)$config['height'];
      }
      if(isset($config['length'])&&$config['length']>0){
        $this->_length=(int)$config['length'];
      }
      if(isset($config['snow'])&&$config['snow']>0){
        $this->_snow=(int)$config['snow'];
      }
      if(isset($config['pixel'])&&$config['pixel']>0){
        $this->_pixel=(int)$config['pixel'];
      }
      if(isset($config['line'])&&$config['line']>0){
        $this->_line=(int)$config['line'];
      }
      $this->_image=imagecreatetruecolor($this->_width,$this->_height);
      return $this->_image;
     }
     else{
      return false;
    }
  }
  public function getCaptcha(){
    $white=imagecolorallocate($this->_image,255,255,255);
    imagefilledrectangle($this->_image,0,0,$this->_width,$this->_height,$white);
    $str=$this->_generateStr($this->_length);
    if(false===$str){
      return false;
    }
    $fontfile=$this->_fontfile;
    for($i=0;$i<$this->_length;$i++){
      $size=$this->_size;
      $angle=mt_rand(-30,30);
      $x=ceil($this->_width/$this->_length)*$i+mt_rand(5,10);
      $y=ceil($this->_height/1.5);
      $color=$this->_getRandColor();
      //針對中文字符截取
      //$text=mb_substr($str,$i,1,'utf-8');
      $text=$str{$i};
      imagettftext($this->_image, $size, $angle, $x, $y, $color, $fontfile, $text);
    }
    if($this->_snow){
      $this->_getSnow();
    }else{
      if($this->_pixel){
        $this->_getPixel();
      }
      if($this->_line){
        $this->_getLine();
      }
    }
    header('content-type:image/png');
    imagepng($this->_image);
    imagedestroy($this->_image);
    return strtolower($str);
  }
  private function _getSnow(){
    for($i=1;$i<=$this->_snow;$i++){
      imagestring($this->_image,mt_rand(1,5),mt_rand(0,$this->_width),mt_rand(0,$this->_height),'*',$this->_getRandColor());
    }
  }
  private function _getPixel(){
    for($i=1;$i<=$this->_pixel;$i++){
      imagesetpixel($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());
    }
  }
  private function _getLine(){
    for($i=1;$i<=$this->_line;$i++){
      imageline($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());
    }
  }
  private function _generateStr($length=4){
    if($length<1 || $length>30){
      return false;
    }
    $chars=array(
      'a','b','c','d','e','f','g','h','k','m','n','p','x','y','z',
      'A','B','C','D','E','F','G','H','K','M','N','P','X','Y','Z',
      1,2,3,4,5,6,7,8,9
      );
    $str=join('',array_rand(array_flip($chars),$length));
    return $str;
  }
  private function _getRandColor(){
    return imagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  }
}
?>


2、testCaptcha.php

<?php
require_once 'classgd.class.php';
$config=array(
'fontfile'=>'fonts/simfang.ttf',  //引入字體文件
//'snow'=>50,
'pixel'=>100,
'line'=>10
  );
$captcha=new Captcha($config);
$captcha->getCaptcha();
?>


就這樣成功實(shí)現(xiàn)了隨機(jī)數(shù)字、字母的驗(yàn)證碼功能!

到此,相信大家對“PHP怎么實(shí)現(xiàn)隨機(jī)數(shù)字、字母的驗(yàn)證碼功能”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI