简体   繁体   中英

codeigniter image_lib does not resize the image for multiple upload

This is my controller

public function do_upload()
        {
          $propertyID = $this->uri->segment(3);
          $this->load->helper('date');
          $config['upload_path'] = './uploads/';
          $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size']  = '10000';
          $config['file_name']  = date('m-y-d-h-m-s')."_".$propertyID;
          $name = $config['file_name'];

      $this->load->library('upload', $config);
      $this->load->model('user_model');
      $prop['propertyID'] = $propertyID;


      // looping $_FILES and create new one
      foreach($_FILES['userfile'] as $key=>$val)
      {
         $i = 1;
         foreach($val as $v)
         {
            $field_name = "file_".$i;
            $_FILES[$field_name][$key] = $v;
            $i++;
         }
      }
      // delete the initial array, because we already have a new array
      unset($_FILES['userfile']);

      //error variables changed, from a string into an array
      $error = array();
      $success = array();
      foreach($_FILES as $field_name => $file)
      {
         if ( ! $this->upload->do_upload($field_name))
         {
            $error[] = $this->upload->display_errors();
         }
         else
         {
            $success[] = $this->upload->data();


        $this->thumb_nail($name);


        $config12['image_library'] = 'gd2';
        $config12['source_image'] = './uploads/'.$config['file_name'].'.jpg';
        $config12['new_image'] = './uploads/medium/'.$config['file_name'];
        $config12['create_thumb'] = TRUE;
        $config12["thumb_marker"] = "";
        $config12['maintain_ratio'] = TRUE;
        $config12['width']     = 500;
        $config12['height']   = 400;

        $this->image_lib->clear();
        $this->image_lib->initialize($config12);
        $this->image_lib->resize();
            $data = $this->upload->data();
            $prop['imgName'] = $data['file_name'];

            $this->user_model->addImage($prop);
         }
      }
      if(count($error) > 0)
      {
         $data['error'] = implode('<br />',$error);
         $data['profile'] = $this->user_model->get_profile_image($propertyID);
         $data['photos'] = $this->user_model->get_property_images_by_ID($propertyID);
         $data['main_content'] = 'manage_property_images1';
         $this->load->view('1_col', $data);
      }
      else
      {
         $data['profile'] = $this->user_model->get_profile_image($propertyID);
         $data['photos'] = $this->user_model->get_property_images_by_ID($propertyID);
         $data['main_content'] = 'manage_property_images1';
         $this->load->view('1_col', $data);
      }
}

If I upload a single image, it resizes it and saves both the images in correct directory.

But if I upload more than one image, it resizes only the first one. Do I need to clear the something. Can someone please help

Try something like this to reset the image library for every image

//Create Thumbnail
$config['image_library']  = 'gd2';
$config['source_image']   = $data['full_path'];
$config['maintain_ratio'] = true;
$config['width']          = 1280;
$config['height']         = 400;

$this->load->library('image_lib');     //<----- SEE
$this->image_lib->initialize($config); //<----- SEE

if($this->image_lib->resize())
{
    $this->image_lib->clear();  //<----- SEE
    return true
}

It worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM