PHP生成制作验证码

看完就会,不会你打我,话不多说、开搞(人狠话不多)

1.0  首先先看代码

 1 <?php
 2 header("Content-Type:text/html;Charset=UTF-8");// 设置页面的编码风格
 3 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像
 4
 5 $img = imagecreatetruecolor(150,50);//创建画布并设置大小  x轴150  y轴50
 6
 7 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景颜色
 8 imagefill($img, 0, 0, $bgcolor); ////把背景填充到图像
 9 imagejpeg($img);             // 输出图像
10 imagedestroy($img);          // 销毁图像
11 ?>

好,现在结合以上代码,来分析分析以上用到的几个函数:

①  imagecreatetruecolor(); 

imagecreatetruecolor — 新建一个真彩色图像(感觉哇,那么长,其实仔细一看挺好记的 image/create/true/color,什么是真彩色图像?往下看)

1 resource imagecreatetruecolor ( int $width , int $height )

imagecreatetruecolor() 和 imagecreate()两个函数都能创建画布

1 resource imagecreate ( int $x_size , int $y_size )

imagecreatetruecolor()建立的是一幅大小为 x和 y的黑色图像(默认为黑色[即便叫法就是真彩色图像]),如想改变背景颜色则需要用填充颜色函数 imagefill($img,0,0,$color);

imagecreate 新建一个空白图像资源,用imagecolorAllocate()添加背景色

上面两个函数只不过是一个功能的两种方法

②  imagecolorallocate();

imagecolorallocate — 为一幅图像分配颜色

1 int imagecolorallocate ( resource $image , int $red , int $green , int $blue )

颜色分别用 红 绿 蓝三色组合,这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。

③  mt_rand();

mt_rand — 生成更好的随机数

1 int mt_rand ( int $min , int $max )

$min 可选的、返回的最小值(默认:0)  $max 可选的、返回的最大值(默认:mt_getrandmax())

这里就是用来让他随机生成背景颜色,0-255随便取值。所以页面没刷新一次画布背景颜色就不一样。
效果图:

2.0  开始往里面做干扰线,干扰点。防止验证图像被秒识别

 1 <?php
 2 header("Content-Type:text/html;Charset=UTF-8");// 设置页面的编码风格
 3 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像
 4
 5 $img = imagecreatetruecolor(150,50);//创建画布并设置大小  x轴150  y轴50
 6
 7 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景颜色
 8
 9 //添加干扰线,并循环3次,背景颜色随机
10 for($i=0;$i<3;$i++){
11
12     $linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
13     imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);
14
15 }
16 //添加干扰点,并循环25次,背景颜色随机
17 for($i=0;$i<25;$i++){
18
19     $dotcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
20     imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);
21
22 }
23
24 imagefill($img, 0, 0, $bgcolor); ////把背景填充到图像
25 imagejpeg($img);             // 输出图像
26 imagedestroy($img);          // 销毁图像
27 ?>

函数分析:

①  imageline();

imageline — 画一条线段

1 bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )

imageline() 用 color 颜色在图像 image 中从坐标 x1y1x2y2(图像左上角为 0, 0)画一条线段。

imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);这里意思就是 画布$img 中从坐标 x1y1 到 x2y2随机

②  imagesetpixel();

imagesetpixel— 画一个单一像素

1 bool imagesetpixel ( resource $image , int $x , int $y , int $color )

imagesetpixel() 在 image 图像中用 color 颜色在 xy 坐标(图像左上角为 0,0)上画一个点。

imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);具体含义同上

效果图:

3.0  添加验证字母数字

 1 <?php
 2 header("Content-Type:text/html;Charset=UTF-8");// 设置页面的编码风格
 3 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像
 4
 5 $img = imagecreatetruecolor(150,50);//创建画布并设置大小  x轴150  y轴50
 6
 7 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景颜色
 8
 9 //添加干扰线,并循环3次,背景颜色随机
10 for($i=0;$i<3;$i++){
11
12     $linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
13     imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);
14
15 }
16 //添加干扰点,并循环25次,背景颜色随机
17 for($i=0;$i<25;$i++){
18
19     $dotcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
20     imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);
21
22 }
23
24 //添加需要验证的字母或者数字
25 $rand_str = "qwertyuiopasdfghjklzxcvbnm1234567890";//需要使用到验证的一些字母和数字
26 $str_arr = array();    //命名一个数组
27 for($i = 0;$i<4;$i++){    //循环4次,就是有四个随机的字母或者数字
28     $pos = mt_rand(0,strlen($rand_str)-1);
29     $str_arr[] = $rand_str[$pos];//临时交换
30 }
31
32 $x_start=150/4;//单个字符X轴位置
33
34 foreach ($str_arr as $key) {
35     $fontcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
36     imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);
37     $x_start +=20;//遍历后单个字符沿X轴 +20
38 }
39
40 imagefill($img, 0, 0, $bgcolor); ////把背景填充到图像
41 imagejpeg($img);             // 输出图像
42 imagedestroy($img);          // 销毁图像
43 ?>

