php 滑动图片验证生成
1.话不多说,直接干货,喜欢的希望大家一键三连
<?php namespace App\Model; class VerifyImage
{
//浮层坐标数组
protected $tailoringArray = [];
//浮层图宽
protected $picW = 100;
//浮层图高
protected $picH = 100;
//浮层图圆半径
protected $r = 10; //截图小图路径
protected $tailoringSmallSavePath;
//截图大图路径
protected $tailoringBigSavePath; //截图小图图片名称
protected $tailoringSmallName;
//截图大图图片名称
protected $tailoringBigName; //图片后缀
protected $picSuffix = '.png'; //原图路径
protected $srcPic;
//原图信息
protected $picInfo; //图片保存路径
protected $picSavePath;
//资源图片路径
protected $resourcesPath; //初始化相关数据
public function init()
{
$this->resourcesPath = BASE_PATH . '/static/code/';
//背景图片存放目录 (Resources)
$dir = BASE_PATH . '/static/images/code/' . date('YmdH');
//被处理图片 存放目录(裁剪小图,错位背景图)
$this->tailoringBigSavePath = $dir . '/big/';
$this->tailoringSmallSavePath = $dir . '/small/';
$this->getRandomPng();
$this->tailoringSmallName = $this->tailoringSmallSavePath . md5(uniqid()) . $this->picSuffix;
$this->tailoringBigName = $this->tailoringBigSavePath . md5(uniqid() . time()) . $this->picSuffix;
//检查目录是否存在
if (!file_exists($this->tailoringBigSavePath)) {
mkdir($this->tailoringBigSavePath, 0777, true);
}
if (!file_exists($this->tailoringSmallSavePath)) {
mkdir($this->tailoringSmallSavePath, 0777, true);
}
//删除过时资源图片
$this->picSavePath = dirname($dir);
//删除一小时之前图片
$this->deleteHourXSVerificationImage($this->picSavePath);
} public function __construct()
{
$this->init();
// 获取原图的 宽高
$this->picInfo = $this->getPicWidthHeight($this->srcPic);
$this->getTailoringImage();
} // 获取图片宽、高
public function getPicWidthHeight($pic_path)
{
$lim_size = getimagesize($pic_path);
return array('w' => $lim_size[0], 'h' => $lim_size[1]);
} //随机获取参照图片
private function getRandomPng()
{
$this->srcPic = $this->resourcesPath . $this->tailoringBigName . mt_rand(1, 4) . '.png'; echo $this->srcPic;
if (!file_exists($this->srcPic)) {
throw new \Exception('图片资源不存在!!!');
} else {
return $this->srcPic;
}
} //获取浮层图片
public function getTailoringImage(){ $dr = $this->r*$this->r; //半径平方
$local_w = $this->r*2-5;
//第一个圆中心点
$circular1_x = $local_w+($this->picW-($local_w)*2)/2;
$circular1_y = $this->r;
//第二个圆中心点
$circular2_x = $this->picH-$this->r;
$circular2_y = $local_w+($this->picH-($local_w)*2)/2;; for($i=0;$i<$this->picH;$i++){
for($j=0;$j<$this->picW;$j++){
//根据公式(x-a)²+(y-b)² = r² 算出像素是否在圆内
$d1 = pow($j-$circular1_x,2)+pow($i-$circular1_y,2);
$d2 = pow($j-$circular2_x,2)+pow($i-$circular2_y,2); if(($i>=$local_w && $j>=$local_w && $i<=$this->picH-$local_w && $j<=$this->picW-$local_w) || $d1<=$dr || $d2<=$dr){
$this->tailoringArray[$i][$j] = 1; //在拼图内为1
} else {
$this->tailoringArray[$i][$j] = 0; //在拼图外为0
}
}
}
} // 创建背景图和浮层图、浮层图x,y坐标
public function createImage(){
//原图
$srcFile = $this->srcPic;
$src_w = $this->picInfo['w']; //原图宽
$src_h = $this->picInfo['h']; //原图高
$src_im = imagecreatefrompng($srcFile); $dst_im = imagecreatetruecolor($src_w,$src_h);
//根据原图尺寸创建一个相同大小的真彩色位图
//给新图填充背景色
$black = imagecolorallocate($dst_im,0,0,0);//黑
imagefill($dst_im,0,0,$black);
imagecopymerge($dst_im,$src_im,0,0,0,0,$src_w,$src_h,100);//原图图像写入新建真彩位图中 //生成透明底拼图画布
$jigsaw_im = imagecreatetruecolor($this->picW,$this->picH); //100*100拼图画布
imagealphablending($jigsaw_im,false);
imagesavealpha($jigsaw_im, true);
$jigsaw_bg = imagecolorallocatealpha($jigsaw_im,0,0,0,127);
imagefill($jigsaw_im, 0, 0, $jigsaw_bg); //随机位置
$src_x = mt_rand(215,$src_w-$this->picW); //水印位于大图x坐标
$src_y= mt_rand(0,$src_h-35); //y坐标 $this->rgb_white = imagecolorallocatealpha($dst_im,127,127,127,60); //灰色水印
//循环轮廓抠图
for($i=0;$i<$this->picH;$i++){
for ($j=0;$j<$this->picW;$j++){
if($this->tailoringArray[$i][$j] == 1){
//如果在轮廓内,取原图像素颜色
$this->rgb = ImageColorAt($dst_im,$src_x+$j,$src_y+$i);
imagesetpixel($jigsaw_im,$j,$i,$this->rgb); //在拼图画布上色
//在原图挖坑(打上灰色)
imagesetpixel($dst_im,$src_x+$j,$src_y+$i,$this->rgb_white);
}
}
} //保存图片
imagepng($dst_im,$this->tailoringBigName);
imagepng($jigsaw_im,$this->tailoringSmallName);
imagedestroy($src_im);
imagedestroy($dst_im);
imagedestroy($jigsaw_im); return ['tailoringBgImage'=>$this->tailoringBigName,'tailoringImage'=>$this->tailoringSmallName,'x'=>$src_x,'y'=>$src_y];
} // 删除一小时之前的 验证码图片
public function deleteHourXSVerificationImage($dir)
{
$op = dir($dir);
$time = date("YmdH", strtotime("-1 hours"));
$i = 0;
while (false != ($item = $op->read()) && $i <= 10) {
$i++;
if ($item == '.' || $item == '..') {
continue;
}
if (is_numeric($item) && $item <= $time || !is_numeric($item)) {
if (is_dir($op->path . '/' . $item) && rmdir($op->path . '/' . $item) == false) {
$this->deleteHourXSVerificationImage($op->path . '/' . $item);
} else {
echo $op->path . '/' . $item;
unlink($op->path . '/' . $item);
}
}
}
} }
php 滑动图片验证生成的更多相关文章
- 滑动验证 和滑动图片验证JS
滑动验证 先放效果图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- c# 简单的滑动图片验证
普通的验证码对用户使用体验不友好,出现了滑动图片验证的验证方式,用户只要按住滑块完成图片的拼接即可通过验证(这是最简单的方式,滑动轨迹,数据分析,滑行速度 什么的暂没考虑) 主要的实现思路: 1.先从 ...
- 完整说明使用SpringBoot+js实现滑动图片验证
常见的网站验证方式有手机短信验证,图片字符验证,滑块验证,滑块图片验证.本文主要讲解的是滑块图片验证的实现流程.包括后台和前端的实现. 实现效果 使用的API java.awt.image.Buffe ...
- react使用ant design pro时的滑动图片组件
react的滑动图片验证,是基于https://segmentfault.com/a/1190000018309458?utm_source=tag-newest做的修改,改动的主要有以下几点: 1. ...
- 极验反爬虫防护分析之slide验证方式下图片的处理及滑动轨迹的生成思路
本文要分享的内容是去年为了抢鞋而分析 极验(GeeTest)反爬虫防护的笔记,由于篇幅较长(为了多混点CB)我会按照我的分析顺序,分成如下四个主题与大家分享: 极验反爬虫防护分析之交互流程分析 极验反 ...
- php 图片验证码生成 前后台验证
自己从前一段时间做了个php小项目,关于生成图片验证码生成和后台的验证,把自己用到的东西总结一下,希望大家在用到相关问题的时候可以有一定的参考性. 首先,php验证码生成. 代码如下: 1.生成图像代 ...
- appium 学习各种小功能总结--功能有《滑动图片、保存截图、验证元素是否存在、》---新手总结(大牛勿喷,新手互相交流)
1.首页滑动图片点击 /** * This Method for swipe Left * 大距离滑动 width/6 除数越大向左滑动距离也越大. * width:720 *height:1280 ...
- [转]php 图片验证码生成 前后台验证
本文转自:https://www.cnblogs.com/xiaoyezi/p/3541195.html 自己从前一段时间做了个php小项目,关于生成图片验证码生成和后台的验证,把自己用到的东西总结一 ...
- Python实现图片滑动式验证识别
1 abstract 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类 ...
- 极验3.0滑动拼图验证的使用--java
[ 前言: 在登录其他网站的时候,看到有个滑动拼图的验证觉得挺好玩的,以前做一个图片验证的小demo,现在发现很多网站都开始流行滑动拼图的验证了,今天也想自己动手来弄一个. 废话不多说,开始撸起来! ...
随机推荐
- CF1404D 题解
题意 传送门 给定 \(2n\) 个数 \(1,2,\dots,2n\),A 和 B 进行交互,如下规则: A 需要将元素分成 \(n\) 组 \(\texttt{pair}\): B 从每组 \(\ ...
- WPF CommandParameter 传递多个参数的方法
1.新建一个按钮内容如下 <Button Name="btnOK" Content="确定" Height="20" Width=&q ...
- killall: command not found
centos7精简安装后,使用中发现没有killall命令.经查找,可以通过以下命令解决:yum install psmisc -y 简单介绍一下 psmisc :Psmisc软件包包含三个帮助管理/ ...
- 七、25.创建user子分支并把代码推送到码云仓库中
打开终端点击+新建一个终端 注意 :如下操作都是在2:powershell下进行 先来检查一下当前所处分支 git branch 我们应该把这些代码都写到user分支上 接下来应该把这些代码统一迁移到 ...
- M1芯片使用pod报错 *** for architecture arm64
1.添加如下配置 pod 和 工程都需要修改 2.Podfile最后添加如下代码 post_install do |installer| installer.pods_project.build_co ...
- 粉色的猫MISC(bugku)
一 题目描述 ps:本题特别感谢树叶大佬给的一些提示以及WP!欢迎大家关注树木有点绿~~ 二 解题过程 下载附件得到zip压缩包 根据作者提示,压缩包注释应该为压缩包密码. 1.压缩包密码 一开始看 ...
- ETCD 实现服务发现讲解
租约:具有时间有效期,键绑定到租约后,当租约到期失效,绑定到的租约的键也会被删除. 创建租约 etcdctl lease grant 600 lease 694d81f509b7940a grante ...
- linux 中EOF用法
EOF是END Of File的缩写,表示自定义终止符.既然自定义,那么EOF就不是固定的,可以随意设置别名,在linux按ctrl-d就代表EOF.EOF一般会配合cat能够多行文本输出.其用法如下 ...
- linux 下 配置 nginx
服务器:centOS7 安装nginx之前操作: yum install -y pcre pcre-devel // pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http ...
- WARN hdfs.DataStreamer: Caught exception
在向hdfs上传文件的时候,报了这么一个错: Exception in thread "main" java.lang.RuntimeException: org.apache.h ...