Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Intervention Saving Image in a specific disk

I'm trying to use Intervention to resize an image, then save it in a disk that I've defined, but I can't get it to work. Using public_folder is saving it in a folder in the root of my app called /public

I've created a new disk:

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'visibility' => 'public',
        ],

        'images' => [
            'driver' => 'local',
            'root' => storage_path('app/public/images'),
            'visibility' => 'public',
        ],

    ],

And I need to accept input from the user, then save their uploaded file to that disk, then the filepath to the database.

    $fieldFile = $request->file($fieldName);
    $imageName = time();
    Image::make($fieldFile)->crop(350, 350)->save(public_path() . '/' . $imageName . '.' . $fieldFile->getClientOriginalExtension());
    $object->$fieldName = public_path() . '/' . $imageName . '.' . $fieldFile->getClientOriginalExtension();

I'd like to end up with something similar to what should happen from

$fieldValue = $fieldFile->store($fieldName, 'images');

The file should end up at

/path/to/laravel/storage/app/public/images/

Later, if I change the images disk to s3 or somewhere else, I'd like this code to continue to function.

like image 495
kurtfoster Avatar asked Feb 26 '17 00:02

kurtfoster


1 Answers

You need to encode the image like so

$fieldFile = $request->file($fieldName);

$mime= $fieldFile->getClientOriginalExtension();
$imageName = time().".".$mime;

$image = Image::make($fieldFile)->crop(350, 350)

Storage::disk('public')->put("images/".$imageName, (string) $image->encode());
like image 107
Nazalas Avatar answered Oct 23 '22 05:10

Nazalas