我正在尝试上传图像并调整它们的大小,到目前为止,上传工作正在进行,还创建了拇指指甲图像。但是我无法调整原始图像的大小。所发生的只是文件以其原始大小直接上传到文件夹中。我想我正在尝试拍摄这张图片,调整大小,然后覆盖最初上传的文件。
这是我的代码,我缺少什么:
//Initialise the upload file class
$config['upload_path'] = './image_uploads/';
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = '6048';
$this->load->library('upload', $config);
$userfile = "_upload_image";
//check if a file is being uploaded
if(strlen($_FILES["_upload_image"]["name"])>0){
if ( !$this->upload->do_upload($userfile))//Check if upload is unsuccessful
{
$upload_errors = $this->upload->display_errors('', '');
$this->session->set_flashdata('errors', 'Error: '.$upload_errors);
redirect('admin/view_food/'.$food->course_id);
}
else
{
//$image_data = $this->upload->data();
////[ THUMB IMAGE ]
$config2['image_library'] = 'gd2';
$config2['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config2['new_image'] = './image_uploads/thumbs';
$config2['maintain_ratio'] = TRUE;
$config2['create_thumb'] = TRUE;
$config2['thumb_marker'] = '_thumb';
$config2['width'] = 75;
$config2['height'] = 50;
$this->load->library('image_lib',$config2);
if ( !$this->image_lib->resize()){
$this->session->set_flashdata('errors', $this->image_lib->display_errors('', ''));
}
//[ MAIN IMAGE ]
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
//$config['new_image'] = './image_uploads/';
$config['maintain_ratio'] = TRUE;
$config['width'] = 250;
$config['height'] = 250;
$this->load->library('image_lib',$config);
if ( !$this->image_lib->resize()){
$this->session->set_flashdata('errors', $this->image_lib->display_errors('', ''));
}
}
}
您是否尝试将其添加到配置选项中
$config['overwrite'] = TRUE;
这应该奏效。
根据文件
如果设置为true,则如果存在与正在上载的文件同名的文件,则该文件将被覆盖。如果设置为false,则如果存在另一个>相同名称的文件名,则会在文件名后面附加一个数字。
在过去的20个小时里,我一直在尝试创建缩略图,但我没有成功,直到我意识到我把代码搞砸了,我没有初始化我的配置数组!!!
只需在加载image_lib后添加这行代码
$this->image_lib->initialize($config);
希望这对你有用。。。。。。。。。。。竖起大拇指!
如果你喜欢玩带CI的Image,我强烈建议你使用Image_mo库。