我有一个可以上传 3 张图片的表格。在 codeigniter 中,我创建了一个 foreach 循环来处理图像,并将它们保存到我的数据库中。这很好用。
但我需要调整图像大小。当我上传一张图片时,调整大小有效,但超过一张图片,第一张图片看起来像狗屎,第二张和第三张图片不会调整大小......你们能帮帮我吗?
$this->load->library('upload');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['encrypt_name'] = TRUE;
//$config['overwrite'] = TRUE;
$this->upload->initialize($config);
$count = 0;
foreach($_FILES as $field => $file)
{
$count = $count + 1;
// No problems with the file
if($file['error'] == 0)
{
// So lets upload
if ($this->upload->do_upload($field))
{
$data = $this->upload->data();
$data2 = array(
'image' . $count => $data['file_name']
);
$this->db->where('id', $query_id);
$this->db->update('ads', $data2);
//afbeeldingen verkleinen.
$config['image_library'] = 'gd2';
$config['source_image'] = $data['full_path'];
//$config['new_image'] = './image_uploads/';
$config['maintain_ratio'] = TRUE;
$config['width'] = 370;
$config['height'] = 277;
$this->load->library('image_lib',$config);
if ( !$this->image_lib->resize()){
$this->session->set_flashdata('errors', $this->image_lib->display_errors('', ''));
echo $this->image_lib->display_errors('', '');
exit();
}
}
else
{
$errors = $this->upload->display_errors();
echo $errors;
exit();
}
}
}
$data['name'] = $name;
redirect('/adDetail/getDetails/' . $query_id, 'location', 200);
}