Image Intervention w/ Laravel 5.4 Storage

后端 未结 10 858
执笔经年
执笔经年 2021-02-05 03:16

I\'m using the storage facade to store a avatar which works fine, but I want to resize my image like I did in previous versions of laravel. How can I go about doing this? Here i

10条回答
  •  时光说笑
    2021-02-05 03:48

    I do it this way:

    1. Resize and save image somewhere (such as in the public folder).
    2. Create a new File and pass it to Laravel filesystem functions (such as putFileAs).
    3. Delete temporary intervention file

    Note: Of course you can modify it according to your needs.

    $file = $request->file('portfolio_thumb_image');
    
    $image = Image::make($file);
    
    $image->resize(570, 326, function ($constraint) {
        $constraint->aspectRatio();
    });
    
    $thumbnail_image_name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME).'.'.$file->getClientOriginalExtension();
    
    $image->save(public_path('images/'.$thumbnail_image_name));
    
    $saved_image_uri = $image->dirname.'/'.$image->basename;
    
    //Now use laravel filesystem.
    $uploaded_thumbnail_image = Storage::putFileAs('public/thumbnails/'.$portfolio_returned->id, new File($saved_image_uri), $thumbnail_image_name);
    
    //Now delete temporary intervention image as we have moved it to Storage folder with Laravel filesystem.
    $image->destroy();
    unlink($saved_image_uri);
    

提交回复
热议问题