Codeigniter 图像在上传前调整大小

Nis*_* Up 2 html php codeigniter image

我正在尝试在 Codeigniter 中上传多个图像并减小每个图像的大小。这是我的观点

 <?php echo form_open_multipart('main_controller/do_insert');?>   
    <div id="mainDiv">
     <div class='group'>
      <div><input type="text" name="name[]"/></div>
      <div><input type="file" name="img[]"/></div>
     </div>
    </div>
    <input type="button" id="add an entry">
    <input type="submit" value="save all"/>
 <?php from_close();?>
Run Code Online (Sandbox Code Playgroud)

我的javascript看起来像

<script>
function add(x)
{
 var str1="<div><input type='text' name='name"+x+"'/></div>"
 var str2="<div><input type='file' name='img"+x+"'/></div>"
 var str3="<input type='button' value='add an entry' onClick='add(x+1)'>";
 $("#mainDiv").append(str1+str2+str3);
}
</script>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器

 function do_insert{
    while($i<=$counter) /*conter have value of total number for images just ignore*/
    {
      $config['upload_path'] = './images/';
      $config['allowed_types'] = 'gif|jpg|png';

      $this->load->library('upload', $config);

      if ( ! $this->upload->do_upload($userfileName))
      {
         echo "error".count;    
      }
      else
      {
         $data = array('upload_data' => $this->upload->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->load->library('image_lib', $config); 

         if (!$this->image_lib->resize()) {
            echo $this->image_lib->display_errors();
         }
         else
         {
            echo "success"; /*and some code here*/
         }
       }
    }
 }
Run Code Online (Sandbox Code Playgroud)

我的问题是只有第一个图像被重新调整大小仍然保持其原始大小。并且图像在上传后调整大小。我认为现在这不是正确的方法有没有其他方法可以调整图像大小?如果在上传之前调整大小可能会更好。

Nis*_* Up 6

我已经解决了调整第一张图片大小的问题,将我的控制器更改为 `

$this->load->library('image_lib');
while($i<=$counter) /*conter have value of total number for images just ignore*/
{
      $config['upload_path'] = './images/';
      $config['allowed_types'] = 'gif|jpg|png';
      $this->load->library('upload', $config);
      if ( ! $this->upload->do_upload($userfileName))
      {
         echo "error".count;    
      }
      else
      {
            $image_data =   $this->upload->data();

            $configer =  array(
              'image_library'   => 'gd2',
              'source_image'    =>  $image_data['full_path'],
              'maintain_ratio'  =>  TRUE,
              'width'           =>  250,
              'height'          =>  250,
            );
            $this->image_lib->clear();
            $this->image_lib->initialize($configer);
            $this->image_lib->resize();
      }
}
Run Code Online (Sandbox Code Playgroud)