laravel --图片上传 极简版

卢晟
2023-12-01
config/filesystems.php中找到’disks’
'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => public_path('uploads'),
        ],
        //你的预配置
        'goods'=>[
            'driver' => 'local',
            'root' => public_path('uploads'),
        ],
        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
        ],

    ],
控制器代码实现

图像处理需安装第三方依赖 composer require intervention/image
修改 app/config/app.php 添加 ServiceProvider:

'providers' => [
    // ...
    Intervention\Image\ImageServiceProvider::class,
    // ...
  ],

// 将下面代码添加到 aliases 数组中
'aliases' => [
    // ...
    'Image' => Intervention\Image\Facades\Image::class,
    // ...
  ],

public function upfile(Request $request){
        $pic=config('up.pic');
        if ($request->hasFile('file')){
            //上传
            //参数2 配置节点名称
            $ret=$request->file('file')->store('goods');

            $image='/uploads/'.$ret;
            //相对路径
            $img='./uploads/'.$ret;
            //绝对路径
            //public_path().'/uploads/'.$ret
            //直接生成略缩图
            //$pic=ImageManagerStatic::make($img)->resize(100,100);
            //打开图片
            $pic=ImageManagerStatic::make($img);
            //略缩图制作
            $pic->fit(100,100);
            $pic->text('水印',50,50,function ($e){
            	//字体
                //$e->file(public_path().'/msyhl.ttc');
                //字体大小
                //$e->size(16);
                //颜色
                //$e->color('#CC0000');
                //位置
                //$e->align('center');
            });
            //保存
            $pic->save($img);
        }
        return ['status'=>0,'pic'=>$image];
    }
 类似资料: