澳门京葡网站:php生成图形验证码三种办法小结

其实这里主要用到了smarty模板中的math函数,现在假设在smarty模板中需要生成一个125-324之间的随机数,// 文件作者,Jesse Lee,  echo ‘你可以使用gd,loaded(‘gd’)) {

出口结果:9
复制代码 代码如下:
{* $row_height = 10, $row_width = 20, #col_div# = 2, assigned in
template *}

复制代码 代码如下:

例2
利用PHP,结合session和GD库扩展开垦的贰个生成验证码的例子(w3c推荐),能够很平价的用来项目中。况且样式赏心悦目
复制代码 代码如下:
//首先开启session
session_start();
//定义前台呈现验证码长&宽
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = ‘./monofont.ttf’;
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = ‘23456789bcdfghjkmnpqrstvwxyz’;
$random_dots = 10;
$random_lines = 30;
$captcha_text_color=”0x142864″;
$captcha_noice_color = “0x142864”;
//定义要生成验证码的字符串
$code = ”;
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0,
strlen($possible_letters)-1), 1);
$i++;
}
$font_size = $image_height * 0.75;
$image = @imagecreate($image_width, $image_height);
/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color[‘red’],
        $arr_text_color[‘green’], $arr_text_color[‘blue’]);
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image,
$arr_noice_color[‘red’],
        $arr_noice_color[‘green’], $arr_noice_color[‘blue’]);
/* generating the dots randomly in background */
for( $i=0; $iimagefilledellipse($image, mt_rand(0,$image_width),
 mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
/* generating lines randomly in background of image */
for( $i=0; $iimageline($image, mt_rand(0,$image_width),
mt_rand(0,$image_height),
 mt_rand(0,$image_width), mt_rand(0,$image_height),
$image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width – $textbox[4])/2;
$y = ($image_height – $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font ,
$code);
/* Show captcha image in the page html page */
header(‘Content-Type: image/jpeg’);// defining the image type to be
shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
//设置session,做验证
$_SESSION[‘6_letters_code’] = $code;
function hexrgb ($hexstr)
{
  $int = hexdec($hexstr);
  return array(“red” => 0xFF & ($int >> 0x10),
               “green” => 0xFF & ($int >> 0x8),
               “blue” => 0xFF & $int);
}

而是要求静心的是: 由于使用了 php 的 eval(State of Qatar 函数,math
函数的实行作用不高. 在 PHP 中做数学生运动算成效会越来越高级中学一年级些,由此要硬着头皮在
PHP 中做数学生运动算,将结果赋给模板变量。

$operator = ‘+-*’; //运算符

咱俩先来检查一下自个儿的php是还是不是开辟了gd库。
复制代码 代码如下:
if(extension_loaded(‘gd’)) {
  echo ‘你能够应用gd
‘;
  foreach(gd_info() as $cate=>$value)
    echo “$cate: $value
“;
}else
  echo ‘你从未设置gd扩大’;
?>

出口结果:9.44

for ($i=0; $i<100; $i++) {
$pxcolor = ImageColorAllocate($img, mt_rand(0,255), mt_rand(0,255),
mt_rand(0,255));
ImageSetPixel($img, mt_rand(0,$width), mt_rand(0,$height),
$pxcolor);
}

假使有重返新闻就精确能够常用使用了
例1
复制代码 代码如下:
/**
 * vCode(m,n,x,y卡塔尔(قطر‎ m个数字  呈现大小为n   边宽x   边高y
 * 自身改写记录session $code
 */
session_start();
vCode(4, 15State of Qatar; //4个数字,突显大小为15
function vCode($num = 4, $size = 20, $width = 0, $height = 0) {
 !$width && $width = $num * $size * 4 / 5 + 5;
 !$height && $height = $size + 10;
 // 去掉了 0 1 O l 等
 $str = “23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW”;
 $code = ”;
 for ($i = 0; $i < $num; $i++) {
  $code .= $str[mt_rand(0, strlen($str)-1)];
 }
 // 画图像
 $im = imagecreatetruecolor($width, $height);
 // 定义要用到的颜料
 $back_color = imagecolorallocate($im, 235, 236, 237);
 $boer_color = imagecolorallocate($im, 118, 151, 199);
 $text_澳门京葡网站,color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0,
120), mt_rand(0, 120));
 // 画背景
 imagefilledrectangle($im, 0, 0, $width, $height, $back_color);
 // 画边框
 imagerectangle($im, 0, 0, $width-1, $height-1, $boer_color);
 // 画忧愁线
 for($i = 0;$i < 5;$i++) {
  $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0,
255), mt_rand(0, 255));
  imagearc($im, mt_rand(- $width, $width), mt_rand(- $height,
$height), mt_rand(30, $width * 2), mt_rand(20, $height * 2),
mt_rand(0, 360), mt_rand(0, 360), $font_color);
 }
 // 画苦闷点
 for($i = 0;$i < 50;$i++) {
  $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0,
255), mt_rand(0, 255));
  imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height),
$font_color);
 }
 // 画验证码
 @imagefttext($im, $size , 0, 5, $size + 3, $text_color,
‘c:\WINDOWS\Fonts\simsun.ttc’, $code);
 $_SESSION[“VerifyCode”]=$code;
 header(“Cache-Control: max-age=1, s-maxage=1, no-cache,
must-revalidate”);
 header(“Content-type: image/png;charset=gb2312”);
 imagepng($im);
 imagedestroy($im);
}
?>

{math equation=”(( x + y ) / z )” x=2 y=10 z=2}

$code = array();
$code[] = mt_rand(1,9);
$code[] = $operator{mt_rand(0,2)};
$code[] = mt_rand(1,9);
$code[] = $operator{mt_rand(0,2)};
$code[] = mt_rand(1,9);
$codestr = implode(”,$code);
eval(“\$result = “.implode(”,$code).”;”);
$code[] = ‘=’;

村办推举推荐第贰个生成验证码程序代码,各位同学可尝试参谋相比哦,最终八个是W3C规范生成的也是利用了php
gd库。

出口结果:100
复制代码 代码如下:

<?php
//————————————-
// 文件表明:数学生运动算验证码
// 文件作者:Jesse Lee
// 最终更新:二零一零-09-07
//————————————-