1、转化base64图片远程或本地图片,通过接口方式传输
<?php
$path='D:\phpstudy_pro\WWW\test\1.png';
$path='https://static/common/img/icon_cf1b905.png';
$base_64=img_base64($path);
$url="http://localhost/test6.php";
$param=[
"img"=>$base_64
];
$headers[] = "Content-Type: application/json; charset=utf-8";
$res = http_post($url,json_encode($param),false,$headers,60);
var_dump($res);exit;
function img_base64($path){
//对path进行判断,如果是本地文件就二进制读取并base64编码
$img_data="";
if (substr($path,0,strlen("http")) === "http"){
$img_data= n_img_base_64($path);
}else{
$img_data= imgToBase64($path);
}
return $img_data;
}
/**
* @param $img
* @return string
* 网络图片转base64
*/
function n_img_base_64($img){
$imageInfo = getimagesize($img);
return 'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode(file_get_contents($img)));
}
/**
* @param $img_file
* @return string\
* 本地图片转base64
*/
function imgToBase64($img_file) {
$img_base64 = '';
if (file_exists($img_file)) {
$app_img_file = $img_file; // 图片路径
$img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
$fp = fopen($app_img_file, "r"); // 图片是否可读权限
if ($fp) {
$filesize = filesize($app_img_file);
$content = fread($fp, $filesize);
$file_content = chunk_split(base64_encode($content)); // base64编码
switch ($img_info[2]) { //判读图片类型
case 1: $img_type = "gif";
break;
case 2: $img_type = "jpg";
break;
case 3: $img_type = "png";
break;
}
$img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码
}
fclose($fp);
}
return $img_base64; //返回图片的base64
}
/**
* POST 请求
* @param string $url
* @param array $param
* @param boolean $post_file 是否文件上传
* @return string content
*/
function http_post($url,$param,$post_file = false,$headers = [],$outTime = 100){
$oCurl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (is_string($param) || $post_file) {
$strPOST = $param;
//exit;
} else {
$aPOST = array();
foreach($param as $key=>$val){
$aPOST[] = $key."=".urlencode($val);
}
$strPOST = join("&", $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_TIMEOUT, $outTime);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($oCurl, CURLOPT_POST,true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
if($headers){
curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);
}
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
if(intval($aStatus["http_code"])==200){
curl_close($oCurl);
return $sContent;
}else{
$err_code = curl_errno($oCurl);
if($sContent){
return $sContent;
}
curl_close($oCurl);
//exit;
return false;
}
}
2、接受图片base64并保存图片
<?php
$data=file_get_contents("php://input");
$result=json_decode($data,true);
$res=saveBase64Image($result['img'],'test');
echo json_encode($res);
function saveBase64Image($base64_image_content,$fileName)
{
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
//图片后缀
$type = $result[2];
if ($type == 'jpeg') {
$type = 'jpg';
}
//保存位置--图片名
$image_name = $fileName . '-' . date('Ymd') . date('His') . '-' . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT) . "." . $type;
$Absolute_Path = $_SERVER['SCRIPT_FILENAME'];
$Absolute_Path = substr($Absolute_Path, 0, -9);
$root = $_SERVER['DOCUMENT_ROOT'];
$image_url1 = $root.'/img/'.date('Ymd');
$image_url =$image_url1.'/'.$image_name;
$httpHost = $image_url1;
if(!is_dir(dirname($image_url1))){
if (!file_exists($image_url1)) {
mkdir($image_url1, 0777, true);
}
}
//解码
$decode = base64_decode(str_replace($result[1], '', $base64_image_content));
if (file_put_contents($image_url, $decode)) {
$data['code'] = '0';
$data['imageName'] = $image_name;
$data['image_url'] = $httpHost;
$data['type'] = $type;
$data['msg'] = '保存成功!';
} else {
$data['code'] = '1';
$data['imgageName'] = '';
$data['image_url'] = '';
$data['type'] = '';
$data['msg'] = '图片保存失败!';
}
} else {
$data['code'] = '1';
$data['imgageName'] = '';
$data['image_url'] = '';
$data['type'] = '';
$data['msg'] = 'base64图片格式有误!';
}
return $data;
}