🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
PHP对Base64的支持非常好,有内置的base64_encode与base64_decode负责图片的Base64编码与解码。 编码上,只要将图片流读取到,而后使用base64_encode进行进行编码即可得到。 而图片编码成base64字符串后,编码内会加入这些字符 data:image/png;base64,本来是用于base64进行识别的。但是如果直接放到php里用base64_decode函数解码会导致最终保存的图片文件格式损坏,而解决方法就是先去掉这一串字符: ``` $base64_string= explode(',', $base64_string); //截取data:image/png;base64, 这个逗号后的字符 $data= base64_decode($base64_string[1]);  //对截取后的字符使用base64_decode进行解码 file_put_contents($url, $data); //写入文件并保存 ``` ### **项目应用实例** ``` /** * 构造函数定义子目录和获取配置文件中定义的上传路径 * */ public function __construct() { $this->base_upload_url = $this->getAmazonImageUploadPath();//获取配置文件中定义的路径 $this->image_path = '/sku_images'; } /** * 批量上传,返回上传路径 * @param $sku_id 子目录 * @param $thumbs base64图像文件 * @param $imageIsFile 是否是图像文件 * @throws * */ public function batchUploadFile($sku_id,$thumbs,$imageIsFile = false) { $pathName = $this->image_path.'/'.$sku_id; $result = []; $images = json_decode($thumbs); if(!empty($images)){ foreach ($images as $img){ $file_name = 'sku'.'-'.time().rand(10000,99999); $path = yield $this->uploadFile($img->data,$pathName,$file_name,$imageIsFile); $result[] ="/publish/amazon/match?path=". $path; } } return $result; } ``` ``` /** * 上传文件 * @param $baseData string * @param $pathName string * @param $fileName string * @return string */ public function uploadFile($baseData, $pathName,$fileName, $isFile = false) { if(!$baseData){ throw new ExceptionFail('未检测到文件'); } $url = $this->base_upload_url . $pathName ; $this->mkdir($url); try { $fileName = strval($fileName).'.jpg'; if(!$isFile){ $start = strpos($baseData, ','); $content = substr($baseData, $start+1); $baseData = base64_decode(str_replace(" ", "+", $content));//base64解码 file_put_contents($url.'/'.$fileName, $baseData); return $pathName.'/'.$fileName; }else{ return $baseData; } } catch (\Exception $e) { throw new ExceptionError($e->getMessage()); } } ``` ### **运行:** 参数说明:图片上传时,需要在前端转成base64格式的图片流。 ![](https://box.kancloud.cn/857775737f3a5b703ec8e2e930f6a319_1063x421.png) 这是本例中测试用的base64图片流: ``` {"data":"data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAHeUlEQVR42u2aC1RNaRTHb0n0WAt1e3gkzaqm8X5EFJphzGIUymP0mGSUipqUWpWiWIPMGMwYRtaiMRYzlgl3zeRZEqFUUsajJYZMDHlHRLVn79M9t1Puq9u90V1nr/VbJed859v/85397e/bn0DAG2+88cYbb7y1hc0TjJmH5CFPEUCeIZeQHYg3YqzNzvuKnZbHYyQJMdBGAU6Qk4c+tYcX0eYAS02Zn+WBPSFzoh2s6T2MK8QVxE7bBHhAzj2JsGCcl0bZXCtYZj6CFeE+YqtNAtSQY2+WCGUKQNTECWGD7RBWhKtIZ20R4A459WyxuVwBGBFizSCxcSQkaYsAueTQVb/eCgUgrvlbcQOjsTYIsJYcErk6KCUAkWwlCYxB7d15HWQJORNn4gT1CcoJcGSCPXdmoHiwUuOBUTxfn0YeIlXITSQdiUR6tLAtQ3p7yGXufH8prBfAHiO4sv8zKDzmCadPfwkn8+bC8fPzmZ9ncnyh6IgHPBUZQ4qnAywSjuQKUYf8ibhownk/BYkKRfJNSDcl3rgXUs7eu+QDRziYYAWVBZ0B7gsYyGF5sNfV3xNAqagLbPeyhxADF25/DiAO6hTgDDV8GJMVmq9fYTSuDLOEc542sPmjQRCoO5p98C1koIw2HNh2iBUDh0DRr6ZQ/59A4lBLBeBSdbUjHIiyhgVGzmxfXiEJiJ46BLhHjT6Pkj5VVQT3gJU9HbmJil2z+79CntP/R1o4Qd4WM+btSXNEVQFYnlzUZ0ZEgI5kNNAs07u1AtD3JTcwUSLDSVQuIB0RfWQ7+9Z/8bGD6jI9uQ60VgCWy2ldIbqXJGegbHN8awR4QQ1RIiJPhJcxZhDbzYl9aAySSb8HGbpCavJspTquLgGYz6K0IywbM4kbp7xUFYAJWvdDuyucogpn2jQJkKHCTy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