您好,欢迎访问一九零五行业门户网

PHP实现加法、减法验证码

本篇文章给大家分享的是php实现加法、减法验证码,有兴趣的朋友可以看一看
<?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实现加法、减法验证码的详细内容。
其它类似信息

推荐信息