CodeIgniter中的产品缩略图

时间:2017-07-13 11:57:29

标签: php codeigniter codeigniter-3

如何制作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宽度,但我只希望缩略图上的大小而不是正常图片上的大小。

1 个答案:

答案 0 :(得分: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{
       //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/');
}

<强> STEPS

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