Image resizing is a common and essential task for most projects. It is considered good practice to resize the images according to their container’s dimensions so that the page performance is not affected much. We have an open-source library in Laravel called Intervention Image that helps to manipulate and handle PHP images. It retains the image quality while resizing and allows saving the resized image.
Installation
Assuming that the Laravel project is already created and running, we install the Intervention Image library for resizing the images. The least PHP version required for this library is 5.4. So, we run the following command in the project’s root directory:
composer require intervention/image
It supports either GD library or Imagick for processing the images. So make sure one of it is available in the php environment of the project. If the GD library is missing, it can be installed by running the following command:
sudo apt-get install php7.0-gd (set the version accordingly, here php version 7.0)
Configuration
Once the library is successfully installed, go to the config/app.php file and add the service provider of the package to the $providers array as follows:
Intervention\Image\ImageServiceProvider::class
Also, add the facade to the $aliases array as below:
'Image' => Intervention\Image\Facades\Image::class
Now run the command php artisan config:cache to get the application cache cleared.
Example to follow the steps for image upload and resize
- Creating the storage link: Firstly, we create a storage directory under the public folder of the project using the following command:
php artisan storage:link
We will be storing the images under the /storage folder of the project but the images can be accessed in the /public/storage for displaying them in the frontend. - Blade image input: Let’s created a form in the view file to save the user uploaded image.
<form action="{{ route('PASS_ACTION_ROUTE') }}" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label for="image_file">Image Input</label>
<input type="file" name="image" id="image_file">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
- Controller: In the controller, we need to add the facade “Image” so suppose we have an ImagesController then the code will be as below:
<?PHP
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Image;
class ImagesController extends Controller
{
}
- Image upload and store function: We create a function viz store to resize and save the image obtained in the request. Next, create an instance of the class Image using the make method for the image that we obtained in the POST request. Then we check for the current width and height of the image so that we can allow proportionate resizing of the image in order to prevent the image from getting blurred or stretched. Suppose in the following example we want to resize the image to 400×350 pixels, we set the value in the variables.We have used the aspect ratio function to prevent the image from cutting off or stretching. In case if one wants to hard crop the image then replace the code:
public function store(Request $request)
{
//filename along with the extension
$imagename = $image->getClientOriginalName();
//image resize logic
$new_image = Image::make($image->getRealPath());
if($new_image != null){
$image_width= $new_image->width();
$image_height= $new_image->height();
$new_width= 400;
$new_height= 350;
$new_image->resize($new_width, $new_height, function ($constraint) {
$constraint->aspectRatio();
});
//saves the image into the public/storage/images folder
$store_image= $new_image->save(public_path('images/' .$filename));
//stores the image into the /storage/app/public/images folder
//$store_image= Storage::put('images/' .$filename, (string) $image->encode());
}
With$new_image->resize($new_width, $new_height, function ($constraint) {
$constraint->aspectRatio();
});
Here, we have the code to save the images into the storage directory (storage path) in the comment.$new_image->resize($new_width, $new_height);
This library has one limitation that it cannot save an image into a new directory that is it cannot create a new directory and save the image, we need to have the directory created first.
Thus, we have created the functionality to resize the images and these images will help optimize the web page by avoiding the use of large size images.
Hope the article helps!
For more such articles, check our blog list.