2013-04-21 51 views
0

這是我的模型:笨 - 上傳圖像,生成縮略圖,保存兩個目錄數據庫

我收到以下錯誤

Fatal error: Call to undefined method CI_Image_lib::data() in C:\xampp\htdocs\adcc\application\models\media_model.php on line 58 

我的問題:我爲什麼不能使用數據()從保存的縮略圖中獲取['full_path'](就像我爲上傳所做的那樣)?

有沒有更好的方法來做到這一點?謝謝!

public function set_media() { 

     $config1 = array(
      'allowed_types' => 'jpg|jpeg|gif|png', 
      'upload_path' => $this->gallery_path . '/images', 
      'max_size' => 2048 
     ); 

     $this->load->library('upload'); 
     $this->upload->initialize($config1); 
     $this->upload->do_upload(); 

     $image_data = $this->upload->data(); 

     $config2 = array(
      'source_image' => $image_data['full_path'], 
      'new_image' => $this->gallery_path . '/thumbs', 
      'maintain_ratio' => true, 
      'width' => 150, 
      'height' => 100 
     ); 

     $this->load->library('image_lib', $config2); 
     $this->image_lib->resize(); 
     $image_data2 = $this->image_lib->data(); 

     $this->load->helper('url'); 

     $id = url_title($this->input->post('title'), 'dash', TRUE); 

     $data = array(
      'id' => $id, 
      'name' => $this->input->post('name'), 
      'link' => $this->input->post('link'), 
      'year' => $this->input->post('year'), 
      'actors' => $this->input->post('actors'), 
      'image' => $image_data['full_path'], 
      'thumb' => $image_data2['full_path'] 
     ); 

     return $this->mongo_db->insert('media', $data); 
    } 
+0

爲什麼要生成縮略圖。上傳的項目已經是圖像了嗎?你想要更小的圖片?選中此項 - > http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ – 2013-04-21 11:42:40

回答

1

你不能得到從圖像數據LIB()[「full_path」],因爲$this->image_lib不上傳庫的一個實例,根本沒有該方法(如錯誤消息狀態)。

現在使用你的配置,image_lib將在你的原始圖像的相同文件名下創建新的調整大小的圖像,並將其保存在您在配置文件new_image中指定的目錄下。生成的完整路徑將被保存到image_lib的$full_dst_path屬性中,並且僅有文件夾和文件名也有$dest_folder$dest_image

所以使用這些,只是下降的線:

$image_data2 = $this->image_lib->data(); // delete this line 

和保存時只寫:

$data = array(
// ... 
'thumb' => $this->image_lib->full_dst_path, 
// ... 
), 

處理錯誤的上傳或Image_lib庫可以返回將是一個不錯的主意太。

0

你也需要調整大小之前,這個工作對我來說,初始化爲image_lib配置陣列的一部分https://stackoverflow.com/a/21187402/2897770

加載image_lib $這個 - > image_lib->初始化($ CONFIG2)後,只需添加這;