file upload with resize image using codeigniter

file upload with resize image using codeigniter

In this tutorial, we are going to how to generate a thumb image or resize an image using Codeigniter. Codeigniter is a PHP framework and it provides image library such as resize, rotate, crop and watermark. you can keep validation on image upload like as size and file type.

Here, we will use the GD2 library for the resize image using Codeigniter.

Creating a Project Directory

First, we have to need the 7.2+ PHP version and Apache 2.4 . after the download latest version of CodeIgniter and set up the project on the htdocs root directory. we will also create “assets/upload/product” and “assets/upload/product/thumb” directory on the project directory for the file upload.

Autoload Configuration

we will use the database, file upload, file validation, HTML in this project application. so we will configuration in the autoload.php. see below some load helpers and library in the autoload.php

Creating Controller Class

we will create the Resizeimage.php file in the “application/controllers” directory. first, we will load the comman_model model in the constructor and after then we will crate index() and add() method.

When you the call index() method then it loads the view file and after submitting the form then call the add() method.

simply we use below library load in the add() method for the file upload and get image size in codeigniter

but, if you want to need to resize image then load the below library in the add() method.

so. you can see below source code for resizing image.

Creating  Model

here, we will create a common_modal.php in the application/models/common_modal.php.

Creating View Files

here, first, we will create the template.php file in the “application/views/” directory and add bootstrap in that file.

After then we will create an add.php file in the “application/views/product” directory, make the form with a file field and that file we pass as the middle for the template file.

 

template.php

add.php

Configuring Route

We will change the default controller as Resizeimage in the application/config/routes.php

You can download our source code

Please follow and like us: