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 滑动图片验证生成的更多相关文章

  1. 滑动验证 和滑动图片验证JS

    滑动验证 先放效果图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  2. c# 简单的滑动图片验证

    普通的验证码对用户使用体验不友好,出现了滑动图片验证的验证方式,用户只要按住滑块完成图片的拼接即可通过验证(这是最简单的方式,滑动轨迹,数据分析,滑行速度 什么的暂没考虑) 主要的实现思路: 1.先从 ...

  3. 完整说明使用SpringBoot+js实现滑动图片验证

    常见的网站验证方式有手机短信验证,图片字符验证,滑块验证,滑块图片验证.本文主要讲解的是滑块图片验证的实现流程.包括后台和前端的实现. 实现效果 使用的API java.awt.image.Buffe ...

  4. react使用ant design pro时的滑动图片组件

    react的滑动图片验证,是基于https://segmentfault.com/a/1190000018309458?utm_source=tag-newest做的修改,改动的主要有以下几点: 1. ...

  5. 极验反爬虫防护分析之slide验证方式下图片的处理及滑动轨迹的生成思路

    本文要分享的内容是去年为了抢鞋而分析 极验(GeeTest)反爬虫防护的笔记,由于篇幅较长(为了多混点CB)我会按照我的分析顺序,分成如下四个主题与大家分享: 极验反爬虫防护分析之交互流程分析 极验反 ...

  6. php 图片验证码生成 前后台验证

    自己从前一段时间做了个php小项目,关于生成图片验证码生成和后台的验证,把自己用到的东西总结一下,希望大家在用到相关问题的时候可以有一定的参考性. 首先,php验证码生成. 代码如下: 1.生成图像代 ...

  7. appium 学习各种小功能总结--功能有《滑动图片、保存截图、验证元素是否存在、》---新手总结(大牛勿喷,新手互相交流)

    1.首页滑动图片点击 /** * This Method for swipe Left * 大距离滑动 width/6 除数越大向左滑动距离也越大. * width:720 *height:1280 ...

  8. [转]php 图片验证码生成 前后台验证

    本文转自:https://www.cnblogs.com/xiaoyezi/p/3541195.html 自己从前一段时间做了个php小项目,关于生成图片验证码生成和后台的验证,把自己用到的东西总结一 ...

  9. Python实现图片滑动式验证识别

    1 abstract 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类 ...

  10. 极验3.0滑动拼图验证的使用--java

    [ 前言: 在登录其他网站的时候,看到有个滑动拼图的验证觉得挺好玩的,以前做一个图片验证的小demo,现在发现很多网站都开始流行滑动拼图的验证了,今天也想自己动手来弄一个. 废话不多说,开始撸起来! ...

随机推荐

  1. Python elasticsearch_dsl 报错及解决方法

    Exception:maxClauseCount is set to 1024 原因:报错原因是Search限制一个bool查询中最多只能有1024个值或子查询,当超过1024时,会抛出异常. 解决办 ...

  2. pycharm软件基本使用python语法的注释变量的使用常量的使用变量的命名规范python的优化垃圾回收机制数据类型

    pycharm软件基本使用 1.pycharm基本的使用方法:1.点击file找到settings(设置)打开Appearance&Behavior点击Appearance看theme这个就是 ...

  3. win10bug可导致系统崩溃

    1.使用浏览器访问访问路径:\\.\globalroot\device\condrv\kernelconnect会立刻导致系统崩溃.会影响Windows10 1709及以上版本 2.使用以下代码保存成 ...

  4. MessageUtil

    1 public abstract class MessageUtil { 2 3 public static String changeMsg(CustomerReportQueryObject q ...

  5. 浅谈storm

    storm分布式,可容错的实时计算框架,低延迟能做到毫秒级的响应,storm进程是常驻内存,Hadoop是不断启停的,storm中的数据不经过磁盘,都在内存中,处理完成后就没有了,但是可以写到数据库中 ...

  6. matplotlib 在同一张图中显示两种图例

    L1=plt.legend(['ManyShot','FewShot'],loc='upper left') #保存为L1 plt.legend(['ManyShot','FewShot'],loc= ...

  7. git修改远程分支

    git remote -v 查看远程仓库 git remote rm origin 删除远程分支 git remote add git remote add origin git@codeup.ali ...

  8. 2022-6,flask+vue+uwsgi+nginx,线上部署完整流程打包配置文件

    uwsgi配置文件 [uwsgi] # 服务端口号,这里没有设置IP值,默认是加载服务器的IP地址 http = :8000 # flask项目地址 chdir = /home/flask_proje ...

  9. 用keil调试程序的时候,一点击调试就弹出STARTUP.A51那个窗口,解决办法

    前天晚上我折腾了很久 网上查了各种方法.最终自己发现,调试之前一定要在keil编译一遍,再debug这样就不会弹窗了. 另外,keil在调试过程中,修改代码是不会有任何作用的,你看我故意写错,继续单步 ...

  10. QT中文显示乱码

    1. 环境:VS2015+QT5.10 解决:在头文件中声明  #pragma  execution_character_set("utf-8") 2. QT5.10中控件显示中文 ...