phplaravelintervention

Resizing uploaded file and saving with the same name in the same location


I try to save an image and resize with Intervention. Filesystem.php has default settings for Laravel 10.

I can successfully save under storage/app/public/image:

$file->storeAs('public/image/', $uniqueFileName);

Than I try to resize and save with same name.

$manager = new ImageManager();
$resizedImage = $manager->make(storage_path('app/public/image/' . $uniqueFileName))->resize(100, 100);
Storage::disk('public')->put(storage_path('public/image/' . $uniqueFileName), $resizedImage);

The problem is, last line creates such a folder and tries to save under this location:

/storage/app/public/Users/tolga/Desktop/project-path/storage/image

Resized images under this location are empty files actually.


Solution

  • As Aro mentioned, I removed storage_path from Storage:put method, but than I realized that I actually don't need this line, with Intervention.

    I first added as alias:

    'aliases' => Facade::defaultAliases()->merge([
        'Image' => Intervention\Image\ImageManagerStatic::class,
    

    Than, as a one liner code:

    use Image;
    
    ...
    
    Image::make(storage_path('app/public/image/' . $uniqueFileName))->fit(200, 200)->save();
    

    This resizes as a square, even the image file is not square and than saves with the same name.