php 图片滑动验证 貳

tp6方式:

注意:验证后销毁保存的图片(背景图和滑动图)

<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\index\model\User;

class Index
{
    public function index()
    {
        return '您好!这是一个[index]示例应用';
    }

    public function getValid(){

        require './../app/sdk/ImageDragAuth.php';

        $imageDragAuth = new \imageDragAuth(imagecreatefromjpeg('static/img/1.jpg'), imagecreatefrompng('static/img/3.png'), imagecreatefrompng('static/img/2.png'), 65280);
        //生成滑动图片的x坐标和y坐标
        $imageDragAuth->generator();
        //生成滑动图
        $huadong = $imageDragAuth->createDragbleImg();
        //生成背景图
        $beijing = $imageDragAuth->createBgImg();

        echo json_encode(array('status' => 0,'huadong' => '/'.$huadong,'biejing'=>'/'.$beijing,'y'=>$_SESSION['imageDragAuthY'].'px'));

    }


    /**
     * 滑动验证
     */
    public function valid(){
        require './../app/sdk/ImageDragAuth.php';

        $imageDragAuth = new \imageDragAuth(imagecreatefromjpeg('static/img/1.jpg'), imagecreatefrompng('static/img/3.png'), imagecreatefrompng('static/img/2.png'), 65280);
        $result = $imageDragAuth->validation($_GET['x'], $_GET['y']);


        echo json_encode(array('status' => $result,'msg' => $result ? '验证成功' : '验证失败','y' => $_SESSION['imageDragAuthY'].'px'));

    }
}

滑动图片验证类:

<?php
declare (strict_types = 1);

//namespace sdk;
/**
* @author: Jobs Fan 289047960@qq.com
* @copyright 2012-2017 Jobs Fan
*/
session_start();

class ImageDragAuth
{
    public $backgroundImgSrc;
    public $fillImgSrc;
    public $transparentImgSrc;
    public $colorTransparentInt;
    
    public $sessionXname;
    public $sessionYname;
    
    /**
    * 构造函数
    * @param $backgroundImgSrc 背景图的resource,可以是来自imagecreatefromjpeg imagecreatefrompng imagecreatefromgif 等
    * @param $fillImgSrc 填充图[和镂空透明图形状大小一样,不过是反的,一个填充,一个镂空]的resource,来自imagecreatefrompng
    * @param $transparentImgSrc 镂空图[和填充图形状大小一样,不过是反的,一个填充,一个镂空]的resource,来自imagecreatefrompng
    * @param $colorTransparentInt 10进制整数,要替换的颜色的数值
    * @param $sessionXname 储存x坐标的session的名字
    * @param $sessionYname 储存y坐标的session的名字
    * @return what return
    * @author Jobs Fan
    * @date: 下午3:17:54
    */
    public function __construct($backgroundImgSrc,$fillImgSrc,$transparentImgSrc,$colorTransparentInt,$sessionXname = 'imageDragAuthX',$sessionYname='imageDragAuthY')
    {
//        $backgroundImgSrc = imagecreatefromjpeg('static/img/1.jpg');
//        $fillImgSrc = imagecreatefrompng('static/img/3.png');
//        $transparentImgSrc = imagecreatefrompng('static/img/2.png');
//        $colorTransparentInt = 65280;

        $this->backgroundImgSrc = $backgroundImgSrc;
        $this->fillImgSrc = $fillImgSrc;
        $this->transparentImgSrc = $transparentImgSrc;
        $this->colorTransparentInt = $colorTransparentInt;
        
        $this->sessionXname = $sessionXname;
        $this->sessionYname = $sessionYname;
    }
    
    /**
    * 生成验证码,其实就是生成x坐标和y坐标,原理是一样的都存在服务器端的session里面
    * @param $stepSession 分步验证时候往session记录的session名
    * @return what return
    * @author Jobs Fan
    * @date: 下午3:25:20
    */
    public function generator($stepSession = 'setpSession')
    {
        $bgX = imagesx($this->backgroundImgSrc);
        $bgY = imagesy($this->backgroundImgSrc);
        $smX = imagesx($this->transparentImgSrc);
        $smY = imagesy($this->transparentImgSrc);
        
        $randX = rand(5,($bgX - $smX -5));
        $randY = rand(5,($bgY - $smY - 5));
        
        $_SESSION[$this->sessionXname] = $randX;
        $_SESSION[$this->sessionYname] = $randY;
        $_SESSION[$stepSession] = false;
    }
    
    /** 
    * 验证码验证
    * @param $x 用户通过ajax提交上来的x坐标值
    * @param $y 用户通过ajax提交上来的y坐标值
    * @param $threshold 容错阈值
    * @param $stepSession 分步验证时候往session记录的session名
    * @return boolean
    * @author Jobs Fan
    * @date: 下午3:39:37
    */
    public function validation($x,$y,$threshold=4,$stepSession = 'setpSession')
    {
        $x = (int) $x;
        $y = (int) $y;
        if (!$x || !$y || !isset($_SESSION[$this->sessionXname]) || !isset($_SESSION[$this->sessionYname]))
        {
            $this->generator();
            return false;
        }
        if ($x >= $_SESSION[$this->sessionXname] - $threshold && $x <= $_SESSION[$this->sessionXname] + $threshold && $y >= $_SESSION[$this->sessionYname] - $threshold && $y <= $_SESSION[$this->sessionYname] + $threshold)
        {
            $this->generator();
            $_SESSION[$stepSession] = true; //用户后面步骤的验证
            return true;
        }
        else 
        {
            $this->generator();
            return false;
        }
    }
    
