codeigniter $this->image_lib->resize() 函数不调整图像大小
codeigniter $this->image_lib->resize() function not resizing image
我想调整图像大小,但它不能正常工作,$this->image_lib->resize() 这个函数不能调整图像大小。它产生原始图像尺寸。我正在上传 600*600 尺寸的图片。但我想调整大小 300*300
请检查下面我的代码。
if( $this->input->post('save')){
$config['upload_path'] = 'images';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 1000;
$config['overwrite'] = false;
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if(!$this->upload->do_upload('user_image')){
$this->session->set_flashdata('message', $this->upload->display_errors());
$this->load->view('create',$data);
return;
}else{
$upicture = $this->upload->data();
$image_data = $upicture;
$config2['image_library'] = 'gd2';
$config2['source_image'] = $image_data['full_path']; //get original image
$config2 ['new_image'] = 'images'; //save as new image //need to create thumbs first
$config2 ['maintain_ratio'] = false;
$config2 ['create_thumb'] = false;
$config2 ['overwrite'] = false;
$config2['width'] = 300;
$config2['height'] = 300;
$this->load->library('image_lib', $config2);
$this->image_lib->resize();
$img_name = $upicture['file_name'];
}
$this->form_validation->set_rules('student_name', 'Student Name','required');
$this->form_validation->set_rules('student_email', 'Student Email','required|valid_email|is_unique[std_info.std_email]',['is_unique'=> 'The {field} field is alredy exits']);
$this->form_validation->set_message('valid_email','This {field} field is invalid');
//$this->form_validation->set_message('is_unique', 'This {field} is Alreay exist!');
$receive = $this->input->post(['student_name','student_email','student_about'], TRUE);
$param = [
'std_name' => (string) $receive['student_name'],
'std_email' => (string) $receive['student_email'],
'std_about' => (string) $receive['student_about'],
'images' => $img_name
];
if($this->form_validation->run()){
$response = $this->std_model->insert_data('std_info',$param);
if($response['status'] === 'success'){
redirect('welcome');
}
}else{
$this->load->view('create',$data);
return;
}
}
If neither of the two preferences listed above (create_thumb, and
new_image) are used, the resizing method will instead target the
original image for processing.
我测试了您的代码,根据我的猜测,您应该执行以下操作:
$config2['image_library'] = 'gd2';
$config2['source_image'] = $image_data['full_path']; //get original image
//$config2 ['new_image'] = 'images'; //save as new image //need to create thumbs first
$config2 ['maintain_ratio'] = false;
$config2 ['create_thumb'] = false;
//$config2 ['overwrite'] = true;
$config2['width'] = 300;
$config2['height'] = 300;
From the documentation on Creating a Copy: "The resizing method will create a copy of the
image file (and preserve the original) if you set a path and/or a
new filename using this preference"
https://www.codeigniter.com/userguide3/libraries/image_lib.html
如果您想保留原件并留个拇指,请在 new_image
字段中为文件重新命名,如 'images/someimage.jpg' 或将 create_thumb
设置为 TRUE
。
重构
我还强烈建议重构您的逻辑。假设表单验证失败,你会留下很多孤立的图像,因为每个 post 都会制作一个新图像(并且你没有覆盖或已知文件名)。所以先做表单验证,然后上传,然后添加到数据库:
if ($this->input->post('save')) {
$this->form_validation->set_rules('student_name', 'Student Name', 'required');
$this->form_validation->set_rules('student_email', 'Student Email', 'required|valid_email|is_unique[std_info.std_email]', ['is_unique' => 'The {field} field is alredy exits']);
$this->form_validation->set_message('valid_email', 'This {field} field is invalid');
if (!$this->form_validation->run()) {
$this->load->view('create', $data);
return;
}
$config['upload_path'] = 'images';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 1000;
$config['overwrite'] = false;
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('user_image')) {
$this->session->set_flashdata('message', $this->upload->display_errors());
$this->load->view('create', $data);
return;
}
$image_data = $this->upload->data();
$config2['image_library'] = 'gd2';
$config2['source_image'] = $image_data['full_path']; //get original image
//$config2['new_image'] = 'images'; //save as new image //need to create thumbs first
$config2['maintain_ratio'] = false;
$config2['create_thumb'] = false;
//$config2['overwrite'] = false;
$config2['width'] = 300;
$config2['height'] = 300;
$this->load->library('image_lib', $config2);
if (!$this->image_lib->resize()) {
$this->session->set_flashdata('message', $this->image_lib->display_errors());
$this->load->view('create', $data);
return;
}
$receive = $this->input->post(['student_name', 'student_email', 'student_about'], TRUE);
$param = [
'std_name' => (string) $receive['student_name'],
'std_email' => (string) $receive['student_email'],
'std_about' => (string) $receive['student_about'],
'images' => $image_data['file_name']
];
$response = $this->std_model->insert_data('std_info', $param);
if ($response['status'] === 'success') {
redirect('welcome');
}
}
我想调整图像大小,但它不能正常工作,$this->image_lib->resize() 这个函数不能调整图像大小。它产生原始图像尺寸。我正在上传 600*600 尺寸的图片。但我想调整大小 300*300 请检查下面我的代码。
if( $this->input->post('save')){
$config['upload_path'] = 'images';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 1000;
$config['overwrite'] = false;
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if(!$this->upload->do_upload('user_image')){
$this->session->set_flashdata('message', $this->upload->display_errors());
$this->load->view('create',$data);
return;
}else{
$upicture = $this->upload->data();
$image_data = $upicture;
$config2['image_library'] = 'gd2';
$config2['source_image'] = $image_data['full_path']; //get original image
$config2 ['new_image'] = 'images'; //save as new image //need to create thumbs first
$config2 ['maintain_ratio'] = false;
$config2 ['create_thumb'] = false;
$config2 ['overwrite'] = false;
$config2['width'] = 300;
$config2['height'] = 300;
$this->load->library('image_lib', $config2);
$this->image_lib->resize();
$img_name = $upicture['file_name'];
}
$this->form_validation->set_rules('student_name', 'Student Name','required');
$this->form_validation->set_rules('student_email', 'Student Email','required|valid_email|is_unique[std_info.std_email]',['is_unique'=> 'The {field} field is alredy exits']);
$this->form_validation->set_message('valid_email','This {field} field is invalid');
//$this->form_validation->set_message('is_unique', 'This {field} is Alreay exist!');
$receive = $this->input->post(['student_name','student_email','student_about'], TRUE);
$param = [
'std_name' => (string) $receive['student_name'],
'std_email' => (string) $receive['student_email'],
'std_about' => (string) $receive['student_about'],
'images' => $img_name
];
if($this->form_validation->run()){
$response = $this->std_model->insert_data('std_info',$param);
if($response['status'] === 'success'){
redirect('welcome');
}
}else{
$this->load->view('create',$data);
return;
}
}
If neither of the two preferences listed above (create_thumb, and new_image) are used, the resizing method will instead target the original image for processing.
我测试了您的代码,根据我的猜测,您应该执行以下操作:
$config2['image_library'] = 'gd2';
$config2['source_image'] = $image_data['full_path']; //get original image
//$config2 ['new_image'] = 'images'; //save as new image //need to create thumbs first
$config2 ['maintain_ratio'] = false;
$config2 ['create_thumb'] = false;
//$config2 ['overwrite'] = true;
$config2['width'] = 300;
$config2['height'] = 300;
From the documentation on Creating a Copy: "The resizing method will create a copy of the image file (and preserve the original) if you set a path and/or a new filename using this preference"
https://www.codeigniter.com/userguide3/libraries/image_lib.html
如果您想保留原件并留个拇指,请在 new_image
字段中为文件重新命名,如 'images/someimage.jpg' 或将 create_thumb
设置为 TRUE
。
重构
我还强烈建议重构您的逻辑。假设表单验证失败,你会留下很多孤立的图像,因为每个 post 都会制作一个新图像(并且你没有覆盖或已知文件名)。所以先做表单验证,然后上传,然后添加到数据库:
if ($this->input->post('save')) {
$this->form_validation->set_rules('student_name', 'Student Name', 'required');
$this->form_validation->set_rules('student_email', 'Student Email', 'required|valid_email|is_unique[std_info.std_email]', ['is_unique' => 'The {field} field is alredy exits']);
$this->form_validation->set_message('valid_email', 'This {field} field is invalid');
if (!$this->form_validation->run()) {
$this->load->view('create', $data);
return;
}
$config['upload_path'] = 'images';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 1000;
$config['overwrite'] = false;
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('user_image')) {
$this->session->set_flashdata('message', $this->upload->display_errors());
$this->load->view('create', $data);
return;
}
$image_data = $this->upload->data();
$config2['image_library'] = 'gd2';
$config2['source_image'] = $image_data['full_path']; //get original image
//$config2['new_image'] = 'images'; //save as new image //need to create thumbs first
$config2['maintain_ratio'] = false;
$config2['create_thumb'] = false;
//$config2['overwrite'] = false;
$config2['width'] = 300;
$config2['height'] = 300;
$this->load->library('image_lib', $config2);
if (!$this->image_lib->resize()) {
$this->session->set_flashdata('message', $this->image_lib->display_errors());
$this->load->view('create', $data);
return;
}
$receive = $this->input->post(['student_name', 'student_email', 'student_about'], TRUE);
$param = [
'std_name' => (string) $receive['student_name'],
'std_email' => (string) $receive['student_email'],
'std_about' => (string) $receive['student_about'],
'images' => $image_data['file_name']
];
$response = $this->std_model->insert_data('std_info', $param);
if ($response['status'] === 'success') {
redirect('welcome');
}
}