Image Intervention w/ Laravel 5.4 Storage

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

    You're trying to pass into putFile wrong object. That method expects File object (not Image).

    $path   = $request->file('createcommunityavatar');
    
    // returns \Intervention\Image\Image - OK
    $resize = Image::make($path)->fit(300);
    
    // expects 2nd arg - \Illuminate\Http\UploadedFile - ERROR, because Image does not have hashName method
    $store  = Storage::putFile('public/image', $resize);
    
    $url    = Storage::url($store);
    

    Ok, now when we understand the main reason, let's fix the code

    // returns Intervention\Image\Image
    $resize = Image::make($path)->fit(300)->encode('jpg');
    
    // calculate md5 hash of encoded image
    $hash = md5($resize->__toString());
    
    // use hash as a name
    $path = "images/{$hash}.jpg";
    
    // save it locally to ~/public/images/{$hash}.jpg
    $resize->save(public_path($path));
    
    // $url = "/images/{$hash}.jpg"
    $url = "/" . $path;
    

    Let's imagine that you want to use Storage facade:

    // does not work - Storage::putFile('public/image', $resize);
    
    // Storage::put($path, $contents, $visibility = null)
    Storage::put('public/image/myUniqueFileNameHere.jpg', $resize->__toString());
    
    0 讨论(0)
  • 2021-02-05 03:38

    Try updating the GD extension for the current php version.

    If that doesn't help, try saving the resized image on local disk and using Storage::putFile.

    You may delete the file once it has been uploaded to your storage path.

    The second parameter to your putFile method is an instance of the Image Intervention class. You need to pass this as the second parameter to the putFile method.

    $resize->save($absolutePath . 'small/' . $imageName);
    
    0 讨论(0)
  • 2021-02-05 03:39

    The cleanest solution I could find—using the native Storage facade—is the following. I can confirm that this works in Laravel 5.7, using intervention/image version 2.4.2.

    $file = $request->file('avatar');
    $path = $file->hashName('public/avatars');
    $image = Image::make($file)->fit(300);
    Storage::put($path, (string) $image->encode());
    
    $url = Storage::url($path);
    
    0 讨论(0)
  • 2021-02-05 03:41

    I've done it with following way, its simple and without any path confusion :

    //Get file
    $path= $request->file('createcommunityavatar');
    
    // Resize and encode to required type
    $img = Image::make($file)->fit(300)->encode('jpg');
    
    //Provide own name
    $name = time() . '.jpg';
    
    //Put file with own name
    Storage::put($name, $img);
    
    //Move file to your location 
    Storage::move($name, 'public/image/' . $name);
    
    0 讨论(0)
  • 2021-02-05 03:47

    In my case the error was caused by calling the hashName method on an image instance.

        //initially
        $image = Image::make(request('image')->getRealPath());
        $filename = $image->hashName();
    
        //to fix the error
        $image = Image::make(request('image')->getRealPath());
        $filename = request('image')->hashName();
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题