Posts Learn Components Snippets Categories Tags About
/

How to Resize Image in Laravel 8

Learn how to resize image in Laravel using the Intervention Image package the easy way

Created on Sep 07, 2021

25 views

In this short snippet, you will learn how to resize images in Laravel using the Intervention Image Package.

Step 1: Install Intervention Image


First, install the intervention image to your Laravel project.
composer require intervention/image

Step 2: Load An Image From Disk Using Storage Facade


Now you will need to load the image from a disk using the Storage facade. You can do so like below. For this example, we'll be calling the "User" model but it goes for any of your models.
<?php

$userProfilePic = User::first()->profile_pic; // The path to the profile pic
$imageData = Storage::disk('public')->get($userProfilePic);

Step 3: Load image data to Intervention Image


Now that you have retrieved the image and get the image data you can pass it into the "make" method of the Intervention Image function. Once you have done that you can call the "resize()" method as follows.
<?php

// resize image to fixed size
Image::make($imageData)->resize(300, 200);
For the other available options, you can refer below.
<?php

// resize image to fixed size
$imageData->resize(300, 200);

// resize only the width of the image
$imageData->resize(300, null);

// resize only the height of the image
$imageData->resize(null, 200);

// resize the image to a width of 300 and constrain aspect ratio (auto height)
$imageData->resize(300, null, function ($constraint) {
    $constraint->aspectRatio();
});

// resize the image to a height of 200 and constrain aspect ratio (auto width)
$imageData->resize(null, 200, function ($constraint) {
    $constraint->aspectRatio();
});

// prevent possible upsizing
$imageData->resize(null, 400, function ($constraint) {
    $constraint->aspectRatio();
    $constraint->upsize();
});

// resize the image so that the largest side fits within the limit; the smaller
// side will be scaled to maintain the original aspect ratio
$imageData->resize(400, 400, function ($constraint) {
    $constraint->aspectRatio();
    $constraint->upsize();
});

Step 4: Save image to Disk


To save the image to the disk you can call the "put()" method of the storage facade.
<?php

$userProfilePic = User::first()->profile_pic; // The path to the profile pic
$imageData = Storage::disk('public')->get($userProfilePic);

// resize image to fixed size
$imageStream = Image::make($imageData)->resize(300, 200)->stream('jpeg', 100);

Storage::disk('public')->put('img/path/here.jpeg', $imageStream, 'public');
By now you will be able to see the image stored in the disk having 300 px width and 200 px height. If you found this tutorial helpful do share it with your friends and cheers.

Snippets by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

Load comments for How to Resize Image in Laravel 8

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your project.

View Components

Other Code Snippets

More