简体   繁体   English

codeigniter image_lib不会为多次上传调整图像大小

[英]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. 它为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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