Upload and Resize Image and Create Thumbnail Codeigniter

How to resize image in Codeigniter in simple and easy way

In this tutorial I will show you how to upload image and create thumbnail or resize image and uploload in another folder using CodeIgniter. Upload file functionality is one of the most common requirements for most of the web applications and Codeigniter providing uploading file and resize file functionality. Codeigniter file upload and resize image library is a very simple to use. Here is file Uploading and resize image step-by-step instructions on how to upload file and resize using CodeIgniter.

Step 1. Create View For Upload and Resize Image and Create Thumbnail Codeigniter

Create a view called welcome_message.php. even you dont have to create “welcome_message.php” view is alread there in it copy and past below code and save it:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Upload image and create thumnail of image</title>
</head>
<body>
	<?php if(isset($error)){echo $error;}?>
	<form method="post" enctype="multipart/form-data">
		<input type="hidden" name="uploadImage" value="1" />
		<div class="form-group">
			<label>Upload Image</label>
			<input class="form-control" type="file" name="image" />
		</div>
		<div class="form-group">
			<input class="btn btn-primary" type="submit" value="Publish" />
		</div>
	</form>
</body>
</html>

Step 2. Create controller for Upload and Resize Image and Create Thumbnail Codeigniter

Create a controller called “Welcome.php” even you dont have to create “Welcome.php” controller is alread there in it copy and past below code and save it:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
	function __construct() {
		parent::__construct();
	}
	
	public function index()
	{
		$this->load->helper(array('form', 'url'));
		if($this->input->post('uploadImage'))
		{
			$file_name = time().'-'.$_FILES["image"]['name'];
			
			$config = array(
				'upload_path' => "./uploads/",
				'allowed_types' => "gif|jpg|png|jpeg|JPEG|JPG|PNG|GIF",
				'overwrite' => TRUE,
				'max_size' => "99999999999",
				'max_height' => "800",
				'max_width' => "1500",
				'file_name' => $new_name
			);
			$this->load->library('upload', $config);
			
			// Create thumnail or resize image
			$thumb['image_library'] = 'gd2';
			$thumb['source_image'] = './uploads/'.$image;
			$thumb['new_image'] = './uploads/thumb/'; // path where you want to save thumnail
			$thumb['create_thumb'] = TRUE;
			$thumb['maintain_ratio'] = TRUE;
			$thumb['width']         = 200;
			$thumb['height']       = 200;
			$this->load->library('image_lib', $thumb);
			$this->image_lib->resize();
			$data['error'] = $this->image_lib->display_errors();
			$this->load->view('welcome_message');
		}
		else
		{
			$this->load->view('welcome_message', $data);
		}
	}
}

How it is working:
Inside controller index() function i checking if image upload form is submitted or not if image upload form is submitted then if conditon code will be execute. Inside that we set the configration for uploading file (file height, width, size, uploading path etc.) after uploading image we will execute the existing liabrary for resize image and set the configration for create thumnail (image height, width, folder path, source file path etc.).

How to execute

example.com/index.php

Tags:
resize image codeigniter upload, Upload and Resize Image using Codeigniter, resize uploaded images in CodeIgniter, How to resize an image in CodeIgniter, codeigniter image resize and save, codeigniter resize image and create thumbnail, image upload and crop in codeigniter, compress and resize uploaded image, codeigniter compress image size, image_lib codeigniter, codeigniter resize image, image upload and display in codeigniter

Mukesh Jakhar - PHP Web Developer
Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.

I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution to problems which i already solved.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.