时间:2021-07-01 10:21:17 帮助过:21人阅读
<?php header('Content-Type: image/png'); $im = imagecreatetruecolor( 200 , 50 ); // Create some colors $white = imagecolorallocate($im, 255, 255, 255); $grey = imagecolorallocate($im, 128, 128, 128); $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 200, 49, $white); // The text to draw $rand = rand( 1 ,2 ) ; $one = rand(1,9); $two = rand(1,9); if( $rand == 1 ){ $result = $one + $two; $text = "$one+$two=?"; }else{ if( $one < $two ){ list( $two , $one ) = [ $one , $two ]; } $result = $one - $two; $text = "$one-$two=?"; } session_start(); $_SESSION['vcode'] = $result; // Replace path by your own font path $font = './Arvo-Regular.ttf'; //imageline( $im , 0 , rand( 1,49 ) , 199 , 30 , $grey ); // Add the text $i = 0; $len = strlen( $text ); while( $i < $len ){ if( is_numeric( $text[$i] ) ){ imagettftext($im, 20, rand(-45,45), 20 * ($i+1) , 30, $black, $font, $text[$i]); }else{ imagettftext($im, 20, 0 , 20 * ($i+1) , 30, $black, $font, $text[$i]); } //echo $text[$i]; $i ++; } // Using imagepng() results in clearer text compared with imagejpeg() imagepng($im); imagedestroy($im);
加法减法可以这么实现,除法和乘法也类似
除法时候会遇到除不尽的情况,可以换一种思路,使用乘法解决
2*3=6
6/2=3
这样就不会出现负数的情况了
相关推荐:
php实现简单的计算器
以上就是PHP实现加法、减法验证码的详细内容,更多请关注Gxl网其它相关文章!