首頁  >  文章  >  後端開發  >  使用php實作加減法驗證碼

使用php實作加減法驗證碼

不言
不言原創
2018-06-21 11:33:462680瀏覽

這篇文章主要介紹了php實現的加減法驗證碼代碼,可以使用10以內的加減法生成圖片,需要的朋友可以參考下

<?php
/*图片验证码文件,加减计算方式*/
class ImageCode{

 private $Jiashu  = 0;        //加数或者减数
 private $JianShu = 0;        //被加数或者被减数
 private $YunSuan = &#39;&#39;;       //运算符
 private $DeShu   = 0;        //得数
 private $String  = &#39;&#39;;       //字符串样式
 private $Img;                //图片对象
 private $Width   = 100;      //图片宽度
 private $Height  = 50;       //图片高度
 private $Ttf     = &#39;Num.ttf&#39;;//字体文件
 private $Session = &#39;code&#39;;   //Session变量

 private function JiaShu(){
  header(&#39;Content-type:image/png&#39;);
  $this -> Jiashu  = rand(1, 10);
  $this -> JianShu = rand(1, 10);
  $this -> YunSuan= $this -> Jiashu > $this -> JianShu ? &#39;-&#39; : &#39;+&#39;;
  $this -> DeShu   = $this -> Jiashu > $this -> JianShu ? $this -> Jiashu - $this -> JianShu : $this -> Jiashu + $this -> JianShu;
 }

 public function Show( $W = 100, $H = 50, $T = &#39;Num.ttf&#39;, $Code = &#39;code&#39; ){
  $this -> JiaShu();
  $this -> String = $this -> Jiashu . $this -> YunSuan . $this -> JianShu . &#39;= ? &#39;;
  $this -> Width  = $W;
  $this -> Height = $H;
  $this -> Ttf    = $T;
  $this -> Session= $Code;
  session_start();
  $_SESSION[$this -> Session] = $this -> DeShu;
  $this -> Images();
 }

 private function Images(){
  $this -> Img = imagecreate($this -> Width, $this -> Height);
  $background_color = imagecolorallocate ($this -> Img, 255, 255, 255);
  imagecolortransparent($this -> Img, $background_color);
        imagettftext($this -> Img, 14, 0, 1, 20, imagecolorallocate ($this -> Img, 0, 0, 0), $this -> Ttf, $this -> String );
  $this -> EchoImages();
 }

 private function EchoImages(){
  imagepng($this -> Img);
  imagedestroy($this -> Img);
 }

}
$ImageCode = new ImageCode;
$ImageCode -> Show(130, 35, &#39;Num.ttf&#39;, &#39;code&#39;);

以上就是本文的全部內容,希望對大家的學習有幫助,更多相關內容請關注PHP中文網!

相關推薦:

關於PHP求多項式導數的函數代碼

關於PHP截取字串的一些方法總結

以上是使用php實作加減法驗證碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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