Codeigniter图片调整大小不完全正常工作

时间:2017-10-04 11:21:24

标签: php codeigniter codeigniter-3

我在CodeIgniter上创建了一个网站,以便用户可以将产品添加到我的网站。我做了一个调整大小的功能,所以图片也会创建一个缩略图。 现在,如果图片大于宽度,则仅调整此缩略图的大小:168 px和height:112px。如果图片小于该图片,则图片不会调整大小。我希望我上传的所有图片都有一个宽度为168像素,高度为112像素的缩略图。

缩略图代码:

 $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/');

        }
    }

1 个答案:

答案 0 :(得分:0)

$config['maintain_ratio'] = TRUE;

以上代码指定调整大小时是否保持原始宽高比。如果设置为true,则会改变图像分辨率。

$config['maintain_ratio'] = FALSE; //DO this

这将保持您的分辨率固定。 我测试过它并且有效。 以下是结果: 原始图像均为512 X 512(比您的尺寸大)和100 X 100(比您的尺寸小) enter image description here