转化base64图片远程或本地图片,通过接口方式传输
转化base64图片远程或本地图片,通过接口方式传输
$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;} function n_img_base_64($img){ $imageInfo = getimagesize($img); return 'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode(file_get_contents($img)));} 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}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; }}
接受图片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;}
来源地址:https://blog.csdn.net/weixin_39934453/article/details/127994289
--结束END--
本文标题: php接口方式上传和接受图片base64
本文链接: https://lsjlt.com/news/393430.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0