您的位置:首页 > 编程语言 > PHP开发

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

2008-11-03 14:28 946 查看
<?php
/**
作 者:冻结回忆 (linghunts@163.com);
功 能:文件上传类 支持文件夹自动分组保存(2008-01/09);
时 间:2007-10-17;
创建类:参数(文件域,文件原名,文件大小);
$myupload = new upfileClass($upfile,$upfile_name,$upfile_size);
$myupload->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 < count($fle); $i++){
if( $this->fileExtent == $fle[$i] )
{
$iwTrue = (int) $iwTrue + 1;
}
}
if( $iwTrue == 0 ){
$this->msg("文件不符合 ".$this->File_type." 格式!");
}
}

# 提示错误信息并终止操作
function msg($Error)
{
echo "<script language=/"javascript/">/n";
echo " alert('".$Error."');/n";
echo " window.history.back();/n";
echo "</script>/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<0)?0:$dstX;
$dstY = ($dstX<0)?0:$dstY;
$dstX = ($dstX>($File_width/2))?floor($File_width/2):$dstX;
$dstY = ($dstY>($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关联的内存
}
}

}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