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

1.0  首先先看代码

 <?php
 header("Content-Type:text/html;Charset=UTF-8");// 设置页面的编码风格
 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像

 $img = imagecreatetruecolor(150,50);//创建画布并设置大小  x轴150  y轴50

 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景颜色
 imagefill($img, 0, 0, $bgcolor); ////把背景填充到图像
 imagejpeg($img);             // 输出图像
 imagedestroy($img);          // 销毁图像
 ?>

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

①  imagecreatetruecolor(); 

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

 resource imagecreatetruecolor ( int $width , int $height )

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

 resource imagecreate ( int $x_size , int $y_size )

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

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

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

②  imagecolorallocate();

imagecolorallocate — 为一幅图像分配颜色

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

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

③  mt_rand();

mt_rand — 生成更好的随机数

 int mt_rand ( int $min , int $max )

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

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

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

 <?php
 header("Content-Type:text/html;Charset=UTF-8");// 设置页面的编码风格
 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像

 $img = imagecreatetruecolor(150,50);//创建画布并设置大小  x轴150  y轴50

 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景颜色

 //添加干扰线,并循环3次,背景颜色随机
 for($i=0;$i<3;$i++){

     $linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
     imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);

 }
 //添加干扰点,并循环25次,背景颜色随机
 for($i=0;$i<25;$i++){

     $dotcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
     imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);

 }

 imagefill($img, 0, 0, $bgcolor); ////把背景填充到图像
 imagejpeg($img);             // 输出图像
 imagedestroy($img);          // 销毁图像
 ?>

函数分析:

①  imageline();

imageline — 画一条线段

 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— 画一个单一像素

 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  添加验证字母数字

 <?php
 header("Content-Type:text/html;Charset=UTF-8");// 设置页面的编码风格
 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像

 $img = imagecreatetruecolor(150,50);//创建画布并设置大小  x轴150  y轴50

 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景颜色

 //添加干扰线,并循环3次,背景颜色随机
 for($i=0;$i<3;$i++){

     $linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
     imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);

 }
 //添加干扰点,并循环25次,背景颜色随机
 for($i=0;$i<25;$i++){

     $dotcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
     imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);

 }

 //添加需要验证的字母或者数字
 $rand_str = "qwertyuiopasdfghjklzxcvbnm1234567890";//需要使用到验证的一些字母和数字
 $str_arr = array();    //命名一个数组
 for($i = 0;$i<4;$i++){    //循环4次,就是有四个随机的字母或者数字
     $pos = mt_rand(0,strlen($rand_str)-1);
     $str_arr[] = $rand_str[$pos];//临时交换
 }

 $x_start=150/4;//单个字符X轴位置

 foreach ($str_arr as $key) {
     $fontcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
     imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);
     $x_start +=20;//遍历后单个字符沿X轴 +20
 }

 imagefill($img, 0, 0, $bgcolor); ////把背景填充到图像
 imagejpeg($img);             // 输出图像
 imagedestroy($img);          // 销毁图像
 ?>

函数:

imagettftext();

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

 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-----------遍历出后的字符

效果:

看起来还是挺可爱的。

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

  1. Jsp制作验证码

    验证码 验证码(CAPTCHA)是"Completely Automated Public Turing test to tell Computers and Humans Apart&qu ...

  2. Java生成随机验证码

    package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  3. PHP利用jquery生成各种验证码和Ajax验证

    PHP生成验证码图片 PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中.PHP 生成验证码的大致流程有: .产生一张png的图片: .为图片设置背景 ...

  4. php生成动态验证码

    <?php/*调用示例*/session_start();$checkCode='';$chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ ...

  5. C#生成图形验证码

    先看效果: 再上代码 public class CaptchaHelper { private static Random rand = new Random(); private static in ...

  6. iOS 生成本地验证码

    在应用程序注册.登陆或者有关支付确认的界面,经常会用到验证码,验证码有的是通过手机发送获取的,有的是在本地点击获取的,通过手机发送获取的动态验证码可以使用第三方类库实现,本地点击获取的是在本地自己绘制 ...

  7. PHP5 GD库生成图形验证码(汉字)

    PHP5 GD库生成图形验证码且带有汉字的实例分享. 1,利用GD库函数生成图片,并在图片上写指定字符imagecreatetruecolor 新建一个真彩色图像imagecolorallocate ...

  8. (一)学习MVC之制作验证码

    制作验证码的方法在@洞庭夕照 看到的,原文链接:http://www.cnblogs.com/mzwhj/archive/2012/10/22/2720089.html 现自己利用该方法制作一个简单的 ...

  9. PHP5生成图形验证码(有汉字)

    利用PHP5中GD库生成图形验证码 类似于下面这样 1.利用GD库函数生成图片,并在图片上写指定字符 imagecreatetruecolor   新建一个真彩色图像      imagecolora ...

随机推荐

  1. 剖析iphone之触摸事件touchstart

    今天做项目发现一个问题,貌似从前没有遇到过,就记录一下,以后方便看.....(代码只显示了js这部分 样式结构都会写) 一般我们绑定click触发事件都是直接用onclick 或者方便一点click, ...

  2. 实现app上对csdn的文章查看,以及文章中图片的保存 (制作csdn app 完结篇)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24022165 今天给大家带来CSDN的完结篇,即增加文章的查看和文章中图片的保存 ...

  3. shell 中函数放回字符串问题

    shell 中函数放回字符串问题 shell 中不可以直接 return 字符串 ,可以return 数字.如果要return 字符串 改为 echo "hello world" ...

  4. poj 3253 Fence Repair(优先队列+哈夫曼树)

    题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...

  5. bash shell for循环1到100 .

    前言 用bash shell写程序时,经常会用到for循环,特别是从1到100这种需求,这里记录几种shell中从1到100的循环方法   方法 类c语言 for ((i=1; i<=100;  ...

  6. Deep Learning for NLP 文章列举

    Deep Learning for NLP 文章列举 原文链接:http://www.xperseverance.net/blogs/2013/07/2124/   大部分文章来自: http://w ...

  7. [oracle 11g 新特性] virtual column虚拟列

    总结:虚拟列可以使用于一些特殊场合,实质是类似于函数列(即以 表中已有的列 经过函数运算得来),“虚拟列不存储在数据库中,是在执行查询时由oracle后台计算出来返回给用户”,因此虚拟列不会增加存储空 ...

  8. C#扫盲之:String字符串的常用方法和冷知识

    前言 字符串对于任何编程语言都是必须操作和了解的,因为在实际编程中,任何项目和工程都必须要处理字符串数据,文件路径.提示消息,文本的处理等等,而在使用过程中很多人都是没有系统的了解,大量使用strin ...

  9. js验证

    验证短日期(2007-06-05) function strDateTime(str) {    var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1 ...

  10. Java Web应用启动间隔执行的程序

    Reference:<Java定时器timer.schedule在Web中间隔执行任务和定时><[Java]Timer和TimerTask详解> 做了一个Demo,完成如下的功 ...