PHP如何实现对图片指定位置(可多个)进行打码处理

<?php
/**
* 图片通用处理逻辑类
*/
namespace common\models\logics;

use Yii;
use yii\helpers\Json;


class ImageLogic
{
/**
* PHP将网页上的图片攫取到本地存储
* @param $imgUrl 图片url地址
* @param string $saveDir 本地存储路径 默认存储在当前路径
* @param null $fileName 图片存储到本地的文件名
* @return mix
*/
public static function crabImage($imgUrl, $fileName=null, $saveDir='/tmp/remote/'){
if(empty($imgUrl)){
return false;
}

//获取图片信息大小
$imgSize = getImageSize($imgUrl);
if(!in_array($imgSize['mime'],array('image/jpg', 'image/gif', 'image/png', 'image/jpeg'),true)){
return false;
}

//获取后缀名
$_mime = explode('/', $imgSize['mime']);
$_ext = '.'.end($_mime);

if(empty($fileName)){ //生成唯一的文件名
$fileName = uniqid(time(),true).$_ext;
}else{
$fileName .= $_ext;
}

//判断你是否已经本地有该图片,如果有则直接返回
if(file_exists($saveDir.$fileName)){
return $saveDir.$fileName;
}

//开始攫取
ob_start();
readfile($imgUrl);
$imgInfo = ob_get_contents();
ob_end_clean();

if(!file_exists($saveDir)){
mkdir($saveDir,0777,true);
}
$fp = fopen($saveDir.$fileName, 'a');
$imgLen = strlen($imgInfo); //计算图片源码大小
$_inx = 204800; //每次写入200k
$_time = ceil($imgLen/$_inx);
for($i=0; $i<$_time; $i++){
fwrite($fp,substr($imgInfo, $i*$_inx, $_inx));
}
fclose($fp);

return $saveDir.$fileName;
}


/**
* 将百度AI返回的位置信息转化为大码方法可以是别的位置坐标信息
* 百度:位置数组(坐标0点为左上角)
* left:表示定位位置的长方形左上顶点的水平坐标
* top:表示定位位置的长方形左上顶点的垂直坐标
* width:表示定位位置的长方形的宽度
* height:表示定位位置的长方形的高度
*
* 大码要求的坐标信息(坐标0点为左上角)
* x1:起点横坐标
* y1:起点纵坐标
* x2:终点横坐标
* y2:终点纵坐标
*/
public static function getLocationReal($location = )
{
if(empty($location))
$position = ;

//内容周围padding
$padding = 5;

//循环计算每个位置的坐标数据
foreach($location as $key => $value){
$position[$key]['x1'] = $value['left'] - $padding;
$position[$key]['y1'] = $value['top'] - $padding;
$position[$key]['x2'] = $value['left'] + $value['width'] + $padding;
$position[$key]['y2'] = $value['top'] + $value['height'] + $padding;
}

return $position;

}


/** 图片局部打马赛克
* @param String $source 原图
* @param Stirng $target 生成的图片
* @param int $x1 起点横坐标
* @param int $y1 起点纵坐标
* @param int $x2 终点横坐标
* @param int $y2 终点纵坐标
* @param int $deep 深度,数字越大越模糊
* @return boolean
*/
public static function imageMosaics($source, $target, $x1, $y1, $x2, $y2, $deep = 6){

// 判断原图是否存在
if(!file_exists($source)){
return false;
}

// 获取原图信息
list($o_width, $o_height, $o_type) = getimagesize($source);

// 判断区域是否超出图片
if($x1>$o_width || $x1<0 || $x2>$o_width || $x2<0 || $y1>$o_height || $y1<0 || $y2>$o_height || $y2<0){
return false;
}

switch($o_type){
case 1: $source_img = imagecreatefromgif($source); break;
case 2: $source_img = imagecreatefromjpeg($source); break;
case 3: $source_img = imagecreatefrompng($source); break;
default:
return false;
}

// 打马赛克
for($x=$x1; $x<$x2; $x=$x+$deep){
for($y=$y1; $y<$y2; $y=$y+$deep){
$color = imagecolorat($source_img, $x+round($deep/2), $y+round($deep/2));
imagefilledrectangle($source_img, $x, $y, $x+$deep, $y+$deep, $color);
}
}

// 生成图片
switch($o_type){
case 1: imagegif($source_img, $target); break;
case 2: imagejpeg($source_img, $target); break;
case 3: imagepng($source_img, $target); break;
}

return is_file($target)? true : false;

}

/**
* 在图片固定位置(支持多个位置)打码
* @param String $source 原图
* @param Stirng $target 生成的图片
* @param array $position 大码位置信息,可能有多个需要打码的位置
* @param int $position->x1 起点横坐标
* @param int $position->y1 起点纵坐标
* @param int $position->x2 终点横坐标
* @param int $position->y2 终点纵坐标
* @param int $deep 深度,数字越大越模糊
* return string 返回处理后的本地图片地址,如 /tmp/convert/1a987608924ce098fc8ccd3deaac8d09.png
*/
public static function imageAddMosaics($source, $target, $position, $deep = 6)
{
//默认生成临时文件地址
$target = empty($target) ? '/tmp/
convert/'.md5(Json::encode($position)).'.png' : $target;

//将要打码的位置信息从百度模式转为本地模式
$position = self::getLocationReal($position);

//判断图片是否为远程图片
if(strpos($source,'http') !==false){
$source = self::crabImage($source,md5(Json::encode($position)));
}

//循环处理图片打码
foreach ($position as $key => $value) {

//如果是第一次进行打码操作
if($key == 0){
self::imageMosaics($source, $target, $value['x1'], $value['y1'], $value['x2'], $value['y2'], 6);
}else{
self::imageMosaics($target, $target, $value['x1'], $value['y1'], $value['x2'], $value['y2'], 6);
}
}

return $target;

}

}


//使用
use common\models\logics\ImageLogic;
$source = 'https://qiniu.zkbhj.com/common/images/jiagou/%E5%87%AF%E5%86%B0%E7%A7%91%E6%8A%80%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84.png';
$target = '';
$position = [
0 => [
'width'=>656,
'top'=>452,
'left'=>110,
'height'=>35

],
1 => [
'width'=>748,
'top'=>1400,
'left'=>188,
'height'=>39

]
];

$img = ImageLogic::imageAddMosaics($source, $target, $position);




需要用到百度AI图片文本内容识别接口,参考:http://ai.baidu.com/docs#/OCR-API/0d9adafa

请求百度图片识别接口代码示例:
<?php

/**
* 发起http post请求(REST API), 并获取REST请求的结果
* @param string $url
* @param string $param
* @return - http response body if succeeds, else false.
*/
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}

$postUrl = $url;
$curlPost = $param;
// 初始化curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_HEADER, 0);
// 要求结果为字符串且输出到屏幕上
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// post提交方式
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
// 运行curl
$data = curl_exec($curl);
curl_close($curl);

return $data;
}
//token需要通过自己的appid和secretkey获取
$token = '24.6a792a76b5edaf7b8d48f65a8843c3c5.2592000.1538639572.282335-11769895';
$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=' . $token;
$img = file_get_contents('source.png');
$img = base64_encode($img);
$bodys = array(
"image" => $img
);
$res = request_post($url, $bodys);
echo $res;

QQ截图20180905165443.jpg

 

0 个评论

要回复文章请先登录注册