Image Intervention w/ Laravel 5.4 Storage

后端 未结 10 961
执笔经年
执笔经年 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:50

    Make sure to add use Illuminate\Http\File; to top of your file for this to work, and read through the documentation section Automatic Streaming.

    This assumes you want all jpegs

    $path   = $request->file('createcommunityavatar');
    $resize = Image::make($path)->fit(300)->encode('jpg');
    $filePath = $resize->getRealPath() . '.jpg';
    $resize->save($filePath);
    $store  = Storage::putFile('public/image', new File($resize));
    $url    = Storage::url($store);
    

    This is how I am doing it in my application with comments to help

    // Get the file from the request
    $requestImage = request()->file('image');
    
    // Get the filepath of the request file (.tmp) and append .jpg
    $requestImagePath = $requestImage->getRealPath() . '.jpg';
    
    // Modify the image using intervention
    $interventionImage = Image::make($requestImage)->resize(125, 125)->encode('jpg');
    
    // Save the intervention image over the request image
    $interventionImage->save($requestImagePath);
    
    // Send the image to file storage
    $url = Storage::putFileAs('photos', new File($requestImagePath), 'thumbnail.jpg');
    
    return response()->json(['url' => $url]);
    

提交回复
热议问题