函数:

imagettftext();

imagettftext — 用 TrueType 字体向图像写入文本

1 array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )

分析下面的代码:

imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);

$img-----------画布

25-----------字体的尺寸。

mt_rand(-15,15)----------角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。(就是字体角度的问题,)

$x_start----------通俗易懂的讲就是字符的X轴位置

50/2----------字符的高度

$fontcolor----------字符颜色

"C:/Windows/Fonts/Verdana.TTF"----------字符的字体样式路径

$key-----------遍历出后的字符

效果:

看起来还是挺可爱的。

时间: 2024-07-31 23:04:27

PHP生成制作验证码的相关文章

简单制作验证码和绘制图片

今天仍然是完善一般处理程序小项目,但是昨天小小的项目已经终结,今天只是完善一下新的样式罢啦,嘿嘿,我们通常在网上浏览图片时间都会看到图片上面呈现有水印字体的样式,其实这些图片都是通过一些简单的小技术来修饰的:另外我们现在在网上随处可见注册页面,当我们想在一个网站上面注册新的用户时间,需要注册完信息以后填写随机生成的验证码,或者我们想要登录一个网站时间也需要验证码的填写,由此可见,验证码也是我们程序员需要必备的小技术哦,下面就来总结一下这两项小功能的实现. 一.图片上的水印字体 我们可以看到第一张

Jsp制作验证码

验证码 验证码(CAPTCHA)是"Completely Automated Public Turing test to tell Computers and Humans Apart"(全自动区分计算机和人类的图灵测试)的缩写,是一种区分用户是计算机还是人的公共全自动程序.可以防止:恶意破解密码.刷票.论坛灌水,有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试,实际上用验证码是现在很多网站通行的方式,我们利用比较简易的方式实现了这个功能.这个问题可以由计算

学习python:实例2.用PIL生成随机验证码

效果: 代码: # 生成随机验证码图片 import string from random import randint, sample from PIL import Image, ImageDraw, ImageFont, ImageFilter # Image 负责处理图片 # ImageDraw 画笔 # ImageFont 文字 # ImageFileter 滤镜 # 定义变量 img_size = (150,50)        # 定义画布大小 img_rgb = (255,255

php生成动态验证码

<?php /** *ImageCode 生成包含验证码的GIF图片的函数 *@param $string 字符串 *@param $width 宽度 *@param $height 高度 **/ function ImageCode($string='',$width=75,$height=25){ $authstr=$string?$string:((time()%2==0)?mt_rand(1000,9999):mt_rand(10000,99999)); $board_width=$wi

在JSP中动态生成随机验证码,登录时后台校验验证码,以及如何避免同一个验证码被重复提交爆破密码

只需几步就可以生成动态随机的验证码,最终效果如下图: 一 前台显示页面login.jsp 其中验证码显示的是一张图片,链接指向的是生成验证码的servlet,同时点击图片后触发changeImg()这个js函数,使其动态生成一个新的验证码,这个函数中的参数t=Math.random()并不会参与验证码的生成,它的作用仅仅只是表示每次提交的并不是同一个请求,需要单独处理,完整的login.jsp代码如下: <%@ page language="java" contentType=&

学习MVC之制作验证码

制作验证码的方法在@洞庭夕照 看到的,原文链接:http://www.cnblogs.com/mzwhj/archive/2012/10/22/2720089.html 现自己利用该方法制作一个简单的验证码,运行程序效果如下: 现正式开始: 1.新建项目:VerificationCodeTest 2.选择模板:基本 3.添加图片: 放置位置: 4.重点来了,新建文件夹Image,再新建一个类Text.cs using System; using System.Collections.Generi

随机生成数字验证码

protected void Page_Load(object sender, EventArgs e) { // 生成验证码 string checkCode = RandLetter(4); // 把新的验证码保存到Session中 Session["CheckCode"] = checkCode; // 输入验证码 CreateImages(checkCode); } /// <summary> /// 生成验证图片 /// </summary> ///

生成随机验证码的方法

1.借助列表 import random def random_code(): random_list = [] for i in range(4): ra = random.randrange(4) if ra == i: random_list.append(chr(random.randrange(97,122))) else: random_list.append(str(random.randrange(0,9))) code = "".join(random_list) r

Python 生成随机验证码

Python生成随机验证码 Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 from PIL import Image img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255)) # 在图片查看器中打开 # img.show()  # 保存在本地 with open('code.png','wb') as f