CodeIgniter 中的产品缩略图



如何制作 CodeIgniter 缩略图?我正在使用 CodeIgniter 中的图像大小调整功能,但我不希望调整图像大小。我希望图片是所有商品页面上的小缩略图,我希望图片在我进入商品详情页面时变大,但我不完全知道如何做到这一点。

因此,当我转到产品详细信息页面时,图像与缩略图一样小,但我希望它更大。

一些信息: 存储图片的"我的表"列:product_foto

存储图片的我的图片文件夹:上传

This is my upload function in my controller file:
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']         = 170;
$config['height']       = 130;
$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/');
}

如您所见,我正在将图片的大小更改为 170 x 130 宽度,但我只希望在缩略图上而不是普通图片上使用该大小。

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/'.$data["raw_name"].$data['file_ext'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 640;
$config['height'] = 380;
//Thumb image
$dataThumb = $this - > upload - > data();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext'];
$configThumb['new_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext'];
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 170;
$configThumb['height'] = 130;
$this - > image_lib - > initialize($config);
$this - > image_lib - > initialize($configThumb);
$this - > image_lib - > resize();
//INSERT ** added the new field/column 'product_foto_thumb'
$this - > db - > insert('products', array(
'product_foto' => $data["raw_name"].$data['file_ext'],
'product_foto_thumb' => $dataThumb["raw_name"].$dataThumb['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'];
$dataThumb['img'] = base_url().
'/upload/'.$dataThumb["raw_name"].$dataThumb['file_ext'];
header('location:https://kadokado-ferran10.c9users.io/Product/');
}

步骤

  1. 您需要创建向数据库表添加缩略图列
  2. 创建变量以保存缩略图
  3. 将名称添加到插入查询中

最新更新