Codeigniter文件上传和调整大小 [英] Codeigniter file upload and resize
本文介绍了Codeigniter文件上传和调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将个人资料照片上传到我的网站,第一个上传工作正常,但是当我尝试调整图像大小并将其重新上传到同一个文件夹时,就会中断。从测试中,我知道这是通过数组,并将数据插入到数据库,但不是重新保存到文件夹中。
这是我有一个。
$ 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->库('upload',$ config);
$ b $ 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->库('image_lib',$ resize);
$ this-> image_lib-> resize();
$ this-> load->库('upload',$ resize);
$ this-> users_model-> uploadPic($ resize ['file_path']);
重定向($ _ SERVER ['HTTP_REFERER']);
$ / code>
任何帮助都会非常感谢!
<$ c解决方案
c> $ config ['upload_path'] ='./uploads/';
$ config ['allowed_types'] ='gif | jpg | png | jpeg';
$ config ['max_size'] ='2048';
$ config ['encrypt_name'] = TRUE;
$ this-> load->库('upload',$ config);
$ b $ if(!$ this-> upload-> do_upload('logoUpload')){
$ this-> data ['error'] = array('error'= > $ this-> upload-> display_errors('< div class =alert alert-danger>','< / div>));
//错误
} 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->加载 - >库('image_lib',$ config);
$ this-> image_lib-> resize();
//添加到数据库
$ this-> settings_model-> upload_logo($ upload_data);
I'm uploading profile pictures to my website and the first upload is working fine, but when I try and resize the image and reupload it to the same folder it breaks. From testing, I know it is going through the array and it is inserting the data into the database, but just not re-saving into the folder.
This is what I have going one.
$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']);
}
Any help would be much appreciated!
解决方案
Just had the same issue, and solved it as follows:
$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);
这篇关于Codeigniter文件上传和调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文