无法在Codeigniter中调整照片大小

时间:2019-08-01 10:04:24

标签: codeigniter image-resizing

上传到路径中时,我需要调整照片的大小,但是无法调整大小,请在此处对我的代码进行更正:

private function _do_upload()
 {

 $config['upload_path']    = './uploads/';
 $config['allowed_types']  = '*';
 $config['file_name']      = slug($this->input->post('judul')).'_'.time();
 $config['image_library']  = 'gd2';
 $config['quality']        = '20%';
 $config['remove_space']   = TRUE;
 $config['create_thumb']   = TRUE;
 $config['maintain_ratio'] = TRUE;
 $config['width']          = 75;
 $config['height']         = 50;

        $this->load->library('image_lib', $config);
        $this->image_lib->initialize($config);
        $this->image_lib->resize();      
        $this->load->library('upload',$config);

    if (!$this->upload->do_upload('photo')) {

          $this->session->set_flashdata('msg', $this->upload->display_errors('',''));
          redirect('backoffice/tambah_video');
    }
          return $this->upload->data('file_name');
}

2 个答案:

答案 0 :(得分:0)

请指定新的图像路径,然后尝试保存。

$config['new_image'] = '/path/to/new_image.jpg';

还要确保已启用GD库。

答案 1 :(得分:0)

您需要在{strong> resize()之后执行do_upload(),并分隔image_libupload库的配置,现在它们已经混合了。 试试这个:

private function _do_upload()
{
    // step 1: upload
    $upload_config['upload_path']    = './uploads/';
    $upload_config['allowed_types']  = '*';
    $upload_config['file_name']      = slug($this->input->post('judul')).'_'.time();

    $this->load->library('upload', $upload_config);

    if (!$this->upload->do_upload('photo')) {
          $this->session->set_flashdata('msg', $this->upload->display_errors('',''));
          redirect('backoffice/tambah_video');
    }

    // step 2: resize
    $resize_config['source_image']   = $this->upload->upload_path . $this->upload->file_name; // uploaded file path
    $resize_config['image_library']  = 'gd2';
    $resize_config['quality']        = '20%';
    $resize_config['remove_space']   = TRUE;
    $resize_config['create_thumb']   = TRUE; 
    $resize_config['maintain_ratio'] = TRUE;
    $resize_config['width']          = 75;
    $resize_config['height']         = 50;

    $this->load->library('image_lib', $resize_config);
    $this->image_lib->resize();

    return $this->upload->data('file_name');
}

如果$resize_config['create_thumb'] = TRUE;将在小图像的名称后加上_thumb。如果您需要覆盖上载的图像,请执行$resize_config['create_thumb'] = FALSE;。另请参阅官方文档中的thumb_markernew_image配置。