Sunday, 6 November 2016

CodeIgniter Tutorial - Compress and Resize Uploaded Image

Hello friends in previous post on codeigniter tutorial, we have discuss on how to upload image using ajax with jquery in Codeigniter application. Now in this post we will discuss how to compress image and how to resize uploaded image and display that image on web page without page refresh. In this post we have first load image library GD2 with predefine option. Method of This image library will compress and resize uploaded image and store into upload folder. Then after we have store image name under database and then after we have fetch image name from database and display all images on web page after successfully image uploaded with out page refresh. This way we have make simple image gallery in codeigniter framework, in this gallery we have compress and resize uploaded image without page refresh using ajax request. So this is my tutorial on how to compress and resize uploaded image in Codeigniter framework.

Source Code

Controllers - main.php

 function image_upload()  
           $data['title'] = "Upload Image using Ajax JQuery in CodeIgniter";  
           $data["image_data"] = $this->main_model->fetch_image();  
           $this->load->view('image_upload', $data);  
      function ajax_upload()  
                $config['upload_path'] = './upload/';  
                $config['allowed_types'] = 'jpg|jpeg|png|gif';  
                $this->load->library('upload', $config);  
                     echo $this->upload->display_errors();  
                     $data = $this->upload->data();  
                     $config['image_library'] = 'gd2';  
                     $config['source_image'] = './upload/'.$data["file_name"];  
                     $config['create_thumb'] = FALSE;  
                     $config['maintain_ratio'] = FALSE;  
                     $config['quality'] = '60%';  
                     $config['width'] = 200;  
                     $config['height'] = 200;  
                     $config['new_image'] = './upload/'.$data["file_name"];  
                     $this->load->library('image_lib', $config);  
                     $image_data = array(  
                          'name'          =>     $data["file_name"]  
                     echo $this->main_model->fetch_image();  
                     //echo '<img src="'.base_url().'upload/'.$data["file_name"].'" width="300" height="225" class="img-thumbnail" />';  

Models - main_model.php

 function insert_image($data)  
           $this->db->insert("tbl_images", $data);  
      function fetch_image()  
           $output = '';  
           $this->db->order_by("id", "DESC");  
           $query = $this->db->get();  
           foreach($query->result() as $row)  
                $output .= '  
                     <div class="col-md-3">  
                          <img src="'.base_url().'upload/'.$row->name.'" class="img-responsive img-thumbnail" />  
           return $output;  

Views - image_upload.php

 <!DOCTYPE html>  
      <title>Webslesson | <?php echo $title; ?></title>  
      <link rel="stylesheet" href="" />  
      <script src=""></script>  
      <div class="container">  
           <br /><br /><br />  
           <h3 align="center"><?php echo $title; ?></h3>  
           <form method="post" id="upload_form" align="center" enctype="multipart/form-data">  
                <input type="file" name="image_file" id="image_file" />  
                <br />  
                <br />  
                <input type="submit" name="upload" id="upload" value="Upload" class="btn btn-info" />  
           <br />  
           <br />  
           <div id="uploaded_image">  
           <?php echo $image_data; ?>  
      $('#upload_form').on('submit', function(e){  
           if($('#image_file').val() == '')  
                alert("Please Select the File");  
                     url:"<?php echo base_url(); ?>main/ajax_upload",   
                     //base_url() = http://localhost/tutorial/codeigniter  
                     data:new FormData(this),  
                     contentType: false,  
                     cache: false,  


 -- Table structure for table `tbl_images`  
 CREATE TABLE IF NOT EXISTS `tbl_images` (  
  `id` int(11) NOT NULL AUTO_INCREMENT,  
  `name` varchar(255) NOT NULL,  
  PRIMARY KEY (`id`)  
 -- Dumping data for table `tbl_images`  

1 comment: