phpは算術認証コード機能を実現します。


最近はphpの画像関数を勉強しました。練習の検証コードも実現しました。先生のアドバイスに従って、自分で算術の検証コードの関数を書きました。しかし、自分の限界も知っていますので、わざわざ書きました。いろいろ教えてください。もっと自分の視野を広げることができます。
phpコードは以下の通りです。検証コードの機能を実現します。

<?php
/**
 * @param int $width   ,   120
 * @param int $height   ,   50
 * @param int $fontSize      
 * @return     
 */
function arithmeticCode($width=120,$height=50,$fontSize=20){
  //  session
  session_start();
  //    
  $img = imagecreatetruecolor($width,$height);
  //    
  $color = imagecolorallocate($img,255,255,255);
  //    
  imagefill($img,0,0,$color);

  //   
  for ($i = 0;$i < 500;$i++){
    $pixColor = imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
    imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$pixColor);
  }
  //   
  for ($i = 0;$i < 4;$i++){
    $lineColor = imagecolorallocate($img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
    imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$lineColor);
  }

  //            
  $arr = ['+','-','*'];
  //       
  $len = count($arr);
  //    1 20   
  $num = range(1,20);
  $numLen = count($num);
  //                  
  $code = [];
  for ($i = 0;$i < $len;$i++) {
    if ($i == 1) {
      $code[] = $arr[mt_rand(0,$len-1)];
    }else {
      $code[] = $num[mt_rand(0,$numLen-1)];
    }
  }

  $str = implode($code);//        
  $textColor = imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
  $fontAngle = 0;
  $x = ($width - $fontSize*3)/2;
  $y = ($height - $fontSize) / 2 + $fontSize;
  imagettftext($img,$fontSize,$fontAngle,$x,$y,$textColor,"./img/msyh.ttc",$str);

  $res = getRes($code);


  //      session 
  $_SESSION['res'] = $res;

  //    
  header("content-type:image/png");
  imagepng($img);
}


/**
 * @param $arr            
 * @return         
 */
function getRes($arr) {
  $sum = 0;
  //         1        
  switch ($arr[1]){
    case '+':
      $sum = $arr[0] + $arr[2];
      break;
    case '-':
      $sum = $arr[0] - $arr[2];
      break;
    case '*':
      $sum = $arr[0] * $arr[2];
      break;
  }

  return $sum;
}

//    
arithmeticCode(100,40,18);
htmlの部分コード

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
     content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    img {
      position: relative;
      top: 20px;
    }
  </style>
</head>
<body>
<form action="test.php">
      <input type="text" name="code">
  <img src="./demo5.php" alt="    ">
  <br>
  <button>  </button>
</form>
</body>
</html>
<script>
  //              
  var img = document.querySelector("img");
  img.onclick = function () {
    this.src = this.src+"?m="+Math.random();
  }
</script>
test.phpのテスト

<?php
session_start();
$res = $_SESSION['res'];
$value = $_GET['code'];
if ($res == $value) {
  echo "test success";
}else{
  echo "test fail";
}
以上が私のコード構成です。
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。