微擎 + 微信小程序内容安全接口(文字+图片内容检测)

https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Mini_Programs/Content_Security_API.html

1、图片

        global $_W;
        if (empty($_FILES['file']['name'])) {
            $this->result(1, "请选择图片");
        }
        if ($_FILES['file']['error'] != 0) {
            $this->result(1, "上传失败, 请重试");
        }

        //获取access_token
        $WxappAccount = new WxappAccount(["uniacid" => $_W['uniacid']]);
        $access_token = $WxappAccount->getAccessToken();
        $url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=".$access_token;

        //上传图片
        include_once IA_ROOT . "/framework/function/file.func.php";
        $file = file_upload($_FILES['file'], 'image');
        if (is_error($file)) {
            $this->result(1, $file['message']);
        }
        
        //图片处理
        $pathname = ATTACHMENT_ROOT .$file['path'];
        $ext = explode(".", $pathname);
        $ext = $ext[count($ext)-1];
        $tmpname = ATTACHMENT_ROOT . 'images/images.' . $ext;
        $this->resize_image($pathname,$tmpname);//把尺寸缩放到规定大小(暂时把图片放在后端,小程序在审核中没办法改)
        load()->func('communication');
        $data = array(
            'media' =>'@' . $tmpname
        );

        //调用接口
        $info =  ihttp_request($url, $data);
        $response = json_decode($info['content'], true);
        if($response['errcode']){
            if(file_exists($pathname)){
                @unlink($pathname);
            }
            if(file_exists($tmpname)){
                @unlink($tmpname);
            }
            $this->result(1, '图片含有违法违规内容');
        }

图片压缩

    public function resize_image($filename,$tmpname, $xmax=750, $ymax=1334)
    {

        $ext = explode(".", $filename);
        $ext = $ext[count($ext)-1];

        if($ext == "jpg" || $ext == "jpeg")
            $im = imagecreatefromjpeg($filename);
        elseif($ext == "png")
            $im = imagecreatefrompng($filename);
        elseif($ext == "gif")
            $im = imagecreatefromgif($filename);

        $x = imagesx($im);
        $y = imagesy($im);

        $p1 = round($x/$xmax,2);
        $p2 = round($y/$ymax,2);
        if($p1>$p2){
            $newx = $xmax;
            $newy = round($y / $p1,2);
        }else{
            $newx = round($x / $p2,2);
            $newy = $ymax;
        }


        $im2 = imagecreatetruecolor($newx, $newy);
        imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);

        if($ext == "jpg" || $ext == "jpeg")
            imagejpeg($im2, $tmpname);
        elseif($ext == "png")
            imagepng($im2, $tmpname);
        elseif($ext == "gif")
            imagegif($im2, $tmpname);
        imagedestroy($im2);
    }

2、文字

        $WxappAccount = new WxappAccount(["uniacid" => $_W['uniacid']]);
        $access_token = $WxappAccount->getAccessToken();
        $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$access_token;
        $send = array();
        $send['content'] = $data['title'];
        $send = json_encode($send,JSON_UNESCAPED_UNICODE);
        if($this->curl_post($url,$send)){
            $this->result(1, '标题含有违法违规内容');
        }

curl_post

    public function curl_post($url,$data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $data = curl_exec($ch);
        curl_close($ch);
        $return = json_decode($data, true);
        if ($return['errcode']) {
            return true;
        }else{
            return false;
        }
    }

 

你可能感兴趣的:(微擎,微信小程序,微擎,微信小程序内容安全接口)