简体   繁体   中英

Laravel 8 - Intervention/image - undefined type 'Image'

I am creating a controller that saves photos to the /storage/ folder. To protect myself from submitting a bunch of large photos and not to style their CSS, I wanted to resize them using the Intervention / image library. Unfortunately, despite following the installation instructions directly from the documentation, several uninstallations and reinstallations of the library do not work. When I use this code snippet:

Use Image;

I get an error saying:

Undefined type 'Image'

Following the instructions, I added the following to /config/app.php :

    'providers' => [
        ...
        Intervention\Image\ImageServiceProvider::class,
    ],

    'aliases' => [
        ...
        'Image' => Intervention\Image\Facades\Image::class,
        ...
    ],

Besides, I cleaned and reconfigured the cache and config, restarted the server, tried to use:

use Intervention\Image\ImageManagerStatic as Image;

But unfortunately that didn't help either.

What am I doing wrong?

Use Image facade directly

\Intervention\Image\Facades\Image::make(\File::get($file_address))
    ->fit($width, $height)
    ->save($path_for_saving);

You can fit or crop the image based on your needs.

You have to use the namespace below
use Intervention\Image\Facades\Image;
Then you can use like-

$image = $request->file('image');
$ext = $image->getClientOriginalExtension();
$img = Image::make($image)->resize(300, 200)->save('storage/folder/filename'.'.'.$ext);
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;


 public function imageUploadPost(Request $request) 
 {

    $photo = $request->file('image');
    $imagename = time() . '.' . $photo->getClientOriginalExtension();

    // Upload Crop Image...
    $destinationPath = public_path('uploads/thumbnail_images');
    if (!File::isDirectory($destinationPath)) {
        File::makeDirectory($destinationPath, 0777, true, true);
    }
    $thumb_img = \Intervention\Image\Facades\Image::make($photo->getRealPath())->resize(100, 100);
    $thumb_img->save($destinationPath . '/' . $imagename, 100); //  Define Quality 100 (Optional)
    

    echo '<pre>';
    print_r("Upload Successfully. Store File : laravel_project/public/uploads & laravel_project/public/uploads/thumbnail_images");

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM