自用 驗證碼 帶一二三加減
阿新 • • 發佈:2019-01-23
<?php session_start(); $_SESSION['code']= 0; header("Content-type: image/png"); $im = @imagecreatetruecolor(70, 27) or die("建立影象失敗"); //建立圖片 $background_color = imagecolorallocate($im, 250, 250, 250); //圖片新增背景 imagefill($im,0,0,$background_color); $border_color = imagecolorallocate($im,0,0,0); //邊框色 imagerectangle($im,0,0,69,26,$border_color); //噪音線 for($i = 0;$i < 7;$i++){ $x1 = rand(3,25); $y1 = rand(2,25);//開始位置 $x2 = rand(45,68);//結束位置 $y2 = rand(2,25); $line_color = imagecolorallocate($im,rand(190,255),rand(190,255),rand(190,255)); //噪音線顏色 imageline($im,$x1,$y1,$x2,$y2,$line_color); } //顯示的文字 $font_size = 18; $first_num = 0;//圖片中顯示的數字,包含中文 $second_num = 0; $first_value = 0;//後臺預算的數字 $second_value = 0; $hanzi_num = array("一","二","三","四","五","六","七","八","九","十"); //$str_HanZiOrNot = rand(0,1);//是否是漢字的判斷 $result = 0; $arrCapcher = array("+","-"); $capter = $arrCapcher[rand(0,1)]; switch ($capter){ case "+": //產生第一個數字 if(rand(0,1) == 0){// ==0表示純數字 $first_num = rand(2,10); $first_value = $first_num; } else{ //漢字 $index_ = rand(0,9); $first_num = $hanzi_num[$index_]; $first_value = $index_ + 1; } //產生第二個數字 if(rand(0,1) == 0){// ==0純數字 $second_num = rand(0,10); $second_value = $second_value; } else{ $index_ = rand(0,9); $second_num = $hanzi_num[$index_]; $second_value = $index_ + 1; } $result = $first_value + $second_value; break; case "-": //產生第一個數字 if(rand(0,1) == 0){// ==0表示純數字 $first_num = rand(3,10); $first_value = $first_num; } else{ //漢字 $index_ = rand(0,9); $first_num = $hanzi_num[$index_]; $first_value = $index_ + 1; } //產生第二個數字 if(rand(0,1) == 0){// ==0純數字 $second_num = rand(0,$first_value-1); $second_value = $second_value; } else{ $index_ = rand(0,$first_value-1); $second_num = $hanzi_num[$index_]; $second_value = $index_ + 1; } $result = $first_value - $second_value; break; } $x = rand(2,5); $arr_X_Y = array(array($x,rand(1,3)),array($x+$font_size,rand(1,4)),array($x+2*$font_size,rand(1,3)),array($x+3*$font_size,rand(1,4))); $str_char = array($first_num,$capter,$second_num,"=","?"); $strs = "";//文字字串 foreach ($str_char as $value){ $strs .= $value; } $text_color = imagecolorallocate($im,rand(50,120),rand(50,120),rand(50,120)); imagettftext($im,15,0,1,20,$text_color,"font/a.ttf",$strs); //輸入漢字時必須使用漢字的字型font $_SESSION['code'] = $result; imagepng($im); imagedestroy($im); ?>