codeigniter $ this-> image_lib-> resize()函数没有调整图像大小

时间:2017-12-08 18:57:33

标签: php image codeigniter resize

我想调整图像大小,但它无法正常工作,$ this-> image_lib-> resize()此函数无法调整图像大小。它产生原始图像大小。我上传600 * 600大小的图像。但我想调整300 * 300的大小 请检查下面的代码。

if( $this->input->post('save')){

        $config['upload_path'] = 'images';
        $config['allowed_types'] = 'jpg|png';
        $config['max_size'] = 1000;
        $config['overwrite'] = false;
        $config['encrypt_name'] = true;
        $this->load->library('upload', $config);

        $this->upload->initialize($config);

        if(!$this->upload->do_upload('user_image')){
            $this->session->set_flashdata('message', $this->upload->display_errors());
            $this->load->view('create',$data);
            return;
        }else{
            $upicture = $this->upload->data();
            $image_data = $upicture;
            $config2['image_library'] = 'gd2';
            $config2['source_image'] = $image_data['full_path']; //get original image
            $config2 ['new_image'] = 'images'; //save as new image //need to create thumbs first
            $config2 ['maintain_ratio'] = false;
            $config2 ['create_thumb'] = false;
            $config2 ['overwrite'] = false;
            $config2['width'] = 300;
            $config2['height'] = 300;
            $this->load->library('image_lib', $config2);
            $this->image_lib->resize();

            $img_name = $upicture['file_name'];
        }


        $this->form_validation->set_rules('student_name', 'Student Name','required');
        $this->form_validation->set_rules('student_email', 'Student Email','required|valid_email|is_unique[std_info.std_email]',['is_unique'=> 'The {field} field is alredy exits']);

        $this->form_validation->set_message('valid_email','This {field} field is invalid');
        //$this->form_validation->set_message('is_unique', 'This {field} is Alreay exist!');


        $receive = $this->input->post(['student_name','student_email','student_about'], TRUE);
        $param = [
            'std_name'  => (string) $receive['student_name'],
            'std_email' => (string) $receive['student_email'],
            'std_about' => (string) $receive['student_about'],
            'images'    => $img_name
        ];
        if($this->form_validation->run()){
            $response = $this->std_model->insert_data('std_info',$param);
            if($response['status'] === 'success'){
                redirect('welcome');
            }
        }else{
            $this->load->view('create',$data);
            return;
        }

    }

1 个答案:

答案 0 :(得分:0)

  

如果上面列出的两个首选项都没有(create_thumb,和   使用new_image),调整大小的方法将改为目标   原始图像进行处理。

我测试了您的代码,至于我的怀疑,您应该执行以下操作:

$config2['image_library'] = 'gd2';
$config2['source_image'] = $image_data['full_path']; //get original image
//$config2 ['new_image'] = 'images'; //save as new image //need to create thumbs first
$config2 ['maintain_ratio'] = false;
$config2 ['create_thumb'] = false;
//$config2 ['overwrite'] = true;
$config2['width'] = 300;
$config2['height'] = 300;
  

从创建副本的文档中:“调整大小的方法将创建一个副本   图像文件(并保留原始文件)如果您设置路径和/或   使用此首选项的新文件名“

https://www.codeigniter.com/userguide3/libraries/image_lib.html

如果您想保留原件并有拇指,请在new_image字段中为文件创建新名称,如'images / someimage.jpg'或将create_thumb设为{{ 1}}。

重构

我也强烈建议重构你的逻辑。假设表单验证失败,您将留下大量孤立图像,因为每个帖子都会生成一个新图像(并且您不会覆盖或已知文件名)。首先进行表单验证,然后上传,然后添加到数据库:

TRUE