我正在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/');
}
}
(比你的尺寸大),你是什麼意思? – JohnDoe122
問題是我上傳的較小的圖片沒有被調整到168寬度和112高度 – JohnDoe122
你有調試設置,所以你可以看看它是通過'system/libraries/Image_lib.php'找到它的方式,看看最後的結果$ width和$ height設置爲? (或者在感興趣的點上插入一堆'log_message'命令。) – ourmandave