You can adjust the dimensions of an image prior to uploading it using Laravel 5.8 by using the Intervention Image package. This package provides easy-to-use image manipulation functions that can be used to resize, crop or rotate images.
Here are the steps you can follow:
Install the Intervention Image package using Composer:
composer require intervention/image
Use the intervention function in your code by importing Intervention Image class.
use Intervention\Image\ImageManagerStatic as Image;
To resize the image, you can use the following code:
$image = Image::make($request->file('image')->getRealPath());
$image->resize(300, 200);
$image->save();
In this example, we are resizing the image to 300px width and 200px height.
To crop the image, you can use the following code:
$image = Image::make($request->file('image')->getRealPath());
$image->crop(200, 300, 0, 0);
$image->save();
In this example, we are cropping the image to 200px width and 300px height starting from (0,0) point.
Finally, you can upload the manipulated image using Laravel's default file upload functionality.
$request->file('image')->move(public_path('images'), $filename);
This is just an example of how to resize or crop images but there are several other functions offered by Intervention Image package which can be used for various image manipulation tasks.
Asked: 2022-03-04 11:00:00 +0000
Seen: 7 times
Last updated: May 09 '21