文件上传类(如果是图片支持生成缩略图)

<?php
/******************文件上传***********************/
/******************制作:小小木同******************/
//Email:jsrgqinbin@126.com
//$saveName;保存名
//$savePath;保存路径
//$smallpath;缩略图路径
//$picturename;成功上传的图片的名称
//$smallpicturename;成功生成缩略图的名称
//$fdate;路径下所生成的目录
//$exname;文件扩展名
//$errormsg;//出错的提示信息
//$fileFormat = array(‘.gif’,’.jpg’,’.jpeg’,’.JPG’,’.GIF’);// 文件格式
//$maxSize = 0;// 文件最大字节
//$thumb = 0;// 是否生成缩略图
//$thumbWidth = 100;// 缩略图宽
//$thumbHeight = 100;// 缩略图高
/*************************************************/
class UploadPic{
var $saveName;
var $savePath;
var $smallpath;
var $picturename;
var $smallpicturename;
var $fdate;
var $errormsg;
var $exname;
var $fileFormat = array(‘.gif’,’.jpg’,’.jpeg’,’.JPG’,’.GIF’);
var $maxSize = 0;
var $thumb = 0;
var $thumbWidth = 100;
var $thumbHeight = 100;

function __construct($path,$fileFormat=”,$maxSize=0){
$this->setSavepath($path);
$this->setFileformat($fileFormat);
$this->setMaxsize($maxSize);
$this->fdate = date(“Ymd”);
}

function setSavepath($savePath)
{
$this->savePath = $savePath;
$this->smallpath = $savePath.”/small”;
}

function getsavaPath()
{
return str_replace($this->savePath,”,$this->picturename);
}

function getsmallpath()
{
return str_replace($this->savePath,”,$this->smallpicturename);
}

function setFileformat($fileFormat){
if(is_array($fileFormat)){$this->fileFormat = $fileFormat ;}
}

function setMaxsize($maxSize)
{
$this->maxSize = $maxSize;
}

function setThumb($thumb,$thumbWidth=100,$thumbHeight=100)
{
$this->thumb = $thumb;
$this->thumbWidth = $thumbWidth;
$this->thumbHeight = $thumbHeight;
}

function geterrormsg()
{
return $this->errormsg.$this->exname;
}
function debug_error($uperror)
{
switch ($uperror) {
case 1:
$this->errormsg = “上传图片超过PHP设置允许的最大值”;
return false;
break;
case 2:
$this->errormsg = “上传超过了表单允许的最大值”;
return false;
break;
case 3:
$this->errormsg = “文件只有部分被上传了”;
return false;
break;
case 4:
$this->errormsg = “文件不有被上传”;
return false;
break;
case 6:
$this->errormsg = “找不到临时上传文件,上传失败”;
return false;
break;
default:return true;
}
}

function makedir()
{
//更新:上传多个图片时要带入一个区分的值,不然只能上传最后一张图片
global $i;
if (!file_exists($this->savePath)){
mkdir($this->savePath,0777);
mkdir($this->savePath.”/”.$this->fdate,0777);
}else{
if (!file_exists($this->savePath.”/”.$this->fdate)){
mkdir($this->savePath.”/”.$this->fdate,0777);
}
}
$picname = “kt”.time().$i;
$bigname = $this->savePath.”/”.$this->fdate.”/”.$picname;
$this->picturename = $bigname.$this->exname;
//缩略图
if ($this->thumb)
{
if (!file_exists($this->smallpath)){
mkdir($this->smallpath,0777);
mkdir($this->smallpath.”/”.$this->fdate,0777);
}else{
if (!file_exists($this->smallpath.”/”.$this->fdate)){
mkdir($this->smallpath.”/”.$this->fdate,0777);
}
}
$this->smallpicturename = $this->smallpath.”/”.$this->fdate.”/kt”.time().”.jpg”;
}
}

//生成目录
function makepath($path){
//这个\没考虑
$detail=explode(“/”,$path);
foreach($detail AS $key=>$value){
if($value==”&&$key!=0){
//continue;
}
$newpath.=”$value/”;
if((eregi(“^\/”,$newpath)||eregi(“:”,$newpath))&&!strstr($newpath,WEB_PATH)){continue;}
if( !is_dir($newpath) ){
if(!mkdir($newpath)){
$result = false;
}
@chmod($newpath,0777);
}
}
if(!$result){
return $path;
}
}
//参数中的$fileInput是表单中上传文件输入框input的名
function Upload($fileInput,$filename = ”,$smallname = ”)
{
$upfile = $_FILES[$fileInput][‘name’];
$uperror = $_FILES[$fileInput][‘error’];
if (!$this->debug_error($uperror))
return false;
$this->exname = strrchr($upfile,”.”);//得到上传文件夹的扩展名
if(!in_array($this->exname,$this->fileFormat)){//判断取得扩展名是否在要求的扩展名内
$this->errormsg = “此扩展名的文件禁止上传,请与管理员联系!”;
return false;
}
$filesize = $_FILES[$fileInput][‘size’];//判断上传的图片大小
if($this->maxSize)
{
if($filesize > $this->maxSize)
{
$this->errormsg = “所要上传的图片太大!”;
return false;
}
}
if(!$filename)
{
$this->makedir();
}
else
{
$this->picturename = $this->savePath.’/’.$filename;
$basename = basename($filename);//上传文件名
$smallname = $smallname?$smallname:$filename;
$this->smallpicturename = $this->savePath.’/small/’.$smallname;
}
$upfiletmp=$_FILES[$fileInput][‘tmp_name’];//得到临时上传的文件
if (!$this->debug_error($uperror))//判断临时文件是否存在
return false;
$goodupfile=@move_uploaded_file($upfiletmp,$this->picturename);
if (!$goodupfile){
$this->errormsg = “上传图片失败”.$this->picturename;
return false;
}else{
if($this->thumb){
$this->makepath($this->savePath.’/small’);
$this->createsmall($fileInput);//生成缩略图的部分!
return true;
}
else
{
return true;
}
}
}
function createsmall($fileInput)
{
if($_FILES[$fileInput][‘type’] == “image/pjpeg”){
$im = imagecreatefromjpeg($this->picturename);
}elseif($_FILES[$fileInput][‘type’] == “image/x-png”){
$im = imagecreatefrompng($this->picturename);
}elseif($_FILES[$fileInput][‘type’] == “image/gif”){
$im = imagecreatefromgif($this->picturename);
}
if($im){
if(file_exists($this->smallpicturename)){
unlink($this->smallpicturename);
}
$this->ResizeImage($im,$this->thumbWidth,$this->thumbHeight,$this->smallpicturename);
imageDestroy($im);
}
}
//$im —- 代表原图的图像标识符
//$maxwidth生成图片的宽度
//$maxheitht生成图片的高度
//$name 大图的名称(完整路径名)
function ResizeImage($im,$maxwidth,$maxheight,$name){
//imagesx() 返回 image 所代表的图像的宽度。
//imagesy() 返回 image 所代表的图像的高度。
$width = imagesx($im); //大图的宽度
$height = imagesy($im); //大图的高度
//$maxwidth生成图片的宽度
//$maxheitht生成图片的高度

if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
//缩略图固定大小,不按原来图片的比例
$ratio_big=$width/$height;
$ratio_small=$maxwidth/$maxheight;
if($ratio_big<$ratio_small)
$width=$width*$ratio_big;
else
$height=$height/$ratio_small;
$newwidth=$maxwidth;
$newheight=$maxheight;
/*
缩略图是按原来图片的比例
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
*/
if(function_exists(“imagecopyresampled”)){
//返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。
$newim = imagecreatetruecolor($newwidth, $newheight);
//将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。如果成功则返回 TRUE,失败则返回 FALSE。
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
//新建一个指定大小的黑白图片
$newim = imagecreate($newwidth, $newheight);
//例imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//把$im图片高$height宽$width按比例拷贝到$newim中大小为宽$newwidth高$newheight
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
//创建一个 JPEG 图像
ImageJpeg ($newim,$name);
ImageDestroy ($newim);
}else{
//原图片比缩略图小则直接生成一个JPEG的图片
ImageJpeg($im,$name);
}
}
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注