We always look to reduce the size of the image when we upload images to our web application. We will learn how to compress and resize uploaded images in Codeigniter 3.


Step 1: Download Codeigniter 3



Step 2: Create Routes

application/config/routes.php

<?php
$route['image-upload'] = 'ImageUpload';
$route['image-upload/post']['post'] = "ImageUpload/uploadImage";



Step 3: Create ImageUpload Controller

application/controllers/ImageUpload.php

<?php
   
class ImageUpload extends CI_Controller {
   
   /**
    * Manage __construct
    *
    * @return Response
   */
   public function __construct() { 
      parent::__construct(); 
      $this->load->helper(array('form', 'url')); 
   }
   
   /**
    * Manage index
    *
    * @return Response
   */
   public function index() { 
      $this->load->view('imageUploadForm', array('error' => '' )); 
   } 
   
   /**
    * Manage uploadImage
    *
    * @return Response
   */
   public function uploadImage() { 
   
      $config['upload_path']   = './uploads/'; 
      $config['allowed_types'] = 'gif|jpg|png'; 
      $config['max_size']      = 1024;
      $this->load->library('upload', $config);
   
      if ( ! $this->upload->do_upload('image')) {
         $error = array('error' => $this->upload->display_errors()); 
         $this->load->view('imageUploadForm', $error); 
      }else { 
  
        $uploadedImage = $this->upload->data();
        $this->resizeImage($uploadedImage['file_name']);
   
        print_r('Image Uploaded Successfully.');
        exit;
      } 
   }
  
   /**
    * Manage uploadImage
    *
    * @return Response
   */
   public function resizeImage($filename)
   {
      $source_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $filename;
      $target_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/';
      $config_manip = array(
          'image_library' => 'gd2',
          'source_image' => $source_path,
          'new_image' => $target_path,
          'maintain_ratio' => TRUE,
          'width' => 500,
      );
   
      $this->load->library('image_lib', $config_manip);
      if (!$this->image_lib->resize()) {
          echo $this->image_lib->display_errors();
      }
   
      $this->image_lib->clear();
   }
} 
?>



Step 4: Create View File

application/views/imageUploadForm.php

<!DOCTYPE html>
<html> 
<head> 
  <title>Codeigniter Compress Image Size Example</title> 
</head>
  
<body> 
   
  <?php echo $error;?> 
     
  <form method='post' action='/image-upload/post' enctype='multipart/form-data'>
     <input type="file" name="image" size="20" />
     <input type="submit" value="upload" /> 
  </form> 
   
</body>
</html>


Thanks, I hope this will help you........