php生成图形验证码几种方法小结_php技巧

我们先来检查一下自己的php是不是打开了gd库。

复制代码 代码如下:

<?php
if(extension_loaded('gd')) {
  echo '你可以使用gd<br>';
  foreach(gd_info() as $cate=>$value)
    echo "$cate: $value<br>";
}else
  echo '你没有安装gd扩展';
?>

如果有返回信息就正确可以常用使用了
例1

复制代码 代码如下:

<?php
/**
 * vCode(m,n,x,y) m个数字  显示大小为n   边宽x   边高y
 * 自己改写记录session $code
 */
session_start();
vCode(4, 15); //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);
}
?>

例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; $i<$random_dots; $i++ ) {
imagefilledellipse($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; $i<$random_lines; $i++ ) {
imageline($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);
}

个人推荐推荐第二个生成验证码程序代码,各位同学可尝试参考对比哦,最后一个是W3C标准生成的也是利用了php gd库。

时间: 2024-08-31 14:48:43

php生成图形验证码几种方法小结_php技巧的相关文章

php生成随机密码的三种方法小结_php技巧

使用PHP开发应用程序,尤其是网站程序,常常需要生成随机密码,如用户注册生成随机密码,用户重置密码也需要生成一个随机的密码.随机密码也就是一串固定长度的字符串,这里我收集整理了几种生成随机字符串的方法,以供大家参考. 方法一: 1.在 33 – 126 中生成一个随机整数,如 35, 2.将 35 转换成对应的ASCII码字符,如 35 对应 # 3.重复以上 1.2 步骤 n 次,连接成 n 位的密码 该算法主要用到了两个函数,mt_rand ( int $min , int $max )函数

PHP中获取文件扩展名的N种方法小结_php技巧

第1种方法: 复制代码 代码如下: function get_extension($file) { substr(strrchr($file, '.'), 1); } 第2种方法: 复制代码 代码如下: function get_extension($file) { return substr($file, strrpos($file, '.')+1); } 第3种方法: 复制代码 代码如下: function get_extension($file) { return end(explode(

PHP判断图片格式的七种方法小结_php技巧

使用php判断文件图片的格式 复制代码 代码如下: <?php $imgurl = "http://www.jb51.net/images/logo.gif"; //方法1 echo $ext = strrchr($imgurl,'.'); echo '<hr>'; //方法2 echo $ext1 = substr($imgurl,strrpos($imgurl, '.')); echo '<hr>'; //方法3 echo(@end(explode(&

php生成图形验证码几种方法总结

我们先来检查一下自己的php是不是打开了gd库.  代码如下 复制代码 <?php if(extension_loaded('gd')) {   echo '你可以使用gd<br>';   foreach(gd_info() as $cate=>$value)     echo "$cate: $value<br>"; }else   echo '你没有安装gd扩展'; ?> 如果有返回信息就正确可以常用使用了 例1  代码如下 复制代码 &l

PHP中替换换行符的几种方法小结_php技巧

第一种: 复制代码 代码如下: <?php ?$str="this is a test \n"; $patten = array("\r\n", "\n", "\r"); ?//先替换掉\r\n,然后是否存在\n,最后替换\r $str=str_replace($order, "", $str); ?> //php 有三种方法来解决 //1.使用str_replace 来替换换行 $str =

PHP合并数组的2种方法小结_php技巧

前言 在此前合并数组我一直用的是array_merge()这个函数,但最近我在换工作的时候遇到一道合并数组的面试题,我当时想的是将两个数组先转化为字符串,合并后再转化为数组输出,面试官说这个思路不太对,完了bulabula讲了一下数组基础的东西,然后确实是因为经验问题,或者是代码写太少,想不到还有什么方法,今天我百度了一下,原来还有'+'号, array_merge_recursive() ,也是可以用来合并数组的,根据我的记忆,我把那道题写出来看一下: $a = array('color'=>

php获取mysql版本的几种方法小结_php技巧

select VERSION()  当前$res=mysql_query("select VERSION()");$row=mysql_fetch_row($res);这样的形式调用SQL查询来获取.  如果查询PHP自身安装的MYSQL支持库(CLIENT)的版本,可以用<?php phpinfo();?> 更为简便的方法使用PHP自带函数mysql_get_server_info();

PHP下用rmdir实现删除目录的三种方法小结_php技巧

1.递规法:利用递归一层一层的删.  复制代码 代码如下: deleteDir($dir)   {   if (rmdir($dir)==false && is_dir($dir)) {   if ($dp = opendir($dir)) {     while (($file=readdir($dp)) != false) {      if (is_dir($file) && $file!='.' && $file!='..') {       del

PHP下打开URL地址的几种方法小结_php技巧

1: 用file_get_contents 以get方式获取内容 复制代码 代码如下: <?php $url='http://www.baidu.com/'; $html = file_get_contents($url); //print_r($http_response_header); ec($html); printhr(); printarr($http_response_header); printhr(); ?> 示例代码2: 用fopen打开url, 以get方式获取内容 复制