2012-04-05 52 views
6

我正在嘗試上傳圖片並調整它們的大小,到目前爲止上傳工作正常,並且還創建了縮略圖圖片。但我無法獲得原始圖像的大小。所發生的一切就是文件以原始大小直接上傳到文件夾。我想我試圖把這個圖像調整大小,然後覆蓋最初上傳的文件。CodeIgniter上傳圖片,然後調整它的大小

這裏是我的代碼,我在想什麼:

//Initialise the upload file class 
    $config['upload_path'] = './image_uploads/'; 
    $config['allowed_types'] = 'jpg|jpeg|gif|png'; 
    $config['max_size'] = '6048'; 
    $this->load->library('upload', $config); 

    $userfile = "_upload_image"; 
      //check if a file is being uploaded 
      if(strlen($_FILES["_upload_image"]["name"])>0){ 

       if (!$this->upload->do_upload($userfile))//Check if upload is unsuccessful 
       { 
        $upload_errors = $this->upload->display_errors('', ''); 
        $this->session->set_flashdata('errors', 'Error: '.$upload_errors); 
        redirect('admin/view_food/'.$food->course_id); 
       } 
       else 
       { 
        //$image_data = $this->upload->data(); 

        ////[ THUMB IMAGE ] 
        $config2['image_library'] = 'gd2'; 
        $config2['source_image'] = $this->upload->upload_path.$this->upload->file_name; 
        $config2['new_image'] = './image_uploads/thumbs'; 
        $config2['maintain_ratio'] = TRUE; 
        $config2['create_thumb'] = TRUE; 
        $config2['thumb_marker'] = '_thumb'; 
        $config2['width'] = 75; 
        $config2['height'] = 50; 
        $this->load->library('image_lib',$config2); 

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

        //[ MAIN IMAGE ] 
        $config['image_library'] = 'gd2'; 
        $config['source_image'] = $this->upload->upload_path.$this->upload->file_name; 
        //$config['new_image'] = './image_uploads/'; 
        $config['maintain_ratio'] = TRUE; 
        $config['width'] = 250; 
        $config['height'] = 250; 
        $this->load->library('image_lib',$config); 

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

回答

4

你有沒有試過把它添加到配置選項

$config['overwrite'] = TRUE; 

這應該做的伎倆。

根據文檔

如果設置爲true,如果具有相同的名稱作爲一個你上傳存在的文件,它會被覆蓋。如果設置爲false,如果存在具有>相同名稱的另一個名稱,則會將一個數字附加到文件名中。

3

我一直在嘗試創建過去20小時的縮略圖,我沒有成功,直到我意識到我弄亂了代碼,我並沒有初始化我的配置數組!

只需添加這行代碼加載你image_lib

$this->image_lib->initialize($config); 

希望這對你的作品...........豎起大拇指了!

0

如果你喜歡用CI與Image一起玩,我強烈推薦image_moo庫。