/** * 上传操作 * * @param string $field 上传表单名 * @return bool */ public function upfile($field) { //上传文件 $this->upload_file = $_FILES[$field]; if ($this->upload_file['tmp_name'] == "") { $this->setError(Language::get('cant_find_temporary_files')); return false; } //对上传文件错误码进行验证 $error = $this->fileInputError(); if (!$error) { return false; } //验证是否是合法的上传文件 if (!is_uploaded_file($this->upload_file['tmp_name'])) { $this->setError(Language::get('upload_file_attack')); return false; } //验证文件大小 if ($this->upload_file['size'] == 0) { $error = Language::get('upload_file_size_none'); $this->setError($error); return false; } if ($this->upload_file['size'] > $this->max_size * 1024) { $error = Language::get('upload_file_size_cant_over') . $this->max_size . 'KB'; $this->setError($error); return false; } //文件后缀名 $tmp_ext = explode(".", $this->upload_file['name']); $tmp_ext = $tmp_ext[count($tmp_ext) - 1]; $this->ext = strtolower($tmp_ext); //验证文件格式是否为系统允许 if (!in_array($this->ext, $this->allow_type)) { $error = Language::get('image_allow_ext_is') . implode(',', $this->allow_type); $this->setError($error); return false; } //检查是否为有效图片 if (!($image_info = @getimagesize($this->upload_file['tmp_name']))) { $error = Language::get('upload_image_is_not_image'); $this->setError($error); return false; } //设置图片路径 $this->save_path = $this->setPath(); //设置文件名称 if (empty($this->file_name)) { $this->setFileName(); } //是否需要生成缩略图 $ifresize = false; if ($this->thumb_width && $this->thumb_height && $this->thumb_ext) { $thumb_width = explode(',', $this->thumb_width); $thumb_height = explode(',', $this->thumb_height); $thumb_ext = explode(',', $this->thumb_ext); if (count($thumb_width) == count($thumb_height) && count($thumb_height) == count($thumb_ext)) { $ifresize = true; } } //计算缩略图的尺寸 if ($ifresize) { for ($i = 0; $i < count($thumb_width); $i++) { $imgscaleto = $thumb_width[$i] == $thumb_height[$i]; if ($image_info[0] < $thumb_width[$i]) { $thumb_width[$i] = $image_info[0]; } if ($image_info[1] < $thumb_height[$i]) { $thumb_height[$i] = $image_info[1]; } $thumb_wh = $thumb_width[$i] / $thumb_height[$i]; $src_wh = $image_info[0] / $image_info[1]; if ($thumb_wh <= $src_wh) { $thumb_height[$i] = $thumb_width[$i] * ($image_info[1] / $image_info[0]); } else { $thumb_width[$i] = $thumb_height[$i] * ($image_info[0] / $image_info[1]); } if ($imgscaleto) { $scale[$i] = $src_wh > 1 ? $thumb_width[$i] : $thumb_height[$i]; // if ($this->config['thumb_type'] == 'gd'){ // $scale[$i] = $src_wh > 1 ? $thumb_width[$i] : $thumb_height[$i]; // }else{ // $scale[$i] = $src_wh > 1 ? $thumb_width[$i] : $thumb_height[$i]; // } } else { $scale[$i] = 0; } // if ($thumb_width[$i] == $thumb_height[$i]){ // $scale[$i] = $thumb_width[$i]; // }else{ // $scale[$i] = 0; // } } } //是否立即弹出错误 if ($this->if_show_error) { echo "<script type='text/javascript'>alert('" . ($this->if_show_error_one ? $error : $this->error) . "');history.back();</script>"; die; } if ($this->error != '') { return false; } if (@move_uploaded_file($this->upload_file['tmp_name'], BASE_UPLOAD_PATH . DS . $this->save_path . DS . $this->file_name)) { //产生缩略图 if ($ifresize) { $resizeImage = new ResizeImage(); $save_path = rtrim(BASE_UPLOAD_PATH . DS . $this->save_path, '/'); for ($i = 0; $i < count($thumb_width); $i++) { $resizeImage->newImg($save_path . DS . $this->file_name, $thumb_width[$i], $thumb_height[$i], $scale[$i], $thumb_ext[$i] . '.', $save_path, $this->filling); if ($i == 0) { $resize_image = explode('/', $resizeImage->relative_dstimg); $this->thumb_image = $resize_image[count($resize_image) - 1]; } } } //删除原图 if ($this->ifremove && is_file(BASE_UPLOAD_PATH . DS . $this->save_path . DS . $this->file_name)) { @unlink(BASE_UPLOAD_PATH . DS . $this->save_path . DS . $this->file_name); } return true; } else { $this->setError(Language::get('upload_file_fail')); return false; } // $this->setErrorFileName($this->upload_file['tmp_name']); return $this->error; }
/** * 裁剪指定图片 * * @param string $field 上传表单名 * @return bool */ public function create_thumb($pic_path) { if (!file_exists($pic_path)) { return; } //是否需要生成缩略图 $ifresize = false; if ($this->thumb_width && $this->thumb_height && $this->thumb_ext) { $thumb_width = explode(',', $this->thumb_width); $thumb_height = explode(',', $this->thumb_height); $thumb_ext = explode(',', $this->thumb_ext); if (count($thumb_width) == count($thumb_height) && count($thumb_height) == count($thumb_ext)) { $ifresize = true; } } $image_info = @getimagesize($pic_path); //计算缩略图的尺寸 if ($ifresize) { for ($i = 0; $i < count($thumb_width); $i++) { $imgscaleto = $thumb_width[$i] == $thumb_height[$i]; if ($image_info[0] < $thumb_width[$i]) { $thumb_width[$i] = $image_info[0]; } if ($image_info[1] < $thumb_height[$i]) { $thumb_height[$i] = $image_info[1]; } $thumb_wh = $thumb_width[$i] / $thumb_height[$i]; $src_wh = $image_info[0] / $image_info[1]; if ($thumb_wh <= $src_wh) { $thumb_height[$i] = $thumb_width[$i] * ($image_info[1] / $image_info[0]); } else { $thumb_width[$i] = $thumb_height[$i] * ($image_info[0] / $image_info[1]); } if ($imgscaleto) { $scale[$i] = $src_wh > 1 ? $thumb_width[$i] : $thumb_height[$i]; } else { $scale[$i] = 0; } } } //产生缩略图 if ($ifresize) { $resizeImage = new ResizeImage(); $save_path = rtrim(BASE_UPLOAD_PATH . DS . $this->save_path, '/'); for ($i = 0; $i < count($thumb_width); $i++) { // $resizeImage->newImg($save_path.DS.$this->file_name,$thumb_width[$i],$thumb_height[$i],$scale[$i],$thumb_ext[$i].'.',$save_path,$this->filling); $resizeImage->newImg($pic_path, $thumb_width[$i], $thumb_height[$i], $scale[$i], $thumb_ext[$i] . '.', dirname($pic_path), $this->filling); } } }
public function upfile($field) { $this->upload_file = $_FILES[$field]; if ($this->upload_file['tmp_name'] == "") { $this->setError(Language::get("cant_find_temporary_files")); return FALSE; } $error = $this->fileInputError(); if (!$error) { return FALSE; } if (!is_uploaded_file($this->upload_file['tmp_name'])) { $this->setError(Language::get("upload_file_attack")); return FALSE; } if ($this->upload_file['size'] == 0) { $error = Language::get("upload_file_size_none"); $this->setError($error); return FALSE; } if ($this->max_size * 1024 < $this->upload_file['size']) { $error = Language::get("upload_file_size_cant_over") . $this->max_size . "KB"; $this->setError($error); return FALSE; } $tmp_ext = explode(".", $this->upload_file['name']); $tmp_ext = $tmp_ext[count($tmp_ext) - 1]; $this->ext = strtolower($tmp_ext); if (!in_array($this->ext, $this->allow_type)) { $error = Language::get("image_allow_ext_is") . implode(",", $this->allow_type); $this->setError($error); return FALSE; } if (!($image_info = @getimagesize($this->upload_file['tmp_name']))) { $error = Language::get("upload_image_is_not_image"); $this->setError($error); return FALSE; } $this->save_path = $this->setPath(); if (empty($this->file_name)) { $this->setFileName(); } $ifresize = FALSE; if ($this->thumb_width && $this->thumb_height && $this->thumb_ext) { $thumb_width = explode(",", $this->thumb_width); $thumb_height = explode(",", $this->thumb_height); $thumb_ext = explode(",", $this->thumb_ext); if (count($thumb_width) == count($thumb_height) && count($thumb_height) == count($thumb_ext)) { $ifresize = TRUE; } } if ($ifresize) { $i = 0; for (; $i < count($thumb_width); ++$i) { if ($image_info[0] < $thumb_width[$i]) { $thumb_width[$i] = $image_info[0]; } if ($image_info[1] < $thumb_height[$i]) { $thumb_height[$i] = $image_info[1]; } $thumb_wh = $thumb_width[$i] / $thumb_height[$i]; $src_wh = $image_info[0] / $image_info[1]; if ($thumb_wh <= $src_wh) { $thumb_height[$i] = $thumb_width[$i] * ($image_info[1] / $image_info[0]); } else { $thumb_width[$i] = $thumb_height[$i] * ($image_info[0] / $image_info[1]); } } } if ($this->if_show_error) { echo "<script type='text/javascript'>alert('" . ($this->if_show_error_one ? $error : $this->error) . "');history.back();</script>"; exit; } if (@copy($this->upload_file['tmp_name'], BasePath . DS . $this->save_path . DS . $this->file_name)) { if ($ifresize) { require_once BasePath . "/framework/libraries/resizeimage.php"; $resizeImage = new ResizeImage(); $save_path = BasePath . DS . $this->save_path; $i = 0; for (; $i < count($thumb_width); ++$i) { $resizeImage->newImg($save_path . DS . $this->file_name, $thumb_width[$i], $thumb_height[$i], $this->resize_cut, $thumb_ext[$i] . ".", $save_path); if ($i == 0) { $resize_image = explode("/", $resizeImage->relative_dstimg); $this->thumb_image = $resize_image[count($resize_image) - 1]; } } } if ($this->ifremove && is_file(BasePath . DS . $this->save_path . DS . $this->file_name)) { @unlink(BasePath . DS . $this->save_path . DS . $this->file_name); } return TRUE; } $this->setError(Language::get("upload_file_fail")); return FALSE; }