Home / Snippets / How to Resize Image in Laravel 8
How to Resize Image in Laravel 8 cover

How to Resize Image in Laravel 8

2.9K

3 years ago

0 comments

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.
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this