ホームページ  >  記事  >  バックエンド開発  >  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 中国語 Web サイトに注目してください。

関連する推奨事項:

PHP で多項式導関数を見つけるための関数コードについて

インターセプトのためのいくつかの方法の概要PHP の文字列

以上がPHPを使用して加算および減算の検証コードを実装するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。