【问题标题】:Product thumbnail in CodeIgniterCodeIgniter 中的产品缩略图
【发布时间】:2017-07-13 11:57:28
【问题描述】:

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

【问题讨论】:

    标签: php codeigniter codeigniter-3


    【解决方案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/');
    }
    

    步骤

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

    【讨论】:

    • 希望对您有所帮助。
    • 您好,感谢您的帮助,它可以工作,但是当我单击产品详细信息页面时,该项目仍然很小,而我正在加载常规 product_foto 列.. 你知道我可以做什么解决这个问题?
    • 就像当我打开缩略图到详情页面时,它仍然显示缩略图而不是普通图片。
    • 您使用的是模板吗?这可能在样式表中设置。不要忘记将细节中的图像显示更改为product_foto。那是带有大/默认图像的字段/列
    • 是的,我正在详细页面中加载 product_foto,但我没有使用任何模板。不知道为什么它加载缩略图
    猜你喜欢
    • 2011-10-16
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 2018-09-30
    • 2014-12-05
    • 1970-01-01
    • 2019-01-07
    • 2022-01-15
    相关资源
    最近更新 更多