การ Upload และ Resize รูปภาพแบบ Array Codeigniter

 

โค๊ดการอัพโหลดรูปภาพ พร้อมกับ Resize รูปภาพเป็นภาพขนาดกลางและ Thumbnail

$files = $this->input->post(‘files’);

$config[‘upload_path’] = ‘— path —;
$config[‘allowed_types’] = ‘gif|jpg’;
$config[‘max_size’] = 1024 * 8;
$config[‘encrypt_name’] = FALSE;

$rand = rand(1111,9999);
$date= date(“Y-m-d “);
$config[‘file_name’] = $date.$rand;

$this->load->library(‘upload’);

$files = $_FILES;
$cpt = count($_FILES[‘files’][‘name’]);
for($i=0; $i<$cpt; $i++)
{
$_FILES[‘files’][‘name’]= $files[‘files’][‘name’][$i];
$_FILES[‘files’][‘type’]= $files[‘files’][‘type’][$i];
$_FILES[‘files’][‘tmp_name’]= $files[‘files’][‘tmp_name’][$i];
$_FILES[‘files’][‘error’]= $files[‘files’][‘error’][$i];
$_FILES[‘files’][‘size’]= $files[‘files’][‘size’][$i];

$this->upload->initialize($config);
$this->upload->do_upload(‘files’);
$tmp = $this->upload->data();

$this->load->library(‘image_lib’);
//Thumbnail configs
$config_t[‘source_image’] = ‘— path —‘ . $tmp[‘file_name’];
$config_t[‘new_image’] = ‘— Thumbnail path —‘ . $tmp[‘file_name’];
$config_t[‘create_thumb’] = FALSE;
$config_t[‘maintain_ratio’] = TRUE;
$config_t[‘width’] = 155;
$config_t[‘height’] = 120;

$config_r[‘source_image’] = ‘— path —‘ . $tmp[‘file_name’];
$config_r[‘new_image’] = ‘— Medium path — . $tmp[‘file_name’];
$config_r[‘create_thumb’] = FALSE;
$config_r[‘maintain_ratio’] = TRUE;
$config_r[‘width’] = 960;
$config_r[‘height’] = 750;
$config_r[‘quality’] = 100;
//end of configs

//load library once
$this->load->library(‘image_lib’);

//use first config
$this->image_lib->initialize($config_t);
//run resize
if(!$this->image_lib->resize())
{
echo “Failed.” . $this->image_lib->display_errors();
}
//clear
$this->image_lib->clear();

//initialize second config
$this->image_lib->initialize($config_r);
//run resize
if(!$this->image_lib->resize())
{
echo “Failed.” . $this->image_lib->display_errors();
}
//clear
$this->image_lib->clear();

}

 

รับทำเว็บไซต์