Integrating Intervention Image (Version 3) Into Your Laravel App
What is Intervention Image?
Intervention Image is an open-source PHP image handling and editing toolkit. It makes it straightforward and convenient to conduct image processing tasks including resizing, cropping, rotating, and filtering. The library is commonly used in web development projects, especially with PHP frameworks such as Laravel.
The Intervention Image Library has the following key features:
1. Image Manipulation: It allows you to effortlessly manipulate images by resizing, cropping, rotating, and flipping.
2. Image Filter: Intervention Image has several filters that may be applied to photographs, including brightness modification, contrast, grayscale, and more.
3. Image Upload Handling: The library makes it easier to handle image uploads by allowing you to do things like validate files, resize photos, and save them to the server.
4. Intervention Image supports a variety of image formats, so you can encode and decode images in multiple file formats.
5. Image Watermarking: You may quickly add watermarks to images using the library.
composer require intervention/image-laravel
Next, add the configuration files to your application using the vendor:publish
command:
php artisan vendor:publish --provider="Intervention\Image\Laravel\ServiceProvider"
This command will publish the configuration file image.php
for the image integration to your app/config
directory. In this file you can set the desired driver for Intervention Image. By default the library is configured to use GD library for image processing.
The integration is now complete and it is possible to access the ImageManager via Laravel’s facade.
use Intervention\Image\Laravel\Facades\Image;
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$image = Image::read($avatar);
// Resize image
$image->resize(300, 300, function ($constraint) {
$constraint->aspectRatio();
})->save(public_path('images/auth/icons/avatars/' . $filename));
$user->avatar = $filename;
}