单文件上传PHP类,可生成缩略图和水印-php是什么文件

单文件上传PHP类,可生成缩略图和水印

<?php

/*

文件上传时需要注意的具体事项有:

1.文件扩展名

2.文件尺寸

3.上传的位置

4.上传的子目录

5.上传的文件名字

6.客户所上传的文件数据来源

*/

class UpLoadOne

{

private $FileData;//定义客户所上传的文件来源

private $AffirmExtension;//定义我们所确认的有效扩展名有哪些

private $UpFileSize;//定义我们所上传文件的大小对其约束

private $UpSeat;//定义上传文件的具体位置

private $UpSubDir;//定义上传文件的子目录

private $ThumbnailState='Off';//定义是否生成缩略图开关On开启Off关闭

private $ThumbnailWidth=array();//定义缩略图的宽度

private $ThumbnailHeight=array();//定义缩略图的高度

private $WatermarkState='Off';//定义是否添加水印,针对原图进行添加

private $WatermarkPicPath='';//定义图片水印的文件路径

private $FileExtension;//定义当前所上传的文件的扩展名

//$FileData,$AffirmExtension,$UpFileSize,$UpSeat,$UpSubDir,$ThumbnailState='Off',$ThumbnailWidth=array(),$ThumbnailHeight=array(),$WatermarkState='Off',$WatermarkPicPath=''

public function __construct($Param=array())

{

$this->FileData=isset($Param['FileData'])?$Param['FileData']:'';

$this->AffirmExtension=isset($Param['AffirmExtension'])?$Param['AffirmExtension']:'';

$this->UpFileSize=isset($Param['UpFileSize'])?$Param['UpFileSize']:'';

$this->UpSeat=isset($Param['UpSeat'])?$Param['UpSeat']:'';

$this->UpSubDir=isset($Param['UpSubDir'])?$Param['UpSubDir']:'';

$this->ThumbnailState=isset($Param['ThumbnailState'])?$Param['ThumbnailState']:'Off';

$this->ThumbnailWidth=isset($Param['ThumbnailWidth'])?$Param['ThumbnailWidth']:array();

$this->ThumbnailHeight=isset($Param['ThumbnailHeight'])?$Param['ThumbnailHeight']:array();

$this->WatermarkState=isset($Param['WatermarkState'])?$Param['WatermarkState']:'Off';

$this->WatermarkPicPath=isset($Param['WatermarkPicPath'])?$Param['WatermarkPicPath']:'';

}

public function FileUpLoad()

{

//验证当前是否有文件上传

if(!$this->CheckFileIsNull())

{

return 1;

}

//验证当前客户所上传的图片扩展名是否有效

if(!$this->CheckExtensionIsValid())

{

return 2;

}

//验证当前客户所上传的文件大小是否合法

if(!$this->CheckFileSizeIsValid())

{

return 3;

}

//文件上传保存

$FileSaveResult=$this->MoveFileSave();

if(empty($FileSaveResult))

{

return 4;

}else{

return $FileSaveResult;

}

}

//验证当前客户所上传的文件是否为空

private function CheckFileIsNull()

{

if($this->FileData['error']==4)

{

return false;

}

$this->GetCurrentFileExtension();//获取当前文件扩展名

$this->CheckDirIsValid();//验证当前的目录是否是合法有效的

$this->CheckSubDirIsValid();//验证当前目录下的子目录是否是合法有效的

return true;

}

//获取当前文件的扩展名

private function GetCurrentFileExtension()

{

$FileName=$this->FileData['name'];

$FileArr=explode('.',$FileName);

$this->FileExtension=end($FileArr);

return true;

}

//验证当前客户所上传的图片扩展名是否有效

private function CheckExtensionIsValid()

{

if(in_array($this->FileExtension,$this->AffirmExtension))

{

return true;

}

return false;

}

//验证当前客户所上传的文件大小是否合法

private function CheckFileSizeIsValid()

{

if($this->FileData['size'] > $this->UpFileSize)

{

return false;

}

return true;

}

//验证当前的目录是否是合法有效的

private function CheckDirIsValid()

{

if(is_dir($this->UpSeat))

{

return true;

}

@mkdir($this->UpSeat);//创建根目录

chmod($this->UpSeat,0777);//设置当前所创建的文件夹权限为可读可写

}

//验证当前目录下的子目录是否是合法有效的

private function CheckSubDirIsValid()

{

if(!empty($this->UpSubDir))

{

if(is_dir($this->UpSeat.$this->UpSubDir))

{

return true;

}

@mkdir($this->UpSeat.$this->UpSubDir);//创建根目录

chmod($this->UpSeat.$this->UpSubDir,0777);//设置当前所创建的文件夹权限为可读可写

}

return '';

}

//依据当前文件夹再创建出年月日的子文件夹

private function CheckYMDDirIsValid()

{

$Path=$this->UpSeat.$this->UpSubDir.date('Ymd');

if(is_dir($Path))

{

return $Path;

}

@mkdir($Path);//创建根目录

chmod($Path,0777);//设置当前所创建的文件夹权限为可读可写

return $Path;

}

//移动客户所上传的文件

private function MoveFileSave()

{

$Back=array();

if(is_uploaded_file($this->FileData['tmp_name']))

{

//依据当前文件夹再创建出年月日的子文件夹

$Dir=$this->CheckYMDDirIsValid();

$ThumbnailPicStr=$Dir.'/'.date('YmdHis').'_'.mt_rand(100,999);

$Path=$ThumbnailPicStr.'.'.$this->FileExtension;

if(move_uploaded_file($this->FileData['tmp_name'],$Path))

{

$Thumbnail='';

if($this->ThumbnailState=='On')

{

$Thumbnail=$this->CreateThumbnail($Path,$ThumbnailPicStr);//为当前所上传的图片生成缩略图

}

if($this->WatermarkState=='On')

{

$this->AddWatermark($Path);

}

$Back['Main']=$Path;

$Back['Thumbnail']=$Thumbnail;

return $Back;

}

return false;

}

return false;

}

//为当前所上传的图片生成缩略图

private function CreateThumbnail($Path,$ThumbnailPicStr)

{

$Back=array();

$PicInfo=$this->GetPicInfo($Path);

$imagecreatefromName='imagecreatefrom'.$PicInfo['PicType'];//imagecreatefromjpeg;

$ImgOld=$imagecreatefromName($Path);

if(!empty($this->ThumbnailWidth) && !empty($this->ThumbnailHeight))

{

foreach ($this->ThumbnailWidth as $key => $value) {

$Height=$this->ThumbnailHeight[$key];

$ImgNew=imagecreatetruecolor($value,$Height);

$Bg=imagecolorallocate($ImgNew,255,255,255);

imagefill($ImgNew,0,0,$Bg);

//计算缩放比例

//$Width、$Height 原始图片宽度和高度

//$this->Width、$this->Height 缩略图宽度和高度

//$X、$Y 当图片缩放后小于缩略图尺寸时,以1/2的比例左右两边分别补白

//$NewImg 新图片资源

//$Img 原始图片资源

if($PicInfo['Width'] < $value && $PicInfo['Height'] < $Height){

$Scale = 1;

} else {

$Scale = min($value/$PicInfo['Width'], $Height/$PicInfo['Height']);

}

//设置缩略图的坐标及宽度和高度

$NewWidth = $PicInfo['Width'] * $Scale;

$NewHeight = $PicInfo['Height'] * $Scale;

$X = ($value - $NewWidth)/2;

$Y = ($Height - $NewHeight)/2;

//imagecopyresampled($NewImg,$Img,$X,$Y,0,0,$NewWidth,$NewHeight,$Width,$Height);

//--------------------------------------------------------------

imagecopyresampled($ImgNew,$ImgOld, $X, $Y, 0, 0, $NewWidth, $NewHeight, $PicInfo['Width'], $PicInfo['Height']);

//header('Content-Type:'.$PicInfo['Mime']);

$imageName='image'.$PicInfo['PicType'];

$SaveName=$ThumbnailPicStr.'.'.$value.'-'.$Height.'.'.$this->FileExtension;

$imageName($ImgNew,$SaveName);

imagedestroy($ImgNew);

$Back[]=$SaveName;

}

imagedestroy($ImgOld);

}

return $Back;

}

//获取图片基本信息

private function GetPicInfo($Path)

{

$PicInfo=getimagesize($Path);

list($Width,$Height,$Type)=$PicInfo;

$Mime=$PicInfo['mime'];

$PicType=image_type_to_extension($Type,false);

$Back=array();

$Back['Width']=$Width;

$Back['Height']=$Height;

$Back['Mime']=$Mime;

$Back['PicType']=$PicType;

return $Back;

}

//为当前所上传的图片添加水印

private function AddWatermark($Path)

{

if(!empty($this->WatermarkPicPath)&&is_file($this->WatermarkPicPath))

{

$PicInfo=$this->GetPicInfo($Path);

$imagecreatefromName='imagecreatefrom'.$PicInfo['PicType'];//imagecreatefromjpeg;

$ImgNew=$imagecreatefromName($Path);

$LogoPicInfo=$this->GetPicInfo($this->WatermarkPicPath);

$imagecreatefromLogoName='imagecreatefrom'.$LogoPicInfo['PicType'];//imagecreatefromjpeg;

$ImgLogo=$imagecreatefromLogoName($this->WatermarkPicPath);

imagecopy($ImgNew,$ImgLogo,$PicInfo['Width']-$LogoPicInfo['Width'],$PicInfo['Height']-$LogoPicInfo['Height'],0,0,$LogoPicInfo['Width'],$LogoPicInfo['Height']);

$imageName='image'.$PicInfo['PicType'];

$imageName($ImgNew,$Path);

imagedestroy($ImgNew);

imagedestroy($ImgLogo);

}

}

}

?>

推荐阅读