How to resize image before upload in Laravel?

Member

by addison , in category: PHP Frameworks , 2 years ago

How to resize image before upload in Laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by muriel.schmidt , a year ago

@addison 

To resize an image before uploading it in Laravel, you can use the intervention/image package. This package provides a simple interface for resizing and manipulating images.


Here's an example of how you can use the intervention/image package to resize an image before uploading it in Laravel:

  1. First, install the intervention/image package by running the following command in your terminal:
1
composer require intervention/image


  1. Next, add the following line to the providers array in your config/app.php file:
1
InterventionImageImageServiceProvider::class


  1. In the same config/app.php file, add the following line to the aliases array:
1
'Image' => InterventionImageFacadesImage::class


  1. In your Laravel controller, you can now use the Image facade to open and resize the image:
1
2
3
4
5
6
7
use Image;

// Open and resize the image
$image = Image::make($request->file('image'))->resize(300, 300);

// Save the resized image
$image->save(public_path('uploads/' . $image->filename));


This code will open the image file specified in the request, resize it to 300x300 pixels, and save it to the uploads folder in the public directory.


You can also customize the resizing process by using the various methods available in the Image class. For example, you can use the fit() method to resize the image while preserving the aspect ratio, or the crop() method to crop the image to a specific size.


For more information on using the intervention/image package to manipulate images in Laravel, you can refer to the documentation at https://image.intervention.io/.

by aniya.jaskolski , 9 months ago

@addison 

To resize an image before uploading it in Laravel, you can use the Intervention Image package. Here are the steps to achieve this:

  1. Install the Intervention Image package by running the following command:
1
composer require intervention/image


  1. After installing, open the config/app.php file and add the following line to the providers array:
1
InterventionImageImageServiceProvider::class,


  1. Add the following line to the aliases array in the same configuration file:
1
'Image' => InterventionImageFacadesImage::class,


  1. Now, you can use the Intervention Image package to resize images before uploading them. Import the Image class at the top of your controller or wherever you want to use it:
1
use Image;


  1. In the upload function of your controller, add the following code to resize the image before saving it:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public function upload(Request $request)
{
    $image = $request->file('image');
    
    $filename = time() . '.' . $image->getClientOriginalExtension();
    
    $resizedImage = Image::make($image)->resize(300, 200)->save(public_path('/uploads/' . $filename));
    
    // Rest of your code to store the image path in the database, etc.
}


Here, make($image) creates an instance of the image, resize(300, 200) resizes the image to the specified dimensions (300x200 in this example), and save(...) saves the resized image to the specified path.


Make sure to adjust the path and file name as per your application's requirements.


Note: Remember to import the necessary classes at the top of your controller file:

1
2
use IlluminateHttpRequest;
use InterventionImageFacadesImage;