一款php生成验证码实现代码
$ico_pic 是你要给图片加水印的水印图片,其它的参数都有详细的说明,如果你下大找这类代码可以下载保存成php文件再利用后面说的调用方法来调用本生成水印图片类代码。session_save_path,可重新定义目录。
*/
$sesssavepath = dirname(__file__)."/../data/sessions/";
if(is_writeable($sesssavepath) && is_readable($sesssavepath)){ session_save_path($sesssavepath); }
session_start();
$vstr = '';
for($i=0; $i<4; $i++) $vstr .= chr(mt_rand(65,90));
if(function_exists("imagecreate")){
$ntime = time();
if(empty($_session['verifycode_last']) || empty($_session['verifycode']) || ($ntime - $_session['verifycode_last'] > 5)){
$_session['verifycode'] = strtolower($vstr);
$_session['verifycode_last'] = $ntime;
}
$vstr = $_session['verifycode'];
$vstrlen = strlen($vstr);
$img = imagecreate(50,20);
imagecolorallocate($img, 255,255,255);
$line1 = imagecolorallocate($img,240,220,180);
$line2 = imagecolorallocate($img,250,250,170);
for($j=3;$j<=16;$j=$j+3){
imageline($img,2,$j,48,$j,$line1);
}
for($j=2;$j<52;$j=$j+(mt_rand(3,6))){
imageline($img,$j,2,$j-6,18,$line2);
}
$bordercolor = imagecolorallocate($img, 0x99,0x99,0x99);
imagerectangle($img, 0, 0, 49, 19, $bordercolor);
$fontcolor = imagecolorallocate($img, 48,61,50);
for($i=0;$i<$vstrlen;$i++){
$bc = mt_rand(0,1);
$vstr[$i] = strtoupper($vstr[$i]);
imagestring($img, 5, $i*10+6, mt_rand(2,4), $vstr[$i], $fontcolor);
}header("pragma:no-cachern");
header("cache-control:no-cachern");
header("expires:0rn");if(function_exists("imagejpeg")){
header("content-type:image/jpegrn");
imagejpeg($img);
}else{
header("content-type:image/pngrn");
imagepng($img);
}
imagedestroy($img);
exit();
}
class smallpic{
private $src_pic;//原图
private $ico_pic = "003.png";//水印图
private $ico_text = "水印";//水印文字
private $small_width;//缩略图宽度
private $small_height;//缩略图高度
private $is_ico_pic = true;//是否加图片水印
private $is_text = true;//是否加文字水印
private $src_x = 20;//水印在原图的x坐标
private $src_y = 20;//水印在原图的y坐标
private $ut = "utf-8";//文字编码
private $font_color = "#990000";//文字水印颜色
private $samll_pic_name = "smallpic";//小图的名称
private $big_pic_name = "bigpic";//大图的名称
function __construct($src_pic,$small_width,$small_height){
$this->checkfile($src_pic);
$this->checkfile($this->ico_pic);
$this->src_pic = $src_pic;
$this->small_width = $small_width;
$this->small_height = $small_height;
}private function __get($property_name){
return $this->$property_name;
}private function __set($property_name,$value){
return $this->$property_name = $value;
}
/**
* 取得图片的一些基本信息,类型为array
*/
function getimageinfo($image){
return @getimagesize($image);
}/**
* 把图片加载到php中
* $image 传进来的图片
*/
function getimage($image){
$image_info = $this->getimageinfo($image);
switch($image_info[2]){
case 1:
$img = @imagecreatefromgif($image);
break;
case 2:
$img = @imagecreatefromjpeg($image);
break;
case 3:
$img = @imagecreatefrompng($image);
break;
}
return $img;
}function createimageforsuffix($big_pic,$new_pic){
$image_info = $this->getimageinfo($this->src_pic);
switch($image_info[2]){
case 1:
//输出大图
@imagegif($big_pic,$this->big_pic_name.".gif");
//输出小图
@imagegif($new_pic,$this->samll_pic_name.".gif");
break;
case 2:
//输出大图
@imagejpeg($big_pic,$this->big_pic_name.".jpg");
//输出小图
@imagejpeg($new_pic,$this->samll_pic_name.".jpg");
break;
case 3:
//输出大图
@imagepng($big_pic,$this->big_pic_name.".png");
//输出小图
@imagepng($new_pic,$this->samll_pic_name.".png");
break;
}
}function checkfile($file){
if(!file_exists($file)){
die("图片:".$file."不存在!");
}
}function createsmallimage(){
$big_pic = $this->getimage($this->src_pic);
$big_pic_info = $this->getimageinfo($this->src_pic);
$new_pic = $this->getimage($this->ico_pic);
$new_pic_info = $this->getimageinfo($this->ico_pic);
$rgb = $this->convcolor();//判断是按宽比例缩放还是按高比例缩放
if($big_pic_info[0] > $big_pic_info[1]){
$ratio = $this->small_width/(int)$big_pic_info[0];
$small_pic_width = $this->small_width;
$small_pic_height = (int)($big_pic_info[1]*$ratio);
}else{
$ratio = $this->small_height/(int)$big_pic_info[1];
$small_pic_height = $this->small_height;
$small_pic_width = (int)($big_pic_info[0]*$ratio);
}//echo $small_pic_width = (int)($big_pic_info[0]*$ratio);
//echo $small_pic_height = (int)($big_pic_info[1]*$ratio);//是否打图片水印
if ($this->is_ico_pic){
//打图片水印
@imagecopy($big_pic,$new_pic,$this->src_x,$this->src_y,0,0,$new_pic_info[0],$new_pic_info[1]);
}
//是否打文字水印
if ($this->is_text){
//设置文字颜色
$text_color = @imagecolorallocate($big_pic,$rgb[0],$rgb[1],$rgb[2]);
//转换文字编码
$text = @iconv($this->ut,"utf-8",$this->ico_text);
//打文字水印
@imagettftext($big_pic,12,0,$this->src_x,$this->src_y,$text_color,"simkai_0.ttf",$text);
}
//新建一个新图片的画板
$new_pic = @imagecreatetruecolor($small_pic_width,$small_pic_height);
//生成缩略图
@imagecopyresized($new_pic,$big_pic,0,0,0,0,$small_pic_width,$small_pic_height,$big_pic_info[0],$big_pic_info[1]);
//输出图
$this->createimageforsuffix($big_pic,$new_pic);
}/**
* 类内部的功能函数把#000000转换成255,255,255
*/
private function convcolor(){
$rgb = array();
$color = preg_replace("/#/","",$this->font_color);
$c = hexdec($color);
$r = ($c >> 16) & 0xff;
$g = ($c >> 8) & 0xff;
$b = $c & 0xff;
$rgb[0] = $r;
$rgb[1] = $g;
$rgb[2] = $b;
return $rgb;
}
}
//调用方法
$pic = new smallpic("002.jpg",600,300);
$pic->is_text = true;
$pic->is_ico_pic = true;
$pic->ico_pic = "./images/004.png";
$pic->ico_text = "新年快乐!";
//$pic->src_x = 80;
$pic->src_y = 80;
$pic->ut = "utf-8";
$pic->font_color = "#0521f8";
$pic->samll_pic_name = "hslsamll";
$pic->big_pic_name = "hslbig";
$pic->createsmallimage();
?>
1.用imagecreatetruecolor和imagecopyresampled函数分别取代imagecreate和imagecopyresized
2.给imagejpeg的第三个参数带上100(例:imagejpeg($ni,$tofile,100))
imagecreatetruecolor -- 新建一个真彩色图像
说明
resource imagecreatetruecolor ( int x_size, int y_size )
imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像
*/
header ("content-type: image/png");
$im = @imagecreatetruecolor (50, 100)
or die ("cannot initialize new gd image stream");
$text_color = imagecolorallocate ($im, 233, 14, 91);
imagestring ($im, 1, 5, 5, "a simple text string", $text_color);
imagepng ($im);
imagedestroy ($im);/*
如果使用普通的imagecreate()函数将造成图片质量失真的情况,从网上搜了一下解决办法,方法是用imagecreateruecolor()函数替换imagecreate()函数。
*/
function createpreview($img,$name,$path,$maxwidth,$maxheight,$quality){//图片,保存名称,保存路径,最大宽,最大高,质量
$widthratio=0;
$heightratio=0;
$width=imagesx($img);
$height=imagesy($img);
//开始计算缩小比例
if($width>$maxwidth||$height>$maxheight){
if($width>$maxwidth){
$widthratio=$maxwidth/$width;
}
if($height>$maxheight){
$heightratio=$maxheight/$height;
}
if($widthratio>0&&$heightratio>0){
if($widthratio<$heightratio){
$ratio=$widthratio;
}else{
$ratio=$heightratio;
}
}elseif($widthratio>0){
$ratio=$widthratio;
}elseif($heightratio>0){
$ratio=$heightratio;
}
//根据得出的比例,重新计算缩略图的宽和高
$newwidth=$ratio*$width;
$newheight=$ratio*$height;
$newimg=imagecreatetruecolor($newwidth,$newheight); // 创建目标图
imagecopyresized($newimg,$img,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($newimg,$path."s_".$name,$quality);
imagedestroy($newimg);
}else{
imagejpeg($img,$path."s_".$name,$quality);
}
}
/*
imagecopyresamples() ,其像素插值算法得到的图像边缘比较平滑.质量较好(但该函数的速度比 imagecopyresized() 慢).
在php中给图片增加水印有imagecreatefromjpeg imagecreatefrompng imagecopymerge imagejpeg就成了,只要你设置原图与水印图片就成了,下面看实例。
*/
代码如下 | 复制代码 |
header("content-type: image/jpeg"); $filename='temp/www.111cn.net/111cn.net.jpg'; $im=imagecreatefromjpeg($filename); $s=imagecreatefrompng('mb.111cn.net/pic/water_template.png'); imagecopymerge($im,$s,0,200,0,0,365,27,20); imagejpeg($im); |
本文章提供这款图片处理类,他可以做的事情有把图片生成缩略图,可能给图片增加水印以及获取图片信息,算是比较实用代码又简洁的函数*/
代码如下 | 复制代码 |
class image function __construct() function image() function watermark($image,$pos=9,$watermarkimg='images/watermark.gif',$pct=65,$text='',$w_font=5,$w_color='#ff0000') function info($image) |
相关文章
- 最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
jQuery Real Person验证码插件防止表单自动提交
本文介绍的jQuery插件有点特殊,防自动提交表单的验证工具,就是我们经常用到的验证码工具,先给大家看看效果。效果图如下: 使用说明 需要使用jQuery库文件和Real Person库文件 同时需要自定义验证码显示的CSS样式 使用实例...2015-11-08- 这篇文章主要为大家详细介绍了JS实现随机生成验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-06
- 本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
- 这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
- 这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢?当然不是,其实有两种方法可以解决。 1. 如果你只用php而不用数据库的话,那时间戳+随...2015-11-24
- 通过jquery.cookie.js插件可以快速实现“点击获取验证码后60秒内禁止重新获取(防刷新)”的功能效果图:先到官网(http://plugins.jquery.com/cookie/)下载cookie插件,放到相应文件夹,代码如下:复制代码 代码如下: <!DOCTYPE ht...2015-03-15
- 下面小编就为大家带来一篇jQuery为动态生成的select元素添加事件的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-09-01
- 经常制作开发不同的网站的后台,写过很多种不同的后台导航写法。 最终积累了这种最写法,算是最好的吧...2013-09-29
- 验证码类文件 CreateImg.class.php <?php class ValidationCode { private $width,$height,$codenum; public $checkcode; //产生的验证码 private $checkimage; //验证码图片 private $disturbColor = ''; /...2015-11-08
- 这篇文章主要介绍了基于JavaScript实现验证码功能的相关资料...2017-04-03
- js生成随机数主要用到了内置的Math对象的random()方法。用法如:Math.random()。它返回的是一个 0 ~ 1 之间的随机数。有了这么一个方法,那生成任意随机数就好理解了。比如实际中我们可能会有如下的需要: (1)生成一个 0 - 1...2015-10-21
Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法
这篇文章主要介绍了Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-06-24- 下面小编就为大家带来一篇单击按钮发送验证码,出现倒计时的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧 代码...2017-07-06
- 验证码是一个现在WEB2.0中常见的一个功能了,像注册、登录又或者是留言页面,都需要注册码来验证当前操作者的合法性,我们会看到有些网站没有验证码,但那是更高级的验证了,...2016-11-25
- 这篇文章主要介绍了基于Pytorch版yolov5的滑块验证码破解思路详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-25
- 复制代码 代码如下:function getTagStyle(){ $minFontSize=8; //最小字体大小,可根据需要自行更改 $maxFontSize=18; //最大字体大小,可根据需要自行更改 return 'font-size:'.($minFontSize+lcg_value()*(abs($maxFo...2013-10-04
- 下面小编就为大家带来一篇JS生成某个范围的随机数【四种情况详解】。小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧...2016-04-22
- 最近接到新需求,需要实现一个点击发送验证码之后,按钮禁用,在5秒之后取消禁用,看似需求很简单,实现起来还真的好好动动脑筋,下面小编把jquery控制按钮禁用核心代码分享给大家,需要的朋友参考下吧...2021-07-24