• laravel 使用微信的图片内容检测及文字内容检测


    文字内容检测

    	const SEC_LABEL = [
            100 => '正常',
            10001 => '广告',
            20001 => '时政',
            20002 => '色情',
            20003 => '辱骂',
            20006 => '违法犯罪',
            20008 => '欺诈',
            20012 => '低俗',
            20013 => '版权',
            21000 => '敏感',
        ];
        
    	/**
         * 检测文字安全
         * @param $openid   openid
         * @param $content  检测文本
         * @param $errmsg   错误信息
         */
        public static function secMsgCheck($openid, $content, &$errmsg)
        {
            $uri = 'https://api.weixin.qq.com/wxa/msg_sec_check';
            $uri = $uri . '?access_token=' . $getAccessToken;
    
            $info = [
                'content' => $content,
                'version' => 2,
                'scene' => 2,
                'openid' => $openid,
            ];
            // curl请求,方式post,参数json
            $res = CurlUtil::getJson($uri, $info);
    
            $response = json_decode($res, true);
    
            switch ($response['errcode']) {
                case 0:
                    if ($response['result'] && ($response['result']['suggest'] ?? 'pass') != 'pass') {
                        $msg_type = self::SEC_LABEL[$response['result']['label'] ?? ''] ?? '违规';
                        $errmsg = sprintf('发布失败,内容可能包含%s内容', $msg_type);
                        return false;
                    }
                    return true;
                case 87014:
                    $errmsg = '内容可能存在风险';
                    return false;
                default:
                    $errmsg = $response['errmsg'];
                    return false;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    调用

    	$flag = Util::secMsgCheck($openid, $content, $errmsg);
    	// $flag == true 及正确,否则返回 $errmsg 信息
        if (!$flag) return $this->error($errmsg);
    
    • 1
    • 2
    • 3

    图片内容检测

    微信的图片检测,需要文件大小在1M以内,这就导致用户上传大图的时候无法检测成功,思路使用PHP将文件大小压缩至1M内

    图片检测方法

    	// 图片检测是否合法
        public function checkImg(Request $request)
        {
            $file = $request->file('file');
            $extt = ['png', 'jpg', 'jpeg'];
            try {
                if (!$file->isValid()) throw new \Exception("请上传文件");
                //扩展名
                $ext = $file->getClientOriginalExtension();
                if (!in_array($ext, $extt)) throw new \Exception("图片格式不支持上传(支持png,jpg,jpeg)");
                // 文件大小
                $size = $file->getSize();
                //临时绝对路径
                $realPath = $file->getRealPath();
                // 大于1M就将图片压缩
                if ($size > 1024 * 1024 * 1) {
                    // 图片压缩
                    $img_path = public_path() . '/images/';
                    $rand_img_url = date('Ymd') . uniqid() . '.' . $ext;
                    ImagesUtil::thumb($realPath, $img_path, $rand_img_url, $ext);
                    // 微信检测图片
                    $rand_img_url = $img_path . $rand_img_url;
                    $flag = Util::secImgCheck($rand_img_url, $errmsg);
                    
                    // 如果想保留图片将就不加这段代码
                    // ******** start ********
                    unlink($rand_img_url);
                    // ********  end  ********
                    
                    if (!$flag) throw new \Exception($errmsg);
                } else {
                    $flag = Util::secImgCheck($file, $errmsg);
                    if (!$flag) throw new \Exception($errmsg);
                }
                return response()->json(['message' => ['检测成功!']]);
            } catch (\Exception $e) {
                return response()->json(['message' => [$e->getMessage()]], 422);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    Util文件

    	/**
         * 检测图片安全
         * @param $file     图片
         * @param $errmsg   错误信息
         * @return bool
         */
        public static function secImgCheckNew($file, &$errmsg)
        {
            $uri = 'https://api.weixin.qq.com/wxa/img_sec_check';
            $uri = $uri . '?access_token=' .  $getAccessToken;
    
            $real_path = realpath($file);
            $obj = new \CurlFile($real_path);
            $obj->setMimeType("image/jpeg");
    
            $info = [
                'media' => $obj,
            ];
            // curl请求,方式post,参数 FormData
            $res = CurlUtil::SeedHttp($uri, $info, true);
    
            $response = json_decode($res, true);
    
            switch ($response['errcode']) {
                case 0:
                    return true;
                case 87014:
                    $errmsg = '图片可能存在风险';
                    return false;
                case 40006:
                    $errmsg = '被检测图片内容不能超过1M';
                    return false;
                case 45002:
                    $errmsg = '被检测图片内容超过限制';
                    return false;
                default:
                    $errmsg = $response['errmsg'];
                    return false;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    ImagesUtil文件

    	/**
         * 对图片进行缩放
         * @param $filename     图片的路径
         * @param $img_path     生成图片路劲
         * @param $img_url      生成图片名称
         * @param $ext          图片后缀
         * @param int $width    设置图片缩放的宽度
         * @param int $height   设置图片缩放的高度
         */
        public static function thumb($filename, $img_path, $img_url, $ext, $width = 600, $height = 600)
        {
            // 获得原图的宽度和高度
            list($width_orig, $height_orig) = getimagesize($filename);
            // 根据参数$width和$height的值,换算出等比例的宽高
            if ($width && ($width_orig < $height_orig)) {
                $width = ($height / $height_orig) * $width_orig;
            } else {
                $height = ($width / $width_orig) * $height_orig;
            }
            // 将原图缩放到这个新创建的图片资源中
            $image_p = imagecreatetruecolor($width, $height);
            // 获取原图的图像资源
            if ($ext == 'jpg' || $ext == 'jpeg') {
                $image = imagecreatefromjpeg($filename);
            } elseif ($ext == 'png') {
                $image = imagecreatefrompng($filename);
            } else {
                $image = imagecreatefromgif($filename);
            }
    
            // 使用imagecopyresamapled()函数进行缩放设置
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
            // 将缩放后的图片保存
            $_img_url = $img_path . $img_url;
            if ($ext == 'jpg' || $ext == 'jpeg') {
                imagejpeg($image_p, $_img_url, 9);
            } elseif ($ext == 'png') {
                imagepng($image_p, $_img_url, 9);
            } else {
                imagegif($image_p, $_img_url);
            }
    
            imagedestroy($image_p);// 销毁图片资源$image_p
            imagedestroy($image);// 销毁图片资源$image
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    CurlUtil文件内容

    	public static function getJson($url = '', $param = [], $contentType = 'json')
        {
            $ch = curl_init();
            // 请求地址
            curl_setopt($ch, CURLOPT_URL, $url);
            // 请求参数类型
            $param = $contentType == 'json' ? urldecode(json_encode($param)) : http_build_query($param);
            // 关闭https验证
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            // post提交
            if ($param) {
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
            }
            // 返回的数据是否自动显示
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            // 执行并接收响应结果
            $output = curl_exec($ch);
            // 关闭curl
            curl_close($ch);
            return $output !== false ? $output : false;
        }
    
        /**
         * 发送CURL请求
         * @param $url 请求URL
         * @param $data 请求数据 无数据 == null
         * @param $ispost 是否发送POST 发送 true or false
         * @return bool|string 返回结果集
         */
        public static function SeedHttp($url, $data, $ispost)
        {
            //初使化init方法
            $ch = curl_init();
            //指定URLCURLOPT_POSTFIELDS
            curl_setopt($ch, CURLOPT_URL, $url);
            //设定请求后返回结果
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
            //声明使用POST方式来进行发送
            if ($ispost) {
                curl_setopt($ch, CURLOPT_POST, 1);
            }
    
            //发送什么数据呢
            if ($data != null || $data != '') {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            }
    
            //忽略证书
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            //忽略header头信息
            curl_setopt($ch, CURLOPT_HEADER, 0);
            //设置超时时间
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            //发送请求
            $output = curl_exec($ch);
            //关闭curl
            curl_close($ch);
            //返回数据
            return $output;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
  • 相关阅读:
    stm32通过AT指令与esp8622通信
    Spring基础之AOP和代理模式
    R语言数据结构---数据框
    表格集算表高性能原理:揭秘纯前端百万行数据秒级响应的魔法
    数字信号处理——专栏说明篇
    java-php-python-基于ASP.NETMVC的苗木购销系统计算机毕业设计
    减少Spring Boot的JVM内存占用的Docker三种配置
    【我的渲染技术进阶之旅】如何编译Filament的windows版本程序?
    第一讲 react的基础---安装 特点 组件 生命周期
    C++学习笔记——链表基础算法
  • 原文地址:https://blog.csdn.net/believer_li/article/details/138122102