如果图片在服务器本地
$filePath = './20200429/bd53134a6add5f1a18dd4cec85546e72.jpg';
$ret = filter_image( $filePath );
/**
* 敏感词过滤
*/
function filter_image( $content ) {
$AccessToken = getAccessToken();
$data = [];
$url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=" . $AccessToken;
$real_path=realpath($content);
$obj = new CURLFile($real_path);
$obj->setMimeType("image/jpeg");
$data['media']=$obj;
// $data = [
// "media" => $content,
// ];
$ret1 = CURLSend($url, "post", $data );
$ret = json_decode($ret1,true);
return $ret;
}
/**
* 模拟浏览器发送
*/
function CURLSend($url, $method = 'get', $data = '') {
$ch = curl_init(); //初始化
$headers = array('Accept-Charset: utf-8');
curl_setopt($ch, CURLOPT_URL, $url); //指定请求的URL
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); //提交方式
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //不验证SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //不验证SSL
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置HTTP头字段的数组
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible;MSIE5.01;Windows NT 5.0)'); //头的字符串
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); //自动设置header中的Referer:信息
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //提交数值
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //是否输出到屏幕上,true不直接输出
$temp = curl_exec($ch); //执行并获取结果
curl_close($ch);
return $temp; //return 返回值
}
如果图片是网络图片或者存在第三方
$img = file_get_contents( $content ); // img就是远程的图片地址 http的图片地址
$filePath = './1.jpg'; //注意这里是填写你保存这张远程图片在你服务器上的绝对路径地址
file_put_contents($filePath, $img);
网络图片鉴定
/*微信图片敏感内容检测*/
function imgSecCheck($img)
{
//图片地址存本地
$img = file_get_contents($img);
$filePath = './uploads/tmp1.png';
file_put_contents($filePath, $img);
resize_image($filePath, $filePath);//把尺寸缩放到规定大小(暂时把图片放在后端,小程序在审核中没办法改)
//拼接文件发送格式
$minetype = 'image/jpeg';
$curl_file = curl_file_create($filePath,$minetype);
$file['media'] = $curl_file;
$token = getAccessToken();
$url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=$token";
//发送数据
$info = CURLSend($url,"post",$file);
return json_decode($info,true);
}
//HTTP请求(支持HTTP/HTTPS,支持GET/POST)
function http_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
file_put_contents('/tmp/heka_weixin.' . date("Ymd") . '.log', date('Y-m-d H:i:s') . "\t" . $output . "\n", FILE_APPEND);
return $output;
}
// 重置图片文件大小
function resize_image($filename, $tmpname, $xmax=750, $ymax=1334)
{
$ext = explode(".", $filename);
$ext = $ext[count($ext)-1];
if($ext == "jpg" || $ext == "jpeg")
$im = imagecreatefromjpeg($tmpname);
elseif($ext == "png")
$im = imagecreatefrompng($tmpname);
elseif($ext == "gif")
$im = imagecreatefromgif($tmpname);
$x = imagesx($im);
$y = imagesy($im);
if($x <= $xmax && $y <= $ymax)
return $im;
if($x >= $y) {
$newx = $xmax;
$newy = $newx * $y / $x;
}
else {
$newy = $ymax;
$newx = $x / $y * $newy;
}
$im2 = imagecreatetruecolor($newx, $newy);
imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
return $im2;
}