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);
<?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');
Leave a reply