title: Codeigniter image resize before upload
body: <p>I am trying to upload multiple images in Codeigniter and reduce the size of each image.
  here is my view </p>
  
  <pre><code> &lt;?php echo form_open_multipart('main_controller/do_insert');?&gt;   
      &lt;div id="mainDiv"&gt;
       &lt;div class='group'&gt;
        &lt;div&gt;&lt;input type="text" name="name[]"/&gt;&lt;/div&gt;
        &lt;div&gt;&lt;input type="file" name="img[]"/&gt;&lt;/div&gt;
       &lt;/div&gt;
      &lt;/div&gt;
      &lt;input type="button" id="add an entry"&gt;
      &lt;input type="submit" value="save all"/&gt;
   &lt;?php from_close();?&gt;
  </code></pre>
  
  <p>and my javascript  is look like </p>
  
  <pre><code>&lt;script&gt;
  function add(x)
  {
   var str1="&lt;div&gt;&lt;input type='text' name='name"+x+"'/&gt;&lt;/div&gt;"
   var str2="&lt;div&gt;&lt;input type='file' name='img"+x+"'/&gt;&lt;/div&gt;"
   var str3="&lt;input type='button' value='add an entry' onClick='add(x+1)'&gt;";
   $("#mainDiv").append(str1+str2+str3);
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>here is my controller</p>
  
  <pre><code> function do_insert{
      while($i&lt;=$counter) /*conter have value of total number for images just ignore*/
      {
        $config['upload_path'] = './images/';
        $config['allowed_types'] = 'gif|jpg|png';
  
        $this-&gt;load-&gt;library('upload', $config);
  
        if ( ! $this-&gt;upload-&gt;do_upload($userfileName))
        {
           echo "error".count;    
        }
        else
        {
           $data = array('upload_data' =&gt; $this-&gt;upload-&gt;data());
           $img=$data['upload_data']['file_name']; /*for geting uploaded image name*/
  
           $config['image_library'] = 'gd2';
           $config['source_image'] = './images/'.$img;
           $config['new_image'] = './images/';
           $config['maintain_ratio'] = TRUE;
           $config['width']    = 640;
           $config['height']   = 480;
  
           $this-&gt;load-&gt;library('image_lib', $config); 
  
           if (!$this-&gt;image_lib-&gt;resize()) {
              echo $this-&gt;image_lib-&gt;display_errors();
           }
           else
           {
              echo "success"; /*and some code here*/
           }
         }
      }
   }
  </code></pre>
  
  <p>my problem is that only the first image is getting re-sized remains kept as its original size. 
    And the image is resized after once uploaded. I think this is not a proper way now 
    Is there any alternative way to resize the image? it may be better if resize before doing the upload.</p>