2017-10-04 7 views
1

私はCodeIgniterでWebサイトを作っているので、ユーザーは自分のWebサイトに製品を追加できます。私は画像がサムネイルを作成するようにサイズ変更機能を作った。 このサムネイルは、画像が幅:168ピクセルと高さ:112ピクセルより大きい場合にのみサイズが変更されます。画像がそれより小さい場合は、画像のサイズが変更されません。アップロードしたすべての画像に、幅168ピクセル、高さ112ピクセルのサムネイル画像が必要です。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 

これにより、解決が固定されます。 これをテストしたところ、うまくいきました。以下 は結果である: オリジナル画像は、あなたが何を意味するX 512の両方512(あなたの寸法より小さい)(あなたの寸法よりも大きい)と100 X 100 enter image description here

+0

私のサムネイル画像で、ちょうど私のコードを見てください – JohnDoe122

+0

あなたの問題は何ですか?詳細を詳しく教えてください –

+0

私のサムネイル画像のコードでわかるように、この画像のサイズは$ configThumb ['width'] = 168; $ configThumb ['height'] = 112です。さて、画像がそのサイズよりも小さくても、すべての画像をその画像サイズにリサイズします。だから、画像がそのサイズより大きければサイズが変更されますが、そのサイズよりも小さい場合は変更されません。私が何を意味するのか理解していますか? – JohnDoe122

関連する問題