Codeigniter文件上传和调整大小

时间:2013-06-05 21:11:57

标签: codeigniter file-upload resize

我正在将个人资料图片上传到我的网站,并且第一次上传工作正常,但是当我尝试调整图片大小并将其重新上传到同一个文件夹时,它会中断。从测试开始,我知道它正在通过数组,它将数据插入到数据库中,但是没有重新保存到文件夹中。

这就是我的目标。

$config['upload_path'] = './uploads/';
$config['file_name'] = $this->user->pen_name.'.jpg';
$config['file_path'] = './uploads/'.$this->user->pen_name.'.jpg';
$config['max-size'] = 2000;
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['remove_spaces'] = true;
$config['max_width'] = 2000;
$config['max_height'] = 3000;
$this->load->library('upload', $config);

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

        $error = array('error' => $this->upload->display_errors());
}else{
    $resize['source_image'] = base_url().'/uploads/'.$config['file_name'];
    $resize['new_image'] = './uploads/';
    $resize['file_path'] = './uploads/'.$this->user->pen_name.'.jpg';
    $resize['create_thumb'] = false;
    $resize['maintain_ratio'] = true;
    $resize['width'] = 200;
    $resize['height'] = 300;
    $resize['overwrite'] = TRUE;
    $this->load->library('image_lib', $resize);
    $this->image_lib->resize();
    $this->load->library('upload', $resize);
    $this->users_model->uploadPic($resize['file_path']);
    redirect($_SERVER['HTTP_REFERER']);
}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:4)

刚刚遇到同样的问题,并按如下方式解决:

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);

if (!$this->upload->do_upload('logoUpload')) {
    $this->data['error'] = array('error' => $this->upload->display_errors('<div class="alert alert-danger">', '</div>'));
    //error
} else {

    $upload_data = $this->upload->data();

    //resize:

    $config['image_library'] = 'gd2';
    $config['source_image'] = $upload_data['full_path'];
    $config['maintain_ratio'] = TRUE;
    $config['width']     = 150;
    $config['height']   = 50;
    $this->load->library('image_lib', $config); 
    $this->image_lib->resize();

    //add to the DB
    $this->settings_model->upload_logo($upload_data);
}

答案 1 :(得分:1)

这些是codeigniter的默认配置,只需将unlink放在最后:

$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;

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

$this->image_lib->resize();

//remove original file
@unlink( $config['source_image'] );