![$blog_data[0]->title](https://theeducation.net/public/frontend/assets/blog_images/63831636a9b22image074806.jpg)
Laravel Image Intervention:
Intervention Image has optional support for Laravel and comes with a Service Provider and Facades for easy integration. In addition, laravel includes the vendor/autoload.php file, so you don’t have to require or autoload manually. Also, I have used the intervention/image package to resize the image and then save the image into the database.
Add Intervention Image Package:
This step describes how to install PHP intervention image package into the laravel application, type command in the terminal, and execute to install the plugin.
composer require intervention/image
Register Image Intervention Package:
In order to use the image intervention package in laravel, you make sure to inject the package classes into the providers and aliases arrays. Open the config/app.php file and update the following code in the file.
Intervention\Image\ImageServiceProvider::class (Add this line of code in provider array in app.php file)
'Image' => Intervention\Image\Facades\Image::class (Add this line of code in aliases array in app.php file)
After adding the above code in the mentioned files run the below command:
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravelRecent"
Code for Adding the Image:
use Image; (use image at the top of your controller)
$imageName = '';
$files = $request->file('image');
if($request->hasFile('images')) |
|
{ |
|
$images_name = 'NewLecture-' . uniqid() . '-' .$file->getClientOriginalName(); |
|
$filePath = public_path('/frontend/assets/lectures_images'); |
|
if(!File::isDirectory($filePath)){ |
|
File::makeDirectory($filePath, 0777, true, true); |
|
} |
|
$img = Image::make($file->getRealPath()); |
|
$img->save($filePath.'/'.$images_name); |
|
} |
Conclusion:
The Upload and resize an image in Laravel tutorial is ended; in this example, we explained how to resize the image and display the image preview.
We hope you liked this tutorial and surely share it with others.
shaheryar bhatti