php检测小程序上传图片是否违规

 /**
     * @Apidoc\Title("上传图片")
     * @Apidoc\Desc("上传图片")
     * @Apidoc\Url("/index/uploadpic")
     * @Apidoc\Method("get")
     * @Apidoc\Tag("")
     * @Apidoc\Header("")
     * @Apidoc\Param("filename", type="string",require=true, desc="上传图片的file名称" )
     * @Apidoc\Returned("", type="", desc="图片路径")
     */
    public function uploadpic()
    {
        $pic_path = $this->one_upload('filename');
        return $pic_path;
    }
   
     /**
     * @Apidoc\Title("图片检测")
     * @Apidoc\Desc("图片检测")
     * @Apidoc\Url("/index/imagecheck")
     * @Apidoc\Method("get")
     * @Apidoc\Tag("")
     * @Apidoc\Header("")
     * @Apidoc\Param("picpath", type="string",require=true, desc="uploadpic返回的图片路径" )
     * @Apidoc\Returned("", type="", desc="")
     */
    public function imagecheck()
    {
        $picpath=input('picpath','');
        if(empty($picpath)){ $this->BackResult(1, '请传入图片'); }
        $access_token=weixin_access_token_check();
        
        //图片检测
        $result=$this->imagecheckcall($access_token,$picpath);
        // echo '<pre>';
        // print_r($result);
       
        $this->BackResult(0, '数据结果',$result);
    }

    //图片检测
    public function imagecheckcall($access_token='',$img_path)
    {
        $img_path='./'.$img_path;
        $url='https://api.weixin.qq.com/wxa/img_sec_check?access_token='.$access_token;
        if (class_exists('\CURLFile')) {
            $josn = array('media' => new \CURLFile(realpath($img_path)));
        } else {
            $josn = array('media' => '@' . realpath($img_path));
        }
        $result=curl_call($url,'post',$josn);
        $result=json_decode($result,true);
      //print_r($result);
        return $result;
    }
 //上传图片
    public function one_upload($name)
    {
        $file = request() -> file($name);
        // echo '<pre>';
        // print_r($file);

        $saveName = Filesystem::disk('picture') -> putFile('picture', $file, time());
        $savename=str_replace('\\', '/',$saveName);
        if($savename)
        {
            return  json(array('code'=>0,'data'=>array('src'=>'/uploads/'.$savename)));
        }else{
            return  json(array('code'=>0,'msg'=>'上传失败'));
        }
    }
    
//CUrl请求调用接口
function curl_call($url,$method='get',$data='')
{
    $ch = curl_init();
    $header = array('Accept-Charset: utf-8');
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    if($error=curl_error($ch)){
        return $error;
    }
    curl_close($ch);
    return $result;
}

//微信Access_Token接口验证
function weixin_access_token_check()
{
    $info=DB::name('wx_access_token')->where(['weixin_id'=>'1'])->order('weixin_id Asc')->find();
    if(empty($info))
    {
        $result=get_weixin_access_token();
//        echo '<pre>';
//        print_r($result);
//        exit;
        if(empty($result['errcode']))
        {
            $data=array();
            $data['weixin_id']='1';
            $data['weixin_access_token']=$result['access_token'];
            $data['weixin_access_time']=time();
            DB::name('wx_access_token')->insert($data);
            return $result['access_token'];
        }
    }else{
        if(time()-$info['weixin_access_time']>=7200)
        {
            $result=get_weixin_access_token();
            if(empty($result['errcode']))
            {
                $where=array();
                $where['weixin_id']='1';

                $data=array();
                $data['weixin_id']='1';
                $data['weixin_access_token']=$result['access_token'];
                $data['weixin_access_time']=time();
                DB::name('wx_access_token')->update($data,$where);
                return $result['access_token'];
            }
        }else{
            return $info['weixin_access_token'];
        }
    }
}

function get_weixin_access_token(){
    $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.Config('app.WeiXinAPPID').'&secret='.Config('app.WeiXinAPPSECRET');
    $result=curl_call($url);
    return json_decode($result,true);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值