2017-07-11 60 views
0

我在CodeIgniter框架上製作了一個功能,用戶可以將產品圖片上傳到產品頁面。事情是我想要圖片被調整到我想要的寬度和高度。如何在CodeIgniter中上傳圖片時調整圖片大小功能

這是在我的控制我的上傳功能:

public function upload(){ 

$this->load->library('upload'); 

if(!$this->upload->do_upload('userfile')){ 
    $error = array('error'=>$this->upload->display_errors()); 
    $this->load->view('product_form', $error); 
}else{ 

    $config['image_library'] = 'gd2'; 
    $config['source_image'] = './upload/'.$file_data["file_name"]; 
    $config['create_thumb'] = TRUE; 
    $config['maintain_ratio'] = TRUE; 
    $config['width']   = 100; 
    $config['height']  = 100; 

    //load library 
    $this->load->library('image_lib', $config); 

    //resize image 
    $this->image_lib->resize(); 


    $this->db->insert('products', array(
     'product_foto' => $file_data['file_name'], 
     'product_naam' => $this->input->post('product_naam'), 
     'product_beschrijving' => $this->input->post('product_beschrijving'), 
     'product_categorie' => $this->input->post('product_categorie'), 
     'ophaal_plaats' => $this->input->post('ophaal_plaats'), 
     'date_created' => date('Y-m-d'), 
     'date_updated' => date('Y-m-d') 
     )); 
    $data['img'] = base_url().'/upload/'.$file_data['file_name']; 
    header('location:https://kadokado-ferran10.c9users.io/Product/'); 
} 

我的看法形式:

<?php echo form_open_multipart('Product/upload'); ?> 


     <table class="aanbieding-cadeau"> 

     <tr> 
      <td><?php echo form_input(array('id'=>'product_naam', 'name'=>'product_naam', 'placeholder' => '1. Naam van het cadeau', 'size'=>25));?></td> 
     </tr> 

     <tr> 
      <td><?php echo($selectField);?></td> 
     </tr> 

     <tr> 
      <td><?php echo form_input(array('id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'placeholder' => '3.Kies een stad', 'size'=>25));?></td> 
     </tr> 

     <div class="checkbox"> 
     <label><input type="checkbox" value="">Gebruik adres van mijn account</label> 
     </div> 

     <tr> 
     <td> 
      <h4>Upload foto</h4> 
     <input type="file" name="userfile" /> 
      </td> 
     </tr> 

    <tr> 
    <td><?php echo form_textarea(array('type'=>'textarea','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'placeholder' => '5. Vertel iets over dit cadeau..', 'size'=>25));?></td> 
    </tr> 


<tr> 
<td><input type="submit" class="btn btn-primary" name="submit" value="Cadeau aanbieden!" /></td> 
</tr> 
    </table> 
    </form> 

我想每一個畫面,用戶上傳爲400寬度400高度,但我該怎麼辦那?

+0

的可能重複:HTTPS:/ /stackoverflow.com/questions/19218247/codeigniter-image-resize –

+0

你可以n參考文檔https://www.codeigniter.com/userguide3/libraries/image_lib.html –

回答

1

使用此:

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{ 

    $data = $this->upload->data(); 
    $config['image_library'] = 'gd2'; 
    $config['source_image'] = './upload/'.$data["raw_name"].$data['file_ext']; 
    $config['new_image'] = './upload/'.$data["raw_name"].$data['file_ext']; 
    $config['create_thumb'] = FALSE; 
    $config['maintain_ratio'] = FALSE; 
    $config['width']   = 400; 
    $config['height']  = 400; 

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

    $this->image_lib->resize(); 


    $this->db->insert('products', array(
     'product_foto' => $data["raw_name"].$data['file_ext'], 
     'product_naam' => $this->input->post('product_naam'), 
     'product_beschrijving' => $this->input->post('product_beschrijving'), 
     'product_categorie' => $this->input->post('product_categorie'), 
     'ophaal_plaats' => $this->input->post('ophaal_plaats'), 
     'date_created' => date('Y-m-d'), 
     'date_updated' => date('Y-m-d') 
     )); 
    $data['img'] = base_url().'/upload/'.$data["raw_name"].$data['file_ext']; 
    header('location:https://kadokado-ferran10.c9users.io/Product/'); 
} 
+0

嘿我試過你的代碼,當我提交什麼都沒有發生..沒有產品被添加到我的數據庫:( – lablanco

+0

以及這正是你正在使用的?那麼這是錯誤的。這段代碼只回答你的問題「我怎麼能如果你的圖片沒有被上傳,那顯然這是行不通的,上傳代碼必須在這行之前:'if(!$ this-> upload-> do_upload ('userfile'))' –

+0

我不明白嗎?你是什麼意思 – lablanco

0

調用庫:

$this->load->library('image_lib');

處理的圖像(加入後上傳這個圖片的代碼)

$config['image_library'] = 'gd2'; 
$config['source_image'] = './upload/'.$file_data["file_name"]; 
$config['create_thumb'] = TRUE; 
$config['maintain_ratio'] = TRUE; 
$config['width']   = 75; 
$config['height']  = 50; 
$this->load->library('image_lib', $config); 

$this->image_lib->resize(); 
+0

是的,你可以在其他條件下調用這個庫 –

+0

好的謝謝,gd2代表什麼:$ config ['image_library'] = 'GD2'; ? – lablanco

+0

gd2是一個圖像庫。如果這對你有幫助,請投票。 –