2017-10-04 74 views
1

我正在CodeIgniter上創建一個網站,以便用戶可以將產品添加到我的網站。我做了一個調整大小的功能,所以圖片也會創建一個縮略圖。 現在,如果圖片大於寬度:168px和height:112px,則此縮略圖僅被調整大小。如果圖片比這更小,圖片不會調整大小。我想要上傳的所有圖片都有一個寬度爲168px,高度爲112px的縮略圖。Codeigniter圖片調整大小不能正常工作

縮略圖圖像代碼:

$dataThumb = $this->upload->data(); 
     $configThumb['image_library'] = 'gd2'; 
     $configThumb['source_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext']; 
     $configThumb['new_image'] = './upload/'.'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext']; 
     $configThumb['create_thumb'] = TRUE; 
     $configThumb['maintain_ratio'] = TRUE; 
     $configThumb['width'] = 168; 
     $configThumb['height'] = 112; 

我完全上傳產品代碼:

public function upload() { 
     $this->load->library('upload'); 
     $this->load->library('image_lib'); 

     $config['upload_path'] = './upload/'; 
     $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; 
     $config['max_size'] = '0'; 
     $config['max_width'] = '0'; 
     $config['max_height'] = '0'; 
     $config['encrypt_name']= true; 
     $this->upload->initialize($config); 


     if(!$this->upload->do_upload('userfile')){ 
      $error = array('error'=>$this->upload->display_errors()); 
      $this->load->view('product_form', $error); 
     }else{ 
      //Main image 
      $data = $this->upload->data(); 
      $config['image_library'] = 'gd2'; 
      $config['source_image'] = './upload/'.$data["raw_name"].$data['file_ext']; 
      $config['new_image'] = './upload/'.'new_'.$data["raw_name"].$data['file_ext']; 
      $config['create_thumb'] = FALSE; 
      $config['maintain_ratio'] = FALSE; 
      $config['width'] = 547; 
      $config['height'] = 430; 

      //Thumb image 
      $dataThumb = $this->upload->data(); 
      $configThumb['image_library'] = 'gd2'; 
      $configThumb['source_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext']; 
      $configThumb['new_image'] = './upload/'.'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext']; 
      $configThumb['create_thumb'] = TRUE; 
      $configThumb['maintain_ratio'] = TRUE; 
      $configThumb['width'] = 168; 
      $configThumb['height'] = 112; 

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

      $this->image_lib->initialize($configThumb); 
      $this->image_lib->resize(); 

      $this-> db-> insert('products', array(
       'user_id' => $_SESSION['user_id'], 
       'product_foto' => 'new_'.$data["raw_name"].$data['file_ext'], 
       'product_foto_thumb' => 'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'], 
       'product_naam' => $this->input->post('product_naam'), 
       'product_beschrijving' => $this->input->post('product_beschrijving'), 
       'ophaal_plaats' => $this->input->post('ophaal_plaats'), 
       'category_id' => !empty($this->input->post('category_id')) ? $this->input->post('category_id') : 0, 
       'date_created' => date('Y-m-d'), 
       'date_updated' => date('Y-m-d') 
       )); 
      $data['img'] = base_url(). 
      '/upload/new_'.$data["raw_name"].$data['file_ext']; 
      $dataThumb['img'] = base_url(). 
      '/upload/thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext']; 

      header('location:https://kadokado-ferran10.c9users.io/Product/'); 

     } 
    } 
+0

(比你的尺寸大),你是什麼意思? – JohnDoe122

+0

問題是我上傳的較小的圖片沒有被調整到168寬度和112高度 – JohnDoe122

+0

你有調試設置,所以你可以看看它是通過'system/libraries/Image_lib.php'找到它的方式,看看最後的結果$ width和$ height設置爲? (或者在感興趣的點上插入一堆'log_message'命令。) – ourmandave

回答

0
$config['maintain_ratio'] = TRUE; 

上面的代碼指定是否調整大小時保持原有的縱橫比。如果設置爲true,則會改變圖像分辨率。

$config['maintain_ratio'] = FALSE; //DO this 

這會保持您的分辨率不變。 我測試了這個,它工作。 下面是結果: 原始圖像都爲512×512和100×100(比你的尺寸小) enter image description here

+0

我在我的縮略圖上做的,只是看我的代碼 – JohnDoe122

+0

所以你可以詳細說明你的問題更多 –

+0

正如你可以看到我的縮略圖圖片代碼我設置此圖片大小:$ configThumb ['width'] = 168; $ configThumb ['height'] = 112。好的,所以我希望所有圖片都能調整到圖片大小,即使圖片小於這個尺寸。所以現在只有當一張圖片比這個尺寸大時,它纔會被調整大小,但是如果它小於那個尺寸,它就不會。你明白我的意思嗎? – JohnDoe122