~~~ //图片转base64 function imgtobase64($img='', $imgHtmlCode=true) { //$imageInfo = getimagesize($img); //return 'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode(file_get_contents($img))); $imageInfo= getimagesize($img); $base64_data = base64_encode(file_get_contents($img)); return 'data:'.$imageInfo['mime'].';base64,'.$base64_data; } ~~~ ~~~ //base64转图片 laravel用法 public function base64_img($base64_image_content) { if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ $name = date("YmdHis",time()).rand(1000,9999); $type = $result[2] ?? 'jpg';//获取图片后缀 if (file_put_contents(storage_path('app/images/'.$name.'.'.$type), base64_decode(str_replace($result[1], '', $base64_image_content)))){ return rtrim(env('APP_URL'),'/').'/images/'.$name.'.'.$type; }else{ return ''; } }else{ return ''; } } ~~~