我使用codeigniter上传图片:
1)路径//上传/属性/ ..(原始图片)
2)路径/上传/ Property / 100X100 / ..(裁剪图像)
3)路径/上传/ Property / 300X300 / ..(裁剪图像)
我的代码片段:
public function photoform(){
$cpt="";
$prp_id=$this->session->userdata('main_prop_id');
$user_email =$this->session->userdata('email');
if($_FILES['userfile']['name'] != ""){
$allowed = array('jpg','jpeg' ,'gif','png');
$filename = $_FILES['userfile']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$testimg =$_FILES["userfile"]["name"];
$ppfilename = preg_replace("/[^A-Za-z0-9_\w]+/", "", $testimg).".".$ext;
$ppfilename = $ppfilename;
$ppfilename = explode(".", $ppfilename);
$ppnew_filename = "ad_" . $ppfilename[0] . time() . "." . $ppfilename[1];
$ppthumb = explode(".", $ppnew_filename);
$ppthumb = $ppthumb[0] . "_thumb". "." . $ppthumb[1];
$_FILES["userfile"]["name"] = $ppnew_filename;
$this->load->library("upload");
$this->upload->initialize($this->set_upload_options());
if($this->upload->do_upload()){
$ppimagedata = $this -> upload -> data();
$ppnewimagename = $ppimagedata["file_name"];
$this -> load -> library("image_lib");
$config['image_library'] = 'gd2';
$config['source_image'] = $ppimagedata["full_path"];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['new_image'] = './uploads/Property/100X100/';
$config['width'] = 100;
$config['height'] = 100;
$this -> image_lib -> initialize($config);
$this -> image_lib -> resize();
$config['new_image'] = './uploads/Property/300X300/';
$config['width'] = 300;
$config['height'] = 300;
$this -> image_lib -> initialize($config);
$this -> image_lib -> resize();
}
}
else{
$ppnewimagename = "";
$ppthumb = "";
}
$room_id = $this->input->post('roomid');
$where = array('room_id'=>$room_id,'prop_id' => $prp_id,'user'=>$user_email);
$arr3 = array(
'image_name' => $ppnewimagename,
'image_thumb' => $ppthumb,
);
$insert_id = $this->home->updateTable('ci_room_details',$arr3,$where);
$arr_f = array(
'property_id_fk' => $prp_id,
'image_name' => $ppnewimagename,
'image_thumb' => $ppthumb,
'room_id'=>$_POST['roomid'],
'entity_type_id_fk'=>$_POST['entity_type_id'],
'sub_entity_type_id_fk'=>$_POST['sub_entity_type_id']
);
$insert_id_f = $this->home->insertIntoTable('ci_property_images',$arr_f);
$cpt = count($_FILES['images']['name']);
if($cpt > 0){
$img = array();
for($i = 0; $i < $cpt; $i++) {
if($_FILES['images']['name'][$i] == ""){
unset($_FILES['images']['name'][$i]);
unset($_FILES['images']['type'][$i]);
unset($_FILES['images']['tmp_name'][$i]);
unset($_FILES['images']['error'][$i]);
unset($_FILES['images']['size'][$i]);
}
}
$cpt = count($_FILES['images']['name']);
for($i = 0; $i < $cpt; $i++) {
if(isset($_FILES['images']['name'][$i])){
if($_FILES['images']['name'][$i] != ""){
$filename = $_FILES['images']['name'][$i];
$ext1 = pathinfo($filename, PATHINFO_EXTENSION);
$ranstr=random_string('alnum', 3);
$new_filename = time() .$ranstr. "." . $ext1;
$thumb = time() . $ranstr. "_thumb." . $ext1;
$_FILES['imag']['name'] = $new_filename;
$_FILES['imag']['type'] = $_FILES['images']['type'] [$i];
$_FILES['imag']['tmp_name'] = $_FILES['images']['tmp_name'] [$i];
$_FILES['imag']['error'] = $_FILES ['images']['error'] [$i];
$_FILES['imag']['size'] = $_FILES ['images']['size'] [$i];
$this->upload->initialize ( $this->set_upload_options());
if($this->upload->do_upload ('imag')){
$imagedata = $this -> upload -> data();
$newimagename = $imagedata["file_name"];
//100x100 resize thumb
$this -> load -> library("image_lib");
$config['image_library'] = 'gd2';
$config['source_image'] = $imagedata["full_path"];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['new_image'] = './uploads/Property/100X100/';
$config['width'] = 100;
$config['height'] = 100;
$this -> image_lib -> initialize($config);
$this -> image_lib -> resize();
//300x300 resize img
$config['source_image'] = $imagedata["full_path"];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['new_image'] = './uploads/Property/300X300/';
$config['width'] = 300;
$config['height'] = 300;
$this -> image_lib -> initialize($config);
$this -> image_lib -> resize();
}
unset($_FILES['imag']);
$img[] = array(
'image_name' => $new_filename,
'image_thumb' => $thumb
);
}
}// end if
} // end for
}
else{
$img[] = array(
'image_name' => "",
'image_thumb' => ""
);
}
$img_cnt=count($img);
if($img_cnt > 0){
for($i = 0; $i < $img_cnt; $i++) {
$arr3 = array(
'property_id_fk' => $prp_id,
'image_name' => $img[$i]['image_name'],
'image_thumb' => $img[$i]['image_thumb'],
'room_id'=>$_POST['roomid'],
'entity_type_id_fk'=>$_POST['entity_type_id'],
'sub_entity_type_id_fk'=>$_POST['sub_entity_type_id']
);
$insert_id1 = $this->home->insertIntoTable('ci_property_images',$arr3);
}
}
if($insert_id!=""){
echo $room_id;
}else{
echo "";
}
exit;
}
适用于尺寸较小的图片,但在高尺寸图片上传过程中出现问题...由于上传mozilla需要很长时间,因此需要$ _FILES [&#39; userfile&#39;] [&#39; ..&#39 ;](未定义的索引错误,但工作正常的低尺寸图像)&amp; chrome警告可以杀死操作。
我提议上传5张图片,意味着15张图片(包括裁剪图片)
请在上面的代码中建议更改..如何加快上传过程?
答案 0 :(得分:0)
PHP的默认值会将您限制为最大2 MB
个上传文件大小。因此,要上传超过2 MB
的文件,您需要设置值:
upload_max_filesize
中的 post_max_size
和php.ini
:
; Maximum allowed size for uploaded files.
upload_max_filesize = 10M
; Must be greater than or equal to upload_max_filesize
post_max_size = 10M
然后重新启动服务器以应用这些设置。