打开config/filesystems.php ~~~ 'links' => [ public_path('storage') => storage_path('app/public'), public_path('images') => storage_path('app/images'), ], ~~~ 执行命令 ~~~ php artisan storage:link ~~~ 上传图片代码 ~~~ /** * 图片上传 * Created by WML * Created on 2021/8/3 * @param $request * @return array|false|string */ public function image(Request $request) { $file = $request->file("image"); $extension = $file->extension();//获取文件名后缀 $ext_arr = ['jpg','gif','png','jpeg','svg','JPG','GIF','PNG','JPEG','SVG','Jpg','Gif','Jpeg','Svg','Png']; if(!in_array($extension,$ext_arr)) return errorMsg(-1,"文件格式不正确"); $path = $file->store('/images'); $secure = $request->secure() ? 'https://' : 'http://'; $data = [ 'path' => $path, 'site_path' => $secure.$_SERVER['SERVER_NAME'].'/'.$path, ]; return successMsg($data); } ~~~ 对图片进行处理 ## 1.安装 使用Composer在命令行安装最新版本的Intervention Image: `composer require intervention/image` ## 2.集成到Laravel 安装好Intervention Image后,打开config/app.php,注册如下服务提供者到$providers数组: `Intervention\Image\ImageServiceProvider::class` 然后添加如下门面到$aliaes数组: `'Image' => Intervention\Image\Facades\Image::class` 这样我们就可以在Laravel应用代码中直接使用Image了。 ## 3.配置 默认情况下,Intervention Image使用PHP的GD库扩展处理所有图片,如果你想要切换到Imagick,你可以将配置文件拉到应用中: `php artisan vendor:publish --provider="Intervention\\Image\\ImageServiceProviderLaravelRecent"` ~~~ //图片上传 use Intervention\Image\ImageManagerStatic as Image; public function imageLocalUpload(Request $request){ $file = $request->file('image'); $width = $request->width ?? 80; $height = $request->height ?? 80; $ext_arr = Config::get('filesystems',array())['ext']; $extension = $file->extension();//获取文件名后缀 if(!in_array($extension,$ext_arr)) except(493,'文件格式不正确'); $name = date("YmdHis",time()).rand(1000,999); $min_path = 'images/'.$name.'_min.'.$extension; $path = $file->storeAs('/images',$name.'.'.$extension); Image::make($file)->resize($width,$height)->save($min_path); //图片压缩并保存 $url = env('APP_URL','http://'.$_SERVER['SERVER_NAME']); $data = [ 'path' => $path, 'old_url' => $url.'/'.$path,//原图 'site_path' => $url.'/'.$min_path ]; return $this->success($data); } ~~~ 更多图片处理 请访问 http://image.intervention.io/