title: how to reduce the file size in kb of an image in codeigniter when we upload?
body: <p>I have a page where user will drag and drop multiple images. I need to resize and store it as thumbnail and also i have to reduce the big images in kb without losing the image width and height. What would be the best way do it in codeigniter?</p>
  
  <pre><code>     if ($_FILES["file"]["name"])
          {
  
          $targetPath = APPPATH . 'uploads/portfolios/';
  
          $result = $this-&gt;do_upload("file", $targetPath);
          $data =  array();
          if (!$result['status']) {
                  $data['error_msg'] ="Can not upload Image for " . $result['error'] . " ";
  
          }
  
                  else {
  
      $this-&gt;resize_image($targetPath . $result['upload_data']['file_name'],$targetPath,'120','120');
  
  
      $file_name = $result['upload_data']['raw_name'].'_thumb'.$result['upload_data']['file_ext'];    
  
  
          }
  
  function resize_image($sourcePath, $desPath, $width = '500', $height = '500')
  {
  
      $this-&gt;load-&gt;library('image_lib');
      $this-&gt;image_lib-&gt;clear();
      $config['image_library'] = 'gd2';
      $config['source_image'] = $sourcePath;
      $config['new_image'] = $desPath;
      //$config['quality'] = '100%';
      $config['create_thumb'] = TRUE;
      $config['maintain_ratio'] = false;
      $config['thumb_marker'] = '_thumb';
      $config['width'] = 120;
      $config['height'] = 120;
   $this-&gt;image_lib-&gt;initialize($config);
  
      if ($this-&gt;image_lib-&gt;resize())
  
          return true;
      return false;
  }
  
    function do_upload($htmlFieldName, $path)
  {
      $config['file_name'] = time();
      $config['upload_path'] = $path;
      $config['allowed_types'] = 'gif|jpg|jpeg|png';
      $config['max_size'] = '20000';
      //$config['max_width'] = '2000';
      //$config['max_height'] = '2000';
      $this-&gt;load-&gt;library('upload', $config);
      $this-&gt;upload-&gt;initialize($config);
      unset($config);
      if (!$this-&gt;upload-&gt;do_upload($htmlFieldName))
      {
          return array('error' =&gt; $this-&gt;upload-&gt;display_errors(), 'status' =&gt; 0);
      } else
      {
          return array('status' =&gt; 1, 'upload_data' =&gt; $this-&gt;upload-&gt;data());
      }
  }
  </code></pre>