php 验证码图片 程序
<?php php 验证码图片 程序
/*
*文件名:class.safeCode.php
*类名:safeCode
*目的:生成web应用时所需的验证码图片
*当前版本:1.0.2
*作者:NoAngels
*联系方式:flare_1023@163.com QQ:82535599 MSN:atizu@hotmail.com
*开发时间:2008年06月20日
*最后更新:2008年06月21日
*更新内容:
*版本1.0.2
*1.更新了对linux主机支持,设置GDFONTPATH环境变量,
* putenv('GDFONTPATH=' . realpath('.'));
*2.对不支持antialias函数的时候避免采用此方法,避免低版本的GD库
*版本1.0.1
*1.更新了绘制指定类型图片,支持类型有:png,jpeg,gif.默认为png(此效果最佳)
*2.优化了部分代码
*/
class safeCode{
function __construct(){
#设置默认safe安全保护码
$this->__safe = substr(md5(time()), 0 , 4);
}
function __destruct(){
#释放资源
unset($this->__img, $this->__canvasWidth, $this->__canvasHeight, $this->__codeLength, $this->__spacePerChar, $this->__code, $this->__safe, $this->__background, $this->__borderColor, $this->__colors, $this->__font, $this->__sessionName);
}
function setCanvas($width = 200, $height = 60){
#设置画布宽度以及高度.有默认值
$this->__canvasWidth = $width;
$this->__canvasHeight = $height;
$this->__img = imagecreatetruecolor($width, $height);
return;
}
function setSafe($char){
#设置安全码,不调用系统会自己设置
$this->__safe = $char;
return;
}
function setCodeLength($num = 8){
#设置验证码长度
$this->__codeLength = $num;
}
function setFont($fontName){
#设置绘图时所用的字体,字体文件必须与类文件同目录
$this->__font = $fontName;
return;
}
function setBackground($arg1 = 255, $arg2 = 255, $arg3 = 255){
#设置背景颜色
$this->__background = imagecolorallocate($this->__img, $arg1, $arg2, $arg3);
return;
}
function setBorderColor($arg1 = 128, $arg2 = 128, $arg3 = 128){
#设置边框颜色
$this->__borderColor = imagecolorallocate($this->__img, $arg1, $arg2, $arg3);
}
function setFontColor($arr){
#设置绘图时所用颜色,参数为颜色集合数组
foreach($arr as $color){
$this->__colors[] = imagecolorallocate($this->__img, $color[0], $color[1], $color[2]);
}
return;
}
function setSession($sessionName = 'safeCode'){
#设置session变量名,默认为$_SESSION['safeCode']
$this->__sessionName = $sessionName;
#$_SESSION[$sessionName] = $this->__code;
return;
}
function display($type = 'png'){
#显示图片,可指定显示类型,目前支持png(默认),jpeg,gif
$this->__setSpacePerChar();
imagefilledrectangle($this->__img, 1, 1, $this->__canvasWidth - 2, $this->__canvasHeight -2, $this->__background);
imagerectangle($this->__img, 0, 0, $this->__canvasWidth - 1, $this->__canvasHeight - 1, $this->__borderColor);
$this->__drawText($this->__colors, $this->__getCode(), $this->__font);
#修正linux以及低版本的GD库问题
if(function_exists(imageantialias)){
imageantialias($this->__img, true);
}
$_SESSION[$this->__sessionName] = $this->__code;
switch ($type){
case 'png':
header('Content-type:image/png');
imagepng($this->__img);
case 'jpeg':
header('Content-type:image/jpeg');
imagejpeg($this->__img, '' , 75);
case 'gif':
header('Content-type:image/gif');
imagegif($this->__img);
default:
header('Content-type:image/png');
imagepng($this->__img);
}
imagegif($this->__img);
}
private function __setSpacePerChar(){
#私有函数,设置字符间隔
$this->__spacePerChar = $this->__canvasWidth / $this->__codeLength;
return;
}
private function __getCode(){
#获取验证码
$this->__code = substr(md5(time().$this->__safe), rand(0, 24), $this->__codeLength);
return $this->__code;
}
private function __drawText($colors, $code, $fontName){
#开始绘图
#设置GBFONTPATH为当前目录,不然linux环境下会报错
putenv('GDFONTPATH=' . realpath('.'));
for($i = 0; $i < strlen($code); $i++){
$color = $colors[$i % count($colors)];
imagettftext(
$this->__img,
24 + rand(0, 8),
-20 + rand(0, 40),
($i + 0.3) * $this->__spacePerChar,
$this->__canvasHeight - 20 + rand(0, 20),
$color,
$fontName,
$code{$i}
);
}
#绘制随机实线
for($i = 0; $i < 400; $i++){
$x1 = rand(5, $this->__canvasWidth - 5);
$y1 = rand(5, $this->__canvasHeight - 5);
$x2 = $x1 - 4 + rand(0, 8);
$y2 = $y1 - 4 + rand(0, 8);
imageline($this->__img, $x1, $y1, $x2, $y2, $this->__colors[rand(0, count($this->__colors) - 1)]);
}
return;
}
private $__img = NULL;
private $__canvasWidth = 120;
private $__canvasHeight = 80;
private $__codeLength = 8;
private $__spacePerChar = 0;
private $__code = 12345678;
private $__safe = 2008;
private $__background = NULL;
private $__borderColor = NULL;
private $__colors = array();
private $__font = 'arial.ttf';
private $__sessionName = '';
}
?>
使用方法.
<?php
#测试文件
include_once('class.safeCode.php');
session_start();
$safeCode = new safeCode;
$safeCode->setCanvas(200, 50);
$safeCode->setCodeLength(8);
$safeCode->setSafe('2008');#设置安全码,不设置有默认值,动态生成
$safeCode->setBackground();
$safeCode->setBorderColor();
$safeCode->setFont('arial.ttf');#设置字体.必须同目录,字体文件
$colors = array(array(128, 64, 192), array(192, 64, 128), array(108, 192, 64));
$safeCode->setFontColor($colors);
$safeCode->setSession();
$safeCode->display('jpeg');
?>
function Watermark_IM($preview = 0) {
global $watermarkstatus, $watermarktype, $watermarktrans, $watermarkquality, $watermarktext, $imageimpath;
$watermarkstatus = $GLOBALS['forum']['disablewatermark'] ? 0 : $watermarkstatus;
switch($watermarkstatus) {
case 1:
$gravity = 'NorthWest';
break;
case 2:
$gravity = 'North';
break;
case 3:
$gravity = 'NorthEast';
break;
case 4:
$gravity = 'West';
break;
case 5:
$gravity = 'Center';
break;
case 6:
$gravity = 'East';
break;
case 7:
$gravity = 'SouthWest';
break;
case 8:
$gravity = 'South';
break;
case 9:
$gravity = 'SouthEast';
break;
}
$targetfile = !$preview ? $this->targetfile : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
if($watermarktype < 2) {
$watermark_file = $watermarktype == 1 ? DISCUZ_ROOT.'./images/common/watermark.png' : DISCUZ_ROOT.'./images/common/watermark.gif';
$exec_str = $imageimpath.'/composite'.
($watermarktype != 1 && $watermarktrans != '100' ? ' -watermark '.$watermarktrans.'%' : '').
' -quality '.$watermarkquality.
' -gravity '.$gravity.
' '.$watermark_file.' '.$this->targetfile.' '.$targetfile;
} else {
$watermarktextcvt = str_replace(array("n", "r", "'"), array('', '', '''), pack("H*", $watermarktext['text']));
$watermarktext['angle'] = -$watermarktext['angle'];
$translate = $watermarktext['translatex'] || $watermarktext['translatey'] ? ' translate '.$watermarktext['translatex'].','.$watermarktext['translatey'] : '';
$skewX = $watermarktext['skewx'] ? ' skewX '.$watermarktext['skewx'] : '';
$skewY = $watermarktext['skewy'] ? ' skewY '.$watermarktext['skewy'] : '';
$exec_str = $imageimpath.'/convert'.
' -quality '.$watermarkquality.
' -font "'.$watermarktext['fontpath'].'"'.
' -pointsize '.$watermarktext['size'].
(($watermarktext['shadowx'] || $watermarktext['shadowy']) && $watermarktext['shadowcolor'] ?
' -fill "rgb('.$watermarktext['shadowcolor'].')"'.
' -draw "'.
' gravity '.$gravity.$translate.$skewX.$skewY.
' rotate '.$watermarktext['angle'].
' text '.$watermarktext['shadowx'].','.$watermarktext['shadowy'].' ''.$watermarktextcvt.''"' : '').
' -fill "rgb('.$watermarktext['color'].')"'.
' -draw "'.
' gravity '.$gravity.$translate.$skewX.$skewY.
' rotate '.$watermarktext['angle'].
' text 0,0 ''.$watermarktextcvt.''"'.
' '.$this->targetfile.' '.$targetfile;
}
@exec($exec_str, $output, $return);
if(empty($return) && empty($output)) {
$this->attach['size'] = filesize($this->targetfile);
}
}
php 生成验证码程序
<?php
$consts = 'bcdfghjkmnpqrstwxyz';
$vowels = 'aei23456789';
for ($x = 0; $x < 6; $x++)
{
$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1);
$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1);
}
$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
$_SESSION['checkcode'] = $string = substr($radomstring,0,4); //only display 4 str
/ t up image, the first number is the width and the second is the height
$imageX = strlen($radomstring)*5.5; //the image width
$imageY = 20; //the image height
$im = imagecreatetruecolor($imageX,$imageY);
//creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250));
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255)));
$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80);
$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160));
$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80);
//fill image with bgcolor
imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
//writes string
imagestring($im, 5, 3, floor(rand(0,5))-1, $string[0], $foregroundArr[rand(0,3)]);
imagestring($im, 5, 13, floor(rand(0,5))-1, $string[1], $foregroundArr[rand(0,3)]);
imagestring($im, 5, 23, floor(rand(0,5))-1, $string[2], $foregroundArr[rand(0,3)]);
imagestring($im, 5, 33, floor(rand(0,5))-1, $string[3], $foregroundArr[rand(0,3)]);
for($i=0; $i<strlen($string); $i++)
{
if(mt_rand(0,ord(substr($string,$i,1)))%2 == 0)
{
$string{$i} = ' ';
}
}
imagestring($im, 2, 2, 0, $string, $foreground2);
//strikethrough
$border = imagecolorallocate($im, 133, 153, 193);
//imagefilledrectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $back);
imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);
$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
for ($i=0;$i<20;$i++)
{
imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
}
//rotate
$middleground = imagecolorallocatealpha($im, rand(160, 200), rand(160, 200), rand(160, 200), 80);
//random shapes
for ($x=0; $x<15;$x++)
{
if(mt_rand(0,$x)%2==0)
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2);
}
else
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2);
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
}
}
//output to browser
header("content-type:image/png\r\n");
imagepng($im);
imagedestroy($im);
?>
php 图片 中文验证码
<img src="verify_image.php" alt="点此刷新验证码" name="verify_code" width="65" height="20" border="0" id="verify_code" onclick="document.getElementById('verify_code').src='verify_image.php?'+Math.random();" style="cursor:pointer;" />
<?php
session_start();
$vi = new vCodeImage;
$vi->SetImage(1,4,65,20,80,1);
class vCodeImage{
var $mode; //1:数字模式,2:字母模式,3:数字字母模式,其他:数字字母优化模式
var $v_num; //验证码个数
var $img_w; //验证码图像宽度
var $img_h; //验证码图像高度
var $int_pixel_num; //干扰像素个数
var $int_line_num; //干扰线条数
var $font_dir; //字体文件相对路径
var $border; //图像边框
var $borderColor; //图像边框颜色
function SetImage($made,$v_num,$img_w,$img_h,$int_pixel_num,$int_line_num,$font_dir='../font',$border=true,$borderColor='255,200,85'){
if(!isset($_SESSION['vCode'])){
session_register('vCode');
}
$_SESSION['vCode']="";
$this->mode = $made;
$this->v_num = $v_num;
$this->img_w = $img_w;
$this->img_h = $img_h;
$this->int_pixel_num = $int_pixel_num;
$this->int_line_num = $int_line_num;
$this->font_dir = $font_dir;
$this->border = $border;
$this->borderColor = $borderColor;
$this->GenerateImage();
}
function GetChar($mode){
if($mode == "1"){
$ychar = "0,1,2,3,4,5,6,7,8,9";
}
else if($mode == "2"){
$ychar = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
}
else if($mode == "3"){
$ychar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
}
else
$ychar = "3,4,5,6,7,8,9,A,B,C,D,H,K,P,R,S,T,W,X,Y";
return $ychar;
}
function RandColor($rs,$re,$gs,$ge,$bs,$be){
$r = mt_rand($rs,$re);
$g = mt_rand($gs,$ge);
$b = mt_rand($bs,$be);
return array($r,$g,$b);
}
function GenerateImage(){
$im = imagecreate($this->img_w,$this->img_h);
$black = imagecolorallocate($im, 0,0,0);
$white = imagecolorallocate($im, 255,255,255);
$bgcolor = imagecolorallocate($im, 250,250,250);
imagefill($im,0,0,$bgcolor);
$fonts = ScanDir($this->font_dir);
$fmax = count($fonts) - 2;
$ychar = $this->GetChar($this->mode);
$list = explode(",",$ychar);
$x = mt_rand(2,$this->img_w/($this->v_num+2));
$cmax = count($list) - 1;
$v_code = '';
for($i=0;$i<$this->v_num;$i++) //验证码
{
$randnum = mt_rand(0,$cmax);
$this_char = $list[$randnum];
$v_code .= $this_char;
$size = mt_rand(intval($this->img_w/5),intval($this->img_w/4));
$angle = mt_rand(-20,20);
$y = mt_rand(($size+2),($this->img_h-2));
if($this->border)
$y = mt_rand(($size+3),($this->img_h-3));
$rand_color = $this->RandColor(0,200,0,100,0,250);
$randcolor = imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);
$fontrand = mt_rand(2, $fmax);
$font = "$this->font_dir/".$fonts[$fontrand];
imagettftext($im, $size, $angle, $x, $y, $randcolor, $font, $this_char);
$x = $x + intval($this->img_w/($this->v_num+1));
}
for($i=0;$i<$this->int_pixel_num;$i++){//干扰像素
$rand_color = $this->RandColor(50,250,0,250,50,250);
$rand_color_pixel = imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);
imagesetpixel($im, mt_rand()%$this->img_w, mt_rand()%$this->img_h, $rand_color_pixel);
}
for($i=0;$i<$this->int_line_num;$i++){ //干扰线
$rand_color = $this->RandColor(0,250,0,250,0,250);
$rand_color_line = imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);
imageline($im, mt_rand(0,intval($this->img_w/3)), mt_rand(0,$this->img_h), mt_rand(intval($this->img_w - ($this->img_w/3)),$this->img_w), mt_rand(0,$this->img_h), $rand_color_line);
}
if($this->border) //画出边框
{
if(preg_match("/^\d{1,3},\d{1,3},\d{1,3}$/",$this->borderColor)){
$borderColor = explode(',',$this->borderColor);
}
$border_color_line = imagecolorallocate($im,$borderColor[0],$borderColor[1],$borderColor[2]);
imageline($im, 0, 0, $this->img_w, 0, $border_color_line); //上横
imageline($im, 0, 0, 0, $this->img_h, $border_color_line); //左竖
imageline($im, 0, $this->img_h-1, $this->img_w, $this->img_h-1, $border_color_line); //下横
imageline($im, $this->img_w-1, 0, $this->img_w-1, $this->img_h, $border_color_line); //右竖
}
imageantialias($im,true); //抗锯齿
$time = time();
$_SESSION['vCode'] = $v_code."|".$time; //把验证码和生成时间负值给$_SESSION[vCode]
//生成图像给浏览器
if (function_exists("imagegif")) {
header ("Content-type: image/gif");
imagegif($im);
}
elseif (function_exists("imagepng")) {
header ("Content-type: image/png");
imagepng($im);
}
elseif (function_exists("imagejpeg")) {
header ("Content-type: image/jpeg");
imagejpeg($im, "", 80);
}
elseif (function_exists("imagewbmp")) {
header ("Content-type: image/vnd.wap.wbmp");
imagewbmp($im);
}
else
die("No Image Support On This Server !");
imagedestroy($im);
}
}
?>
php截屏代码
<?php
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Navigate("http://www.111cn.net");
/* Still working? */
while ($browser->Busy) {
com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagepng($im, "iesnap.png");
?>
相关文章
使用PHP+JavaScript将HTML页面转换为图片的实例分享
这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13- 下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
- 这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
- 本文通过两个示例讲解了一下Process类调用外部应用程序的基本用法,并简单讲解了StartInfo属性,有需要的朋友可以参考一下。...2020-06-25
- 这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
- 最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
- Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
- 大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
jQuery Real Person验证码插件防止表单自动提交
本文介绍的jQuery插件有点特殊,防自动提交表单的验证工具,就是我们经常用到的验证码工具,先给大家看看效果。效果图如下: 使用说明 需要使用jQuery库文件和Real Person库文件 同时需要自定义验证码显示的CSS样式 使用实例...2015-11-08- 这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
- 这篇文章主要为大家详细介绍了JS实现随机生成验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-06
将c#编写的程序打包成应用程序的实现步骤分享(安装,卸载) 图文
时常会写用c#一些程序,但如何将他们和photoshop一样的大型软件打成一个压缩包,以便于发布....2020-06-25- 图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04