>  기사  >  백엔드 개발  >  PHP를 사용하여 덧셈 및 뺄셈 확인 코드 구현

PHP를 사용하여 덧셈 및 뺄셈 확인 코드 구현

不言
不言원래의
2018-06-21 11:33:462678검색

이 글은 주로 PHP에서 구현된 덧셈과 뺄셈을 사용하여 그림을 생성하는 코드를 소개합니다. 필요한 친구들은 참고할 수 있습니다.

<?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으로 문의하세요.