搜尋

tp5验证码
类文件位于extend/other/Captcha.php<br> <br> <br> <br>        //控制器中 获取验证码<br>        public function get_captcha(){    <br>         //使用memcheck 设置session    <br>         Session::init(['prefix'=> 'wll_','type'=> '','auto_start' => true]);<br>         $captcha = new \other\Captcha(86,48,4);<br>         echo $captcha->showImg();        <br>         Session::set('code',$captcha->getCaptcha());<br>         exit;    <br>     }<br> <br> <br> <br> <br> 模块中:  <img class="code-img lazy" src="/static/imghwm/default1.png" data-src="__WEBSITE__/public/index.php/seller/login/get_captcha" alt="tp5验证码" ><br> <br> <br> <br> <br> echo Session::get('code','wll_'); 输出验证码<br> <br> <br> 以下是Captcha.php 类<br> <?php <br /> namespace other;<br> class Captcha{<br>     private $width;<br>     private $height;<br>     private $codeNum;<br>     private $code;<br>     private $im;<br>     //初始化<br>     function __construct($width=80, $height=20, $codeNum=4){<br>         $this->width = $width;<br>         $this->height = $height;<br>         $this->codeNum = $codeNum;<br>     }<br>     //显示验证码<br>     function showImg(){<br>         //创建图片<br>         $this->createImg();<br>         //设置干扰元素<br>         $this->setDisturb();<br>         //设置验证码<br>         $this->setCaptcha();<br>         //输出图片<br>         $this->outputImg();<br>     }<br>     <br>     //获取显示的验证码,用来验证验证码是否数据正确<br>     function getCaptcha(){<br>         return $this->code;<br>     }<br>     <br>     //创建图片<br>     private function createImg(){<br>         $this->im = imagecreatetruecolor($this->width, $this->height);<br>         $bgColor = imagecolorallocate($this->im, 255, 255, 255);//创建的前景为白色<br>         imagefill($this->im, 0, 0, $bgColor);<br>     }<br>     <br>     //设置干扰元素<br>     private function setDisturb(){<br>         $area = ($this->width * $this->height) / 20;<br>         $disturbNum = ($area > 250) ? 250 : $area;<br>         //加入点干扰<br>         for ($i = 0; $i              $color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));<br>             imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);<br>         }<br>         //加入弧线<br>         for ($i = 0; $i              $color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));<br>             imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);<br>         }<br>     }<br>     //设置验证码随机数<br>     private function createCode(){<br>         $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";<br>         for ($i = 0; $i codeNum; $i++) {<br>             $this->code .= $str{rand(0, strlen($str) - 1)};<br>         }<br>     }<br>     //设置验证码<br>     private function setCaptcha(){<br>           <br>         //设置验证码随机数        <br>         $this->createCode();<br>         <br>         //文字前景<br>         $color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));<br>                <br>         //因为imagechar最大的文字字体为5,字体太小而不用这个方式了<br>         //imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);           <br>                <br>         //因为imagechar最大的文字字体为5,而这里要显示更大的文字,所以用 imagefttext <br>         imagefttext($this->im,30,0,10,35,$color,'static/pc/fonts/monofont.ttf',$this->code);//图象资源,尺寸,角度,x轴,y轴,颜色,字体路径,文本插入图像<br>     }<br>     //输出图片<br>     private function outputImg(){<br>         if (imagetypes() & IMG_JPG) {<br>             header('Content-type:image/jpeg');<br>             imagejpeg($this->im);<br>         } elseif (imagetypes() & IMG_GIF) {<br>             header('Content-type: image/gif');<br>             imagegif($this->im);<br>         } elseif (imagetype() & IMG_PNG) {<br>             header('Content-type: image/png');<br>             imagepng($this->im);<br>         } else {<br>             die("Don't support image type!");<br>         }<br>     }//end<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器