生成图形验证码需要使用php GD库来生成,如果你没开户GD库我们需要在php.ini文件找到extension=php_gd2.dll 去掉前面的;就行了,然后重启apache 或iis环境即可
检查是否打开了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推荐),可以很方便的用于项目中。而且样式美观
<?php
//首先开启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; //by www.jbxue.com
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);
}
推荐第二种方法。

有关php  图形验证码的例子,还可以参考:

php 仿QQ验证码

php 验证码(GD库生成验证码)
php 图片验证码
php 彩色验证码
php GD库生成验证码
php 雪花背景的验证码
php生成动态图片验证码

php 图形验证码的3种方法的更多相关文章

  1. Python随机生成验证码的两种方法

    Python随机生成验证码的方法有很多,今天给大家列举两种,大家也可以在这个基础上进行改造,设计出适合自己的验证码方法方法一:利用range Python随机生成验证码的方法有很多,今天给大家列举两种 ...

  2. laravel框架——验证码(第一种方法)

    一.在框架根目录下下载图形验证码的组件 composer require gregwar/captcha=1.* 二.控制器代码 use Gregwar\Captcha\CaptchaBuilder; ...

  3. laravel加入验证码类几种方法 && Laravel引入第三方库的方法

    1,使用require , inlcude 的方法将验证码类文件包含进来,再进行new 2,将验证码类文件放于Http目录下面,也就是和控制器controller放在一个目录下面,在验证码类文件中加上 ...

  4. laravel框架——验证码(第二种方法)

    开发环境: laravel5.5 php7.1.11 mysql 一.安装扩展包 ,安装前确认当前环境支持composer(出现如下图所示则安装成功) $ composer require " ...

  5. selenium UI自动化解决验证码的五种方法

    TesseractPy3.py #coding=utf-8 import os import subprocess import traceback import logging from PIL i ...

  6. .NET 采用 SkiaSharp 生成二维码和图形验证码及图片进行指定区域截取方法实现

    在最新版的 .NET 平台中,微软在逐步放弃 System.Drawing.Imaging ,给出的理由如下: System.Drawing命名空间对某些操作系统和应用程序类型有一些限制. 在Wind ...

  7. [ASP.NET] 图形验证码破解-以简单图形为例

    原文 http://www.dotblogs.com.tw/joysdw12/archive/2013/06/08/captcha-cracked.aspx 前言 这次来讲个比较有趣的主题,就是该如何 ...

  8. Tornado框架实现图形验证码功能

    图形验证码是项目开发过程中经常遇到的一个功能,在很多语言中都有对应的不同形式的图形验证码功能的封装,python 中同样也有类似的封装操作,通过绘制生成一个指定的图形数据,让前端HTML页面通过链接获 ...

  9. python 生成图形验证码

    文章链接:https://mp.weixin.qq.com/s/LYUBRNallHcjnhJb1R3ZBg 日常在网站使用过程中经常遇到图形验证,今天准备自己做个图形验证码,这算是个简单的功能,也适 ...

随机推荐

  1. Subsequence poj 3061 二分(nlog n)或尺取法(n)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9236   Accepted: 3701 Descr ...

  2. Easier Done Than Said?

    Problem Description Password security is a tricky thing. Users prefer simple passwords that are easy ...

  3. 《Cortex-M0权威指南》之体系结构---栈空间操作

    转载请注明来源:cuixiaolei的技术博客 栈空间作为一种存储器使用机制,是"先入先出"的结构,在系统空间中用作临时数据的存储.栈空间操作的关键之一为栈指针寄存器,每次执行栈操 ...

  4. Oracle 基础 <1> --存储过程

    一.子程序 子程序是已命名的PL/SQL块,它们存储在数据库中,可以Wie它们指定参数,可以从任何数据库客户端和应用程序中调用它们.子程序包括存储过程和函数. 子程序包括: 1.声明部分:声明部分包括 ...

  5. ie下使用firebug

    https://getfirebug.com/firebuglite/ 是介绍firebug在别的浏览器如何使用 或者在html中引入firebug-lite.js,然后安F12 <script ...

  6. 转:eclipse怎样修改包(package)的显示样式、格式 工具/原料

    转:http://blog.csdn.net/longyaxx258/article/details/50774128 eclipse 打开我们的项目,可以看到左侧的package看上去特别多,没有层 ...

  7. android 开源库集合的链接列表

    1.  内容挺多的 https://github.com/Trinea/android-open-project

  8. android菜鸟学习笔记2----关于adb

    adb : android debug bridge android调试桥 路径:adt-bundle目录/sdk/platform-tools/adb.exe 常见的adb命令: adb devic ...

  9. Sass之初识

    Sass is the most mature, stable, and powerful professional grade CSS extension language in the world ...

  10. 关于css的一些小细节---link

    <link rel="stylesheet" href=“a.css” type="text/css"> rel:当前文档与被链接文档间的关系,必须 ...