2013-10-28 35 views
0

我有一張表單,可以上傳3張圖片。 在codeigniter中,我做了一個foreach循環來處理圖像,並將它們保存到我的數據庫中。這很好。在codeigniter中上傳多個文件並調整大小

但我需要調整圖像大小。當我上傳一個圖像時,調整大小的作品,但有更多的圖像,第一個圖像看起來像狗屎,第二個和第三個圖像不會調整大小......你們可以幫我嗎?

$this->load->library('upload'); 
$config['upload_path'] = './uploads/'; 
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; 
$config['encrypt_name'] = TRUE; 
//$config['overwrite'] = TRUE; 

$this->upload->initialize($config); 
$count = 0; 
      foreach($_FILES as $field => $file) 
      { 
       $count = $count + 1; 
       // No problems with the file 
       if($file['error'] == 0) 
       { 
        // So lets upload 
        if ($this->upload->do_upload($field)) 
        { 
         $data = $this->upload->data(); 

         $data2 = array(
       'image' . $count => $data['file_name'] 
      ); 

$this->db->where('id', $query_id); 
$this->db->update('ads', $data2); 
//afbeeldingen verkleinen. 

        $config['image_library'] = 'gd2'; 
        $config['source_image'] = $data['full_path']; 
        //$config['new_image'] = './image_uploads/'; 
        $config['maintain_ratio'] = TRUE; 
        $config['width'] = 370; 
        $config['height'] = 277; 
        $this->load->library('image_lib',$config); 

        if (!$this->image_lib->resize()){ 
       $this->session->set_flashdata('errors', $this->image_lib->display_errors('', '')); 
       echo $this->image_lib->display_errors('', ''); 
       exit(); 
       } 

        } 
        else 
        { 
         $errors = $this->upload->display_errors(); 
         echo $errors; 
         exit(); 
        } 
       } 
      } 
$data['name'] = $name; 
redirect('/adDetail/getDetails/' . $query_id, 'location', 200); 
} 
+0

嘗試重置'$ config'變量 –

回答

1

嘗試在循環結束時卸載image_lib。

+0

我會嘗試添加$ this-> image_lib-> clear(); – Daan

+0

現在,當我上傳2個文件時,第一個文件的大小被正確調整。第二個文件出現錯誤:您的服務器不支持處理此類圖像所需的GD功能。但是我試圖上傳的2張圖像是一樣的... – Daan

+0

您每次在循環中重新加載庫。正如我所說,嘗試在最後卸載它,所以它不會多次加載。 – wclark