Laravel Image Resize and Upload using Global Function

Ashwani Garg
1 min readFeb 13, 2020

--

In this article, I’m going to explain, how you can make and use a Global function to Resize and Upload Images using third party package “intervention/image”.

Step 1:

composer require intervention/image

Step 2:

In your root directory in Terminal or Command Prompt

mkdir app/Helpers

Step 3:

Make a file under app/Helpers named Resize_Upload_Image.php

app/Helpers/Resize_Upload_Image.php

Step 4:

open composer.json, and add this file under autoload array as given below

composer.json

Step 5:

Open terminal or command prompt

composer dump-autoload

Step 6:

Open your controller file, where you want to use this function. Suppose you want to upload file in store method of your controller.

$main_directory, here is the folder under the public/images/ directory
Its best practice to keep images in specific folders for different usages.
Here I’m storing all User profiles images in the app/public/images/user_profile folder
If this folder is not created, the below code will create it automatically
You just have to provide the folder name to the $main_directory variable.

UserController.php

Wow, That’s It.

I hope, This function will help you and save your time. Please let me know, if you like this article. Happy Laravel.

For Video Tutorials, You can also visit my YouTube Channel —

https://www.youtube.com/channel/UC6np8ZmjDg0ccOwG3xU6a5Q/

--

--

Ashwani Garg
Ashwani Garg

Written by Ashwani Garg

FullStack Web, App Developer and also Full Time Blogger

Responses (1)