Intervention Image with Laravel 5.3 Storage

Published 1 year ago by Swaz

I like how Laravel 5.3 handles file uploads:

$path = $request->file('avatar')->store('avatars');
// avatars/bf5db5c75904dac712aea27d45320403.jpeg

Is there any way to keep this functionality while using Intervention for image processing?

// This doesn't work, but something like...
$image = Image::make($request->file('avatar'));

$image->fit(250, 250, function ($constraint) {
    $constraint->aspectRatio();
});

$path = Storage::put('avatars', $image);
Best Answer (As Selected By Swaz)
Swaz

@bobbybouwmann Thanks for the reply.

I just looked at the code for Spark (forgot it was updated to 5.3). Could use some refactoring but looks like it is possible.

$file = $request->file('avatar');

$path = $file->hashName('avatars');
// avatars/bf5db5c75904dac712aea27d45320403.jpeg

$image = Image::make($file);

$image->fit(250, 250, function ($constraint) {
    $constraint->aspectRatio();
});

Storage::put($path, (string) $image->encode());
bobbybouwmann

I don't think you can, since it's build into the framework. You need to override some methods and reregister some components to do this, as far as I can tell.

I think your better of using only the intervention package.

Swaz
Swaz
1 year ago (42,075 XP)

@bobbybouwmann Thanks for the reply.

I just looked at the code for Spark (forgot it was updated to 5.3). Could use some refactoring but looks like it is possible.

$file = $request->file('avatar');

$path = $file->hashName('avatars');
// avatars/bf5db5c75904dac712aea27d45320403.jpeg

$image = Image::make($file);

$image->fit(250, 250, function ($constraint) {
    $constraint->aspectRatio();
});

Storage::put($path, (string) $image->encode());
bobbybouwmann

You are doing the except same thing as the intervention example.. Or am I wrong?

Swaz
Swaz
1 year ago (42,075 XP)

@bobbybouwmann

It's pretty close, the only difference is this line:

Storage::put($path, (string) $image->encode());

$image needs to be encoded and cast to a string.

$path needs to include the filename.

bobbybouwmann

@Swaz You still mis the simple API like

$path = $request->file('avatar')->store('avatars');

I would expect a syntax like

$path = $request->file('avatar')->fit(250, 250)->store('avatars');

You can probably create a method for this, but it needs more setup.

I think you can stick with the way how Spark handles this ;)

Swaz
Swaz
1 year ago (42,075 XP)

@bobbybouwmann

That syntax would be amazing, though I'm not quite ready to tackle something like that.

I'm just happy I can still use the Storage facade so I can easily specify disks and such.

zachleigh
zachleigh
1 year ago (203,150 XP)

Wrap up the logic in a custom method:

public function storeImage(Request $request, $path, $name = 'image')
    {
        $imagePath = $request->file($name)->store($path);

        $name = collect(explode('/', $imagePath))->last();

        $image = Image::make(Storage::get($imagePath))->encode('jpg', 50);

        Storage::put($imagePath, $image);

        return [
            'name' => $name,
            'imagePath' => $imagePath,
        ];
    }

I keep this in a trait and use it where needed. You could expand the trait to enable more image manipulation methods.

tgif
tgif
1 year ago (16,270 XP)

@Swaz why does $image need to be encoded and cast to a string?

Isn't an image binary data?

Swaz
Swaz
1 year ago (42,075 XP)

@tgif I'm not actually sure but it won't work otherwise. Still trying to learn this stuff.

Probably has to do with $image being an instance of Intervention\Image.

Storage::put($path, $image);
// ErrorException: fstat() expects parameter 1 to be resource, object given
Storage::put($path, $image->encode());
// ErrorException: fstat() expects parameter 1 to be resource, object given
Storage::put($path, (string) $image);
// Uploads an empty file with a size of 0 bytes
Storage::put($path, (string) $image->encode());
// All goooods
tgif
tgif
1 year ago (16,270 XP)

@Swaz cool thanks

COACHTHEM

File upload to s3 with image manipulation( using image intervention library ) on laravel 5.3. Even converts image with any extension to .jpg

public function upload_pic_to_s3(Request $request){

    ini_set('memory_limit', '-1');
    ini_set('max_execution_time', 120);

    #PATH.
    $path='uploads/test/';
    $file_path=$path.time().'.jpg';

    Image::configure(array('driver' => 'gd'));

    $image = Image::make($request->file('pic'));
    $image->resize(300, 350, function ($constraint) {
        $constraint->aspectRatio();
    })
    ->encode('jpg');

    $path=\Storage::disk('s3')->put( $file_path, (string) $image );

    return $url = \Storage::disk('s3')->url($file_path);

}
wlays

@Swaz awesome looks soo much cleaner than other solutions. thank you!

proxomos

So I have this working although a symlink is required from the storage directory,

        $file = request()->file('file');
        $file->storeAs('avatars/' . auth()->user()->id, 'avatar.jpg');

        $pathtoimage = 'img/avatars/'.auth()->user()->id.'/avatar.jpg';

        $img = Image::make(request()->file('file')->getRealPath());
        $img->sharpen(9);
        $img->resize(null, 480, function ($constraint) {
            $constraint->aspectRatio();
        });
        $img->crop(320,480);
        $img->save($pathtoimage);

Symlink from storage/app/avatars to public/img/avatars:

ln -s /storage/app/avatars /public/img/avatars

Hope that helps someone.

Please sign in or create an account to participate in this conversation.