class upload_class{
private $ptname; //Upload form name;
private $udname; //Whether to create a subdirectory based on month (0 means no, others are true);
private $ufname ; //Whether to create a file name based on time (0 means no, others are true);
private $ultype; //Upload file type;
private $ulsize; //Upload file size ;
private $ulname; //Output file name;
private $ulpath; //Output file path;
private $wm; //Watermark appended (0 means no addition, Others are added);
private $wmtype; //Watermark type (0 is text, others are pictures);
private $wmpic; //Watermark picture;
private $wmpicquality; //Picture quality;
private $wmpictrans; //Picture transparent;
private $wmstr; //Watermark characters;
private $wmstrsize; //Character size;
private $wmstrfont; / /Character font;
private $wmstrcolor; //Character color;
private $wmpos; //Watermark position (1 is top left, 2 is top center, 3 is top right...);
function __construct($ptname='upfile',$udname=1,$ufname=1,$ultype=array('image/jpg','image/jpeg','image/png','image/pjpeg',' image/gif','image/bmp','image/x-png'),$wm=1,$wmtype=1,$wmpic='images/wm.gif',$ulsize=2097152,$ulpath=' images/temp/',$wmpictrans=20,$wmpicquality=80,$wmstr='DONLINE',$wmstrsize=5,$wmstrfont='./font/cour.ttf',$wmstrcolor='#ff0000',$ wmpos=9){
$this->ptname=$_FILES[$ptname];
$this->udname=$udname;
$this->ufname=$ufname;
$this->ultype=$ultype;
$this->ulsize=$ulsize;
$this->ulpath=$ulpath;
$this->wm=$wm;
$this->wmtype=$wmtype;
$this->wmpic=$wmpic;
$this->wmpicquality=$wmpicquality;
$this->wmpictrans=$ wmpictrans;
$this->wmstr=$wmstr;
$this->wmstrsize=$wmstrsize;
$this->wmstrfont=$wmstrfont;
$this->wmstrcolor =$wmstrcolor;
$this->wmpos=$wmpos;
}
function uploadfun(){
if($_SERVER['REQUEST_METHOD']=='POST'){
if(!is_uploaded_file($this->ptname['tmp_name']))$this->errorfun('Upload failed! ');
if(!in_array($this->ptname['type'],$this->ultype))$this->errorfun('Unsupported file type!');
if($this->ulsize<$this->ptname['size'])$this->errorfun('The file is too big!');
if($this->udname){ date_default_timezone_set('UTC');$this->ulpath=$this->ulpath.'month_'.date('Ym').'/';}
else{$this->ulpath=$ this->ulpath;}
$this->createfun($this->ulpath);
if($this->ufname){$t=pathinfo($this->ptname[ 'name']);$this->ulname=$this->ulpath.time().'.'.$t['extension'];}
else{$this->ulname=$ this->ulpath.$this->ptname['name'];}
if(file_exists($this->ulname))$this->errorfun('The file already exists!');
if(!move_uploaded_file($this->ptname['tmp_name'],$this->ulname))$this->errorfun('Moving file error!');
$this-> ;wmfun();
$this->errorfun('Upload successful!');
}
}
function createfun($d){
if(!file_exists($d )){$this->createfun(dirname($d));mkdir($d);}
}
function wmfun(){
if($this->wm){
if(file_exists($this->ulname)){
$groundimg=getimagesize($this->ulname);
$ow=$groundimg[0];
$oh=$ groundimg[1];
switch($groundimg[2]){
case 1:$g=imagecreatefromgif($this->ulname);break;
case 2:$g=imagecreatefromjpeg($ this->ulname);break;
case 3:$g=imagecreatefrompng($this->ulname);break;
case 4:$g=imagecreatefromwbmp($this->ulname);break ;
default:$this->errorfun('Unsupported background image type! ');
}
}
else{$this->errorfun('The background image does not exist!');}
if(file_exists($this->wmpic)){
$wmimg=getimagesize($this->wmpic);
$ww=$wmimg[0];
$wh=$wmimg[1];
switch($wmimg[2]){
case 1:$w=imagecreatefromgif($this->wmpic);break;
case 2:$w=imagecreatefromjpeg($this->wmpic);break;
case 3:$w=imagecreatefrompng($this->wmpic);break;
case 4:$w=imagecreatefromwbmp($this->wmpic);break;
default:$this->errorfun('不支持的水印图片类型!');
}
}
else{$this->errorfun('水印图片不存在!');}
switch($this->wmtype){
case 0:$tp=imagettfbbox(ceil($this->wmstrsize*2.5),0,$this->wmstrfont,$this->wmstr);$ww=$tp[2]-$tp[6];$wh=$tp[3]-$tp[7];unset($tp);break;
case 1:$ww=$ww;$wh=$wh;break;
default:$ww=$ww;$wh=$wh;break;
}
if($ow<$ww || $oh<$wh)$this->errorfun('背景图片太小!无法生成水印!');
switch($this->wmpos){
case 0:$x=rand(0,($ow-$ww));$y=rand(0,($oh-$wh));break;//随机
case 1:$x=0;$y=0;break;//1为顶端居左
case 2:$x=($ow-$ww)/2;$y=0;break;//2为顶端居中
case 3:$x=$ow-$ww;$y=0;break;//3为顶端居右
case 4:$x=0;$y=($oh-$wh)/2;break;//4为中部居左
case 5:$x=($ow-$ww)/2;$y=($oh-$wh)/2;break;//5为中部居中
case 6:$x=$ow-$ww;$y=($oh-$wh)/2;break;//6为中部居右
case 7:$x=0;$y=$oh-$wh;break;//7为底端居左
case 8:$x=($ow-$ww)/2;$y=$oh-$wh;break;//8为底端居中
case 9:$x=$ow-$ww;$y=$oh-$wh;break;//9为底端居右
default:$x=rand(0,($ow-$ww));$y=rand(0,($oh-$wh));break;//随机
}
imagealphablending($g, true);
switch($this->wmtype){
case 0:
if($this->wmstrcolor){$R=hexdec(substr($this->wmstrcolor,1,2));$G=hexdec(substr($this->wmstrcolor,3,2));$B=hexdec(substr($this->wmstrcolor,5));}
else{$this->errorfun('水印文字颜色不存在!');}
imagestring($g,$this->wmstrfont,$x,$y,$this->wmstr,imagecolorallocate($g,$R,$G,$B));break;
case 1;imagecopymerge($g,$w,$x,$y,0,0,$ww,$wh,$this->wmpictrans);break;
default:imagecopymerge($g,$w,$x,$y,0,0,$ww,$wh,$this->wmpictrans);break;
}
@unlink($this->ulname);
switch($groundimg[2]){
case 1:imagegif($g,$this->ulname);break;
case 2:imagejpeg($g,$this->ulname,$this->wmpicquality);break;
case 3:imagepng($g,$this->ulname);break;
case 4:imagewbmp($g,$this->ulname);break;
default:$this->errorfun('生成图片失败!');
}
if(isset($wmimg))unset($wmimg);
if(isset($w))imagedestroy($w);
unset($groundimg);
imagedestroy($g);
}
}
function errorfun($e='未知错误!'){
$msg='';
echo $msg;
exit;
}
}
?>