应用PHP GD库中图像处理函数制作验证码
2017-07-15 10:10
447 查看
<?php //开启session,之后会把验证码存入session,然后在后端与用户输入的验证码对比 session_start(); //新建一个真彩色图像 $image=imagecreatetruecolor(100, 30); //定义白色 $bgcolor=imagecolorallocate($image, 255, 255, 255); //填充颜色 imagefill($image,0,0,$bgcolor); //验证码字符串 $verify_str=''; //循环产生四位字符 for ($i=0; $i < 4; $i++) { //设置字体 $font='img/msyh.ttc'; $fontsize=16; //随机颜色 $fontcolor=imagecolorallocate($image, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120)); $contentstr='23456789acefghijkmpqrstvwxyz'; //截取字符 $fontcontent=substr($contentstr,mt_rand(0,strlen($contentstr)-1),1); $verify_str.=$fontcontent; //随机位置 $x=($i*100/4)+mt_rand(5,10); $y=mt_rand(20,25); //将文字写进图中 imagettftext($image, $fontsize, mt_rand(-30,30), $x, $y, $fontcolor, $font, $fontcontent); } //中文验证码 /*for ($i=0; $i < 4; $i++) { $font='img/msyh.ttc'; $fontsize=10; $fontcolor=imagecolorallocate($image, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120)); $contentstr='这是一段话'; $fontcontent=mb_substr($contentstr,mt_rand(0,strlen($contentstr)/3-1),1,"utf-8"); $verify_str.=$fontcontent; $x=($i*100/4)+mt_rand(5,10); $y=mt_rand(20,25); imagettftext($image, $fontsize, mt_rand(-30,30), $x, $y, $fontcolor, $font, $fontcontent); }*/ //把字符串存入session $_SESSION['verify']=$verify_str; //制作干扰项 200个点 三条线 for ($i=0; $i <200 ; $i++) { $pointcolor=imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200)); imagesetpixel($image, mt_rand(1,99), mt_rand(1,29), $pointcolor); } for ($i=0; $i < 3; $i++) { $linecolor=imagecolorallocate($image, mt_rand(80,220), mt_rand(80,220), mt_rand(80,220)); imageline($image, mt_rand(1,99), mt_rand(1,29), mt_rand(1,99), mt_rand(1,29), $linecolor); } //设置头信息,告诉浏览器输出的内容是图像 header('Content-type:image/png'); //输出图片 imagepng($image); //销毁图像 释放与 image 关联的内存。 imagedestroy($image); ?>
效果展示:
相关文章推荐
- 运用php GD库的图像函数制作登录验证码图片
- 如何用php写验证码(gd库的应用)
- 用PHP的GD库函数制作优惠券
- 应用PHP GD库制作占位图片服务
- [PHP] GD库(九)合并图像 imagecopymerge 函数(水印制作实例)
- PHP用GD库绘制图片,制作验证码浏览器不能输出
- php使用GD库制作验证码,点击验证码或看不清会刷新验证码
- 应用Javascript+GD2函数制作无刷新验证码
- PHP学习笔记之使用GD库制作验证码(一)
- PHP使用GD库制作验证码的方法(点击验证码或看不清会刷新验证码)
- php制作验证码
- PHP制作验证码
- PHP安装GD库后和图像相关的函数包括
- 利用PHP绘图函数实现简单验证码功能
- PHP GD库相关图像生成和处理函数小结
- postgresql函数中文解析 在php应用中
- php GD库函数(保存不同大小的图片,即原图与缩放图片等)
- 一段实用的php验证码函数
- GD库 验证码函数
- PHP 用session与gd库实现简单验证码生成与验证的类