codeigniter image resize example

Today, We want to share with you codeigniter image resize example.In this post we will show you how to resize image size(quality) with codeigniter image_lib, hear for codeigniter tutorial – compress and resize uploaded image we will give you demo and example for implement.In this post, we will learn about PHP Automatic Flexify Image Resizing with an example.

Codeigniter 3 resize image and create thumbnail example

Step 1: Download Fresh Codeigniter 3

download fresh version of Codeigniter 3
download from here Free Download Codeigniter 3

Step 2: Create Routes

application/config/routes.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');


$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;


$route['image-upload'] = 'ProfilePicUpload';
$route['image-upload/post']['post'] = "ProfilePicUpload/storeProfileDP";

Step 3: Create ProfilePicUpload Controller

application/controllers/ProfilePicUpload.php

<?php


class ProfilePicUpload 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('profileDpFormLoadData', array('error' => '' )); 
   } 


   /**
    * Manage storeProfileDP
    *
    * @return Response
   */
   public function storeProfileDP() { 


      $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('profileDpFormLoadData', $error); 
      }else { 


        $uploadedImage = $this->upload->data();
        $this->resizeImage($uploadedImage['file_name']);


        print_r('Image Uploaded Successfully.');
        exit;
      } 
   }


   /**
    * Manage storeProfileDP
    *
    * @return Response
   */
   public function resizeImage($filename)
   {
      $source_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $filename;
      $target_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/thumbnail/';
      $config_manip = array(
          'image_library' => 'gd2',
          'source_image' => $source_path,
          'new_image' => $target_path,
          'maintain_ratio' => TRUE,
          'create_thumb' => TRUE,
          'thumb_marker' => '_thumb',
          'width' => 150,
          'height' => 150
      );


      $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/profileDpFormLoadData.php

<!DOCTYPE html>
<html> 
<head> 
  <title>Codeignier 3 Image Upload with Resize Example from Scratch - www.pakainfo.com</title> 
</head>


<body> 


  <?php echo $error;?> 
  <?php echo form_open_multipart('image-upload/post');?> 
     <input type="file" name="image" size="20" />
     <input type="submit" value="upload" /> 
  </form> 


</body>
</html>

Now i requred to make 2 main folder. First you have to make “uploads” and inside uploads folder you have to make second new folder “thumbnail”.

I hope you get an idea about Codeignier 3 Image Upload with Resize.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.