    /**
    * 生成背景图
    * @param $x generator方法返回的x值
    * @param $y generator方法返回的y值
    * @return 直接输出图片
    * @author Jobs Fan
    * @date: 下午4:08:56
    */
    public function createBgImg()
    {
        header('Content-type: image/png');
        
        $bgX = imagesx($this->backgroundImgSrc);
        $bgY = imagesy($this->backgroundImgSrc);
        $smX = imagesx($this->fillImgSrc);
        $smY = imagesy($this->fillImgSrc);
        
        $background = imagecreatetruecolor($bgX,$bgY);
        imagecopy($background, $this->backgroundImgSrc, 0, 0, 0, 0, 868, 390);
        
        imagecopy($background, $this->fillImgSrc, (int)($_SESSION[$this->sessionXname]), (int)($_SESSION[$this->sessionYname]), 0, 0, $smX, $smY);
//        imagepng($background);
        // 把图片保存到本地
        $path = 'static/img/hua/'.date('YmdHis').rand(10000,99999).'.png';
        imagepng($background,$path);
        //销毁内存中的图片
        imagedestroy($background);

        $path = str_replace("\\", '/', $path);
        return $path;
    }
    
    /**
    * 生成前景图,也就是鼠标拖动的图
    * @param $x generator方法返回的x值
    * @param $y generator方法返回的y值
    * @return 直接输出图片
    * @author Jobs Fan
    * @date: 下午4:15:25
    */
    public function createDragbleImg()
    {
        header('Content-type: image/png');
      
        //imagesx — 取得图像宽度
      //imagesy — 取得图像高度
      //用法  int imagesy ( resource $image )
      
        $bgX = imagesx($this->backgroundImgSrc);   //868  
        $bgY = imagesy($this->backgroundImgSrc);   //390
        $smX = imagesx($this->transparentImgSrc);  //149
        $smY = imagesy($this->transparentImgSrc);  //149
      
        //imagecreatetruecolor — 新建一个真彩色图像
      //用法 resource imagecreatetruecolor ( int $width , int $height )
      
        $background = imagecreatetruecolor($bgX,$bgY);
        
      //
        imagecopy($background, $this->backgroundImgSrc, 0, 0, 0, 0, $bgX, $bgY);
        imagecopy($background, $this->transparentImgSrc, (int)($_SESSION[$this->sessionXname]), (int)($_SESSION[$this->sessionYname]), 0, 0, $smX, $smY);

        
      $imgCrop = imagecrop($background, array('x' => (int)($_SESSION[$this->sessionXname]),'y' => (int)($_SESSION[$this->sessionYname]), 'width' => $smX, 'height' => $smY));

        imagecolortransparent($imgCrop,65280);

        // 把图片保存到本地
        $path = 'static/img/hua/'.date('YmdHis').rand(10000,99999).'.png';
        imagepng($imgCrop,$path);
        //销毁
        imagedestroy($imgCrop);

        $path = str_replace("\\", '/', $path);
        return  $path;


    }

    /**
     * @name 删除 验证码图片
     */
    public function delete_img($path)
    {
        unlink($path);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于PHP图片上传及验证,可以按照以下步骤进行操作: 1. 在HTML表单中添加多个文件上传字段: ```html <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="image[]" multiple> <input type="submit" value="Upload"> </form> ``` 2. 在服务器端创建一个名为upload.php的文件来处理上传请求: ```php <?php if(isset($_FILES['image'])){ $errors = []; $uploadedFiles = []; $extension = ['jpg', 'jpeg', 'png', 'gif']; // 获取上传文件信息 $fileNames = $_FILES['image']['name']; $fileTmps = $_FILES['image']['tmp_name']; $fileSizes = $_FILES['image']['size']; $fileErrors = $_FILES['image']['error']; // 遍历每个上传文件 foreach($fileNames as $key => $fileName){ $fileTmp = $fileTmps[$key]; $fileSize = $fileSizes[$key]; $fileError = $fileErrors[$key]; // 检查文件是否上传成功 if($fileError === UPLOAD_ERR_OK){ // 检查文件大小和类型 $fileExt = pathinfo($fileName, PATHINFO_EXTENSION); if(!in_array($fileExt, $extension)){ $errors[] = "Invalid file extension for $fileName"; } elseif($fileSize > 5000000){ $errors[] = "File size exceeds limit for $fileName"; } else{ // 生成唯一的文件名并移动到目标文件夹 $newFileName = uniqid().'.'.$fileExt; $uploadPath = 'uploads/'.$newFileName; move_uploaded_file($fileTmp, $uploadPath); $uploadedFiles[] = $uploadPath; } } else{ $errors[] = "Error uploading $fileName"; } } // 输出结果 if(!empty($errors)){ print_r($errors); } elseif(!empty($uploadedFiles)){ print_r($uploadedFiles); } } ?> ``` 上述代码会将上传的图片保存到名为"uploads"的文件夹中,并输出上传成功的文件路径或错误信息。 需要注意的是,上述代码仅提供了基本的文件类型和大小验证,您可以根据实际需求进行进一步的验证和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值