《单文件上传PHP类,可生成缩略图和水印》要点:
本文介绍了单文件上传PHP类,可生成缩略图和水印,希望对您有用。如果有疑问,可以联系我们。
维易PHP培训学院每天发布《单文件上传PHP类,可生成缩略图和水印》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。
<必修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);
}
}
}
必修>