PHP图片旋转验证实现思路

实现思路:PHP从图片库中随机取出一张,使用gd库裁剪并随机旋转0-360°【A】,记下旋转度数。把图片输出给前端。用户将图片旋转至正确的位置(只能朝着一侧旋转,且最大旋转值为360°),前端记下用户旋转的角度【B】传给后端。

验证思路:A = 360 - B 即正常(可以加左右模糊值)。否则验证失败。

代码如下:

<?php

namespace controllers\auth;

use Gd\Imagine;
use File;
use Image;

class ImageVerify extends ApiController {
    const ALLOW_ERROR_RANGE = 15;    // 允许的角度误差范围
    const EXPIRED_TIME = 60;        //操作过期时间
    const IMG_SIZE = 300;        //图片大小

    /**
     * 获取验证图片
     *
     * @return string
     */
    public function actionGetVerifyImage() {

        $phone = $app->request->post('phone');

        if (!$phone){
            return '参数错误';
        }

        //从图片库取图片
        //随机取图(略)
        $imgUrl = $image->url;

//生成验证码图片
        $srcImg = imagecreatefromstring(file_get_contents($imgUrl));

        $imageSize = getimagesize($imgUrl);

        $w = $imageSize[0];
        $h = $imageSize[1];

        $cw = min($w, $h);
        $ch = $cw;

        $circleImg = imagecreatetruecolor($cw, $ch);
        imagealphablending($circleImg,false );
        imagesavealpha($circleImg, true);

        $bg = imagecolorallocatealpha($circleImg, 255, 255, 255, 127);
        imagefill($circleImg, 0, 0, $bg);

        $r = $cw / 2;

        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($srcImg, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($circleImg, $x, $y, $rgbColor);
                }
            }
        }


        $ip = str_replace('.', '', $app->request->getUserIP());
        $key = $ip . $phone . date('Ymd');

        ob_start();
        imagepng($circleImg);
        $imgContent = ob_get_contents();
        ob_end_clean();
        imagedestroy($circleImg);
//旋转验证码图片
        $randAngle = rand(60, 300);

        $imagineGd = new Imagine();
        $imageInterface = $imagineGd->load($imgContent);

        $imgResource = Image::resize($imageInterface, self::IMG_SIZE, self::IMG_SIZE, true, true)->rotate($randAngle);

        $size = $imgResource->getSize()->getWidth();
        $r_new = self::IMG_SIZE / 2;
        $imgStr = Image::crop($imgResource, self::IMG_SIZE, self::IMG_SIZE, [$size / 2 - $r_new, $size / 2 - $r_new])->get('png');

        $base64ImgString = 'data:image/png;base64,' . chunk_split(base64_encode($imgStr));

        $app->cache->set($key, $randAngle, self::EXPIRED_TIME); //redis记录下旋转角度

//        header('Content-type: image/png');
//        header('Content-Transfer-Encoding: binary');

        echo $base64ImgString;
        exit();
    }

    /**
     * 验证旋转角度
     *
     * @return array|string
     * @throws \Exception
     */
    public function verifyAngle() {
        $data = $app->request->post();

        if (!$data['phone']){
            return [1, '参数错误'];
        }

        $ip = str_replace('.', '', $app->request->getUserIP());
        $key = $ip . $data['phone'] . date('Ymd');

        $angle= $app->cache->get($key); //获取redis中存下来的角度
        if (!$angle){
            return [1, '操作超时'];
        }

        $app->cache->delete($key);  //删除redis记录

        $rightAngle = 360 - $data['angle'];

        if (($angle >= ($rightAngle - self::ALLOW_ERROR_RANGE)) && ($angle <= ($rightAngle + self::ALLOW_ERROR_RANGE))){
            //验证通过........................

            return true;
        }

        return [1, '验证失败'];
    }

}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现QQ邮箱验证,可以使用PHP编程语言进行操作。下面是一个使用PHP验证QQ邮箱的示例代码: ```php <?php // 获取用户输入的邮箱地址 $email = $_POST['email']; // 正则表达式验证邮箱格式 if (!preg_match("/^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-.]+$/", $email)) { // 邮箱格式不正确 echo "邮箱格式不正确"; } else { // 使用SMTP协议连接QQ邮箱服务器 $smtpServer = "smtp.qq.com"; $username = "[email protected]"; // QQ邮箱账号 $password = "your_password"; // QQ邮箱密码 try { $smtpConnection = fsockopen($smtpServer, 25, $errno, $errstr, 5); if (!$smtpConnection) { throw new Exception($errstr, $errno); } // 响应码验证 $response = fgets($smtpConnection, 515); if (substr($response, 0, 3) != "220") { throw new Exception("连接服务器失败"); } // 发送验证命令 fputs($smtpConnection, "HELO $smtpServer\r\n"); $response = fgets($smtpConnection, 515); if (substr($response, 0, 3) != "250") { throw new Exception("HELO命令发送失败"); } fputs($smtpConnection, "AUTH LOGIN\r\n"); $response = fgets($smtpConnection, 515); if (substr($response, 0, 3) != "334") { throw new Exception("AUTH LOGIN命令发送失败"); } // 发送邮箱账号 fputs($smtpConnection, base64_encode($username) . "\r\n"); $response = fgets($smtpConnection, 515); if (substr($response, 0, 3) != "334") { throw new Exception("用户名发送失败"); } // 发送邮箱密码 fputs($smtpConnection, base64_encode($password) . "\r\n"); $response = fgets($smtpConnection, 515); if (substr($response, 0, 3) != "235") { throw new Exception("密码发送失败"); } // 发送目标邮箱地址 fputs($smtpConnection, "MAIL FROM: <$email>\r\n"); $response = fgets($smtpConnection, 515); if (substr($response, 0, 3) != "250") { throw new Exception("MAIL FROM命令发送失败"); } // 处理验证结果 $result = $response == "250 OK\r\n" ? "邮箱验证通过" : "邮箱验证失败"; echo $result; // 关闭连接 fputs($smtpConnection, "QUIT\r\n"); fclose($smtpConnection); } catch (Exception $e) { echo $e->getMessage(); } } ?> ``` 首先,通过正则表达式验证用户输入的邮箱格式是否正确。然后,使用SMTP协议连接QQ邮箱服务器,并发送验证命令,包括验证邮箱账号、密码和目标邮箱地址。根据服务器的响应,可以判断邮箱验证是否通过。最后,关闭与服务器的连接。 请注意,代码中的“[email protected]”和“your_password”需要替换为你自己的QQ邮箱账号和密码。另外,由于使用SMTP协议需要与邮件服务器进行交互,因此需要确保服务器上已开启相关的网络端口。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值