我是CodeIgniter的新手。这就是我想做的:
-
将产品图像调整为350*250、50*50和原始图像,同时从管理员上传添加产品中的图像。
-
三个不同文件夹中的所有这三个图像。
这是我的代码-
public function products() { $config=array(); $config['upload_path']="./uploads/"; $config['allowed_types']="jpg|jpeg|gif|png"; $this->load->library('upload',$config); $this->upload->initialize($config); if ( ! $this->upload->do_upload('userfile')) { $error = array('error' => $this->upload->display_errors()); } else { $file_data = $this->upload->data(); $data1['img']=base_url().'images/'.$file_data['file_name']; $data_image = array( 'agenda_id' => $this->input->post('agenda_id'), 'file' => $file_data['file_name'] ); $config['image_library']='gd2'; $config['source_image']='./uploads/'.$file_data["file_name"]; $config['create_thumb']=FALSE; $config['maintain_ratio']=FALSE; $config['quality']='60%'; $config['width']=350; $config['height']=250; $config['new_image']='./uploads350/'.$file_data["file_name"]; $this->upload->initialize($config); $this->load->library('image_lib',$config); $this->image_lib->resize(); } $this->load->model("Admin_model"); $data= array( "Type" =>$this->input->post("Type"), "Brand" =>$this->input->post("Brand"), "Product_Name" =>$this->input->post("Product_Name"), "Price" =>$this->input->post("Price"), "Image"=>$data_image['file'] ); $this->Admin_model->admin($data); $this->session->set_flashdata('message', 'Product added successfully'); redirect('Admin/product_list'); }
这应该可以正常工作(尚未测试(。只需确保文件夹存在即可。此外,如果我是你,我可能会把所有东西都放在同一个文件夹里,只需准备大小的后记,比如some_image_50.jpg
:
public function products() {
$config = array();
$config['upload_path'] = "./uploads/";
$config['allowed_types'] = "jpg|jpeg|gif|png";
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors()); // do something with this...
// e.g. show_error($this->upload->display_errors());
} else {
$file_data = $this->upload->data();
$data1['img'] = base_url() . 'images/' . $file_data['file_name'];
$data_image = array(
'agenda_id' => $this->input->post('agenda_id'),
'file' => $file_data['file_name']
);
//$config['image_library'] = 'gd2'; (default; not needed)
$this->load->library('image_lib');
$cfg['source_image'] = './uploads/' . $file_data["file_name"];
$cfg['create_thumb'] = FALSE;
$cfg['maintain_ratio'] = FALSE;
$cfg['quality'] = '60%';
$cfg['width'] = 350;
$cfg['height'] = 250;
$cfg['new_image'] = './uploads350/' . $file_data["file_name"];
//$this->upload->initialize($config); why??
$this->image_lib->initialize($cfg);
$this->image_lib->resize();
// the following vars will override certain vars above and keep the ones we haven't overridden
$cfg['width'] = 50;
$cfg['height'] = 50;
$cfg['new_image'] = './uploads50/' . $file_data["file_name"]; // make sure folder exists
$this->image_lib->initialize($cfg);
$this->image_lib->resize();
}
$this->load->model("Admin_model");
$data = array(
"Type" => $this->input->post("Type"),
"Brand" => $this->input->post("Brand"),
"Product_Name" => $this->input->post("Product_Name"),
"Price" => $this->input->post("Price"),
"Image" => $data_image['file']
);
$this->Admin_model->admin($data);
$this->session->set_flashdata('message', 'Product added successfully');
redirect('Admin/product_list');
}