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. 第二周作业N67044-张铭扬

    1. 运行脚本可以显示出本机的ip地址 2. 如果ip地址中有3这个数字,那么就打印出当前的系统时间 3. 如果ip地址中不含3这个数字,就批量建立用户magedu_00, magedu_01, .. ...

  2. javaSE学习四

    Super /*super注意点: 1. super调用父类的构造方法,必须在构造方法的第一一个 2. super必须只能出现在子类的方法或者构造方法中! 3. super和this 不能同时调用构造 ...

  3. C++程序设计实验四 继承

    程序源码: #include <iostream> #include <typeinfo> // definitation of Graph class Graph { pub ...

  4. 【剑指Offer】【树】二叉搜索树的后序遍历序列

    题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. A:在二叉树的后序遍历中,数组最后一个元素为根节点,左 ...

  5. Linux安装Jemalloc

    在安装Jemalloc之前首选安装解压工具,Jemalloc源来自Github一般服务器很少安装bzip2解压 bzip2安装命令 yum -y install bzip2 CnetOS完整安装Jem ...

  6. 使用elemeng-plus控制台报错:$weight

    使用element-plus最开始按需使用,加入的组件少没有问题,但组件引入多了以后发现控制台会报下面的警告: Deprecation Warning: $weight: Passing a numb ...

  7. 解决Idea 中Java编译器的版本自动变成1.5的问题

    可在pom文件中加入以下配置: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins ...

  8. js-惰性函数

    1. 需求:我们现在需要写一个 foo 函数,这个函数返回首次调用时的 Date 对象,注意是首次. 使用场景:当我们每次都需要进行条件判断,其实只需要判断一次,接下来的使用方式都不会发生改变的时候, ...

  9. HIVE 调优思路和实践

    1,数据存储调优 1.1 设置压缩: 设置中间数据/输出结果压缩传输,使用snappy格式. hive-site.xml: set hive.exec.compress.output = true # ...

  10. uniapp引入腾讯云直播助手插件

    在uniapp开发小程序,引入腾讯云直播助手插件 1.在manifest.json文件中,找到微信小程序特有相关,声明要使用的插件 /* 小程序特有相关 */ "mp-weixin" ...