codeigniter加载错误的图像大小

时间:2017-08-30 08:27:16

标签: php codeigniter-3 image-resizing

我在CodeIgniter中创建了一个函数,所以当我添加产品图片时,它会上传常规尺寸(product_foto)和缩略图尺寸(product_foto_thumb)。在“所有产品”页面上,图片都是我喜欢的缩略图大小,但是当我点击产品进入详细页面时,图像仍然很小,而且它仍然与缩略图相同,而我是在该视图页面上加载常规图片。

这是我在控制器文件中的上传功能:

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'] = 168;
        $configThumb['height'] = 150;

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

        $this->image_lib->resize();


        $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://www.example.com/Product/');
}

我的视图文件,我正在加载大尺寸图片:

<div class="container">
<div class="row">
<div class="col-md-4">
<img src="<?php echo base_ur().'upload/'$product['product_foto'] ?>">

</div>
</div>

</div>

我的缩略图视图文件:

<?php foreach($products as $product) : ?>
<div class="col-lg-2">
<div id="product">
<a href="<?php echo base_url() ?>/Products/details/<?php echo $product['product_id']; ?>">
<?php echo '<img src="upload/'.$product['product_naam']; ?> </div>
</div>

<?php endforeach; ?>

1 个答案:

答案 0 :(得分:0)

请参阅下面的代码。我认为这肯定会解决你的问题。

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'] = 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/'.'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];
            $configThumb['create_thumb'] = TRUE;
            $configThumb['maintain_ratio'] = TRUE;
            $configThumb['width'] = 168;
            $configThumb['height'] = 150;

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

            $this->image_lib->initialize($configThumb);
            $this->image_lib->resize();

            $this-> db-> insert('products', array(
                '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'),
                '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/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/');
        }
    }
}

您的拇指代码示例

<?php foreach($products as $product) : ?>
<div class="col-lg-2">
    <div id="product">
        <img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>">
    </div>
</div>
<?php endforeach; ?>

您的主要图片示例

<?php foreach($products as $product) : ?>
<div class="col-lg-2">
    <div id="product">
        <img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>">
    </div>
</div>
<?php endforeach; ?>