Codeigniter文件上传和调整大小

3
我正在将个人资料照片上传到我的网站,第一次上传正常工作,但当我尝试调整图像大小并重新上传到同一文件夹时,它会出错。经过测试,我知道它正在遍历数组并将数据插入数据库,但只是没有重新保存到文件夹中。
以下是我的操作步骤。
$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个回答

5

我曾经遇到相同的问题,以下是解决方法:

$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

这是来自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'] );

抱歉,您可以从这里获取它。并在核心文件夹中创建uploads/cache文件夹,其中index.php保留不变。用法:<img src="/thumb/view/w/640/h/480/src/images/example.jpg" alt="Example" />。thumb.php是控制器。 - Furkat U.
我觉得我在这里有些误解了。我不是在寻找一个缩略图生成器,我只是想调整用户上传的图像大小,以便如果他们上传的图像太大,它将被缩小,我就不会托管或缓存更大的图像。 - zazvorniki
虽然那个方法不起作用,但我最初的代码就是基于它的。它没有上传调整大小后的图像,只上传了原始图像。 - zazvorniki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接