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

Leave a Reply

Related Posts: You may like

Codeigniter pagination with search query string

Minify HTML in CodeIgniter using Hooks

Redirect http to https using htaccess

How to execute mysql query inside codeigniter helper

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

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.