您好,欢迎访问一九零五行业门户网

php图片文件上传类(可自动生成缩略图)

分享一个php图片上传类,且可以自动生成缩略图,有需要的朋友,可以作个参考,希望可以对您有用。php实现的图片文件上传类,代码:
savefile(); # 保存方法 并返回保存路径附带文件名;@ echo makebuild($buildfile,$newfile,$file_width);生成指定文件的缩略图;$myupload->makebuild(images/a.jpg,news/b.jpg,100);*/class upfileclass{var $upfile, $upfile_name, $upfile_size;var $new_upfile_name; # 上传后的文件名称 ; var $fleth, $fileextent; # 文件扩展名(类型) ; var $f1, $f2, $f3; # 文件保存路径(多级) upfiles/2008-01/08/;var $filename; # 文件(带路径) ;var $maxsize, $file_type; # 允许上传文件的大小 允许上传文件的类型 ;var $buildfile,$newfile,$file_width,$file_height,$rate;function upfileclass($upfile,$upfile_name,$upfile_size){ $this->upfile = $upfile; $this->upfile_name = $upfile_name; $this->upfile_size = $upfile_size; $this->new_upfile_name = $this->createnewfilename($this->upfile_name); $this->f1 = upfiles; $this->f2 = $this->f1./.date('y').-.date('m');$this->f3 = $this->f2./.date('d'); $this->filename = $this->f3 . / . $this->new_upfile_name; $this->maxsize = 500*1024; # 文件大小 500kb $this->file_type = gif/jpg/jpeg/png; # 允许上传的文件类型}# 创建新文件名 (原文件名)function createnewfilename($file_name){ $this->fleth = explode(.,$file_name); $this->fileextent = $this->fleth[(int)count($this->fleth)-1]; # 获取文件后缀; $tmpstr = date('ymdhis') . . .$this->fileextent; # 创建新文件名; return $tmpstr;}# 检测文件类型是否正确function chk_fileextent(){ $iwtrue = 0; $fle = explode(/,$this->file_type); for($i=0; $i fileextent == $fle[$i] ) { $iwtrue = (int) $iwtrue + 1; } } if( $iwtrue == 0 ){ $this->msg(文件不符合 .$this->file_type. 格式!); }}# 提示错误信息并终止操作function msg($error){ echo /n; die();}# 保存文件function savefile(){ $this->chk_fileextent(); $this->chk_filesize(); $this->createfolder( ../.$this->f1 ); $this->createfolder( ../.$this->f2 ); $this->createfolder( ../.$this->f3 ); return $this->chk_savefile();}# 检测上传结果是否成功function chk_savefile(){ $copymsg = copy($this->upfile,../.$this->filename); if( $copymsg ){ return $this->filename; } else{ $this->msg(文件上传失败! /n/n请重新上传! ); }}# 创建文件夹function createfolder($foldername){ if( !is_dir($foldername) ){ mkdir($foldername,0777); }}# 检测文件大小function chk_filesize(){ if( $this->upfile_size > $this->maxsize ){ $this->msg(目标文件不能大于. $this->maxsize/1024 . kb); }}# 删除文件($filepath 文件相对路径)function deletefile($filepath){ if( !is_file($filepath) ){ return false; } else{ $ending = @unlink($filepath); return $ending; }}/* 函数:生成缩略图 makebuild(images/a.jpg,news/b.jpg,100); 参数: echo $buildfile; 原图 带路径 echo $newfile; 生成的缩略图 带路径 echo $file_width; 缩略图宽度值 echo $file_height; 缩略图高度值 (默认为宽度的比例值) echo $rate; 缩略图象品质;*/function makebuild($buildfile,$newfile,$file_width,$file_height=0,$rate=100) { if(!is_file($buildfile)){ $this->msg(文件 .$buildfile. 不是一个有效的图形文件!/n/n系统无法生成该文件的缩略图!); return false; } $data = getimagesize($buildfile); switch($data[2]){ case 1: $im = @imagecreatefromgif($buildfile); break; case 2: $im = @imagecreatefromjpeg($buildfile); break; case 3: $im = @imagecreatefrompng($buildfile); break; } if(!$im){ return false; } else{ $srcw=imagesx($im); # 取得原图宽度; $srch=imagesy($im); # 取得原图高度; $dstx=0; $dsty=0; if($file_height==0){ $file_height = $file_width/$srcw*$srch; } if ($srcw*$file_height>$srch*$file_width){ $ffile_height = round($srch*$file_width/$srcw); $dsty = floor(($file_height-$ffile_height)/2); $ffile_width = $file_width; } else { $ffile_width = round($srcw*$file_height/$srch); $dstx = floor(($file_width-$ffile_width)/2); $ffile_height = $file_height; } $ni = imagecreatetruecolor($file_width,$file_height); $dstx = ($dstx($file_height/2))?floor($file_height/s):$dsty; imagecopyresized($ni,$im,$dstx,$dsty,0,0,$ffile_width,$ffile_height,$srcw,$srch); imagejpeg($ni,$newfile,$rate); # 生成缩略图; imagedestroy($im); # imagedestroy(resource) 释放image关联的内存 }} }?>
其它类似信息

推荐信息