php教程支持生成縮略圖文件上傳代碼
?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
</head>
<body>
<?php
class upfileclass {
var $upfile, $upfile_name, $upfile_size;
# $upfile 臨時文件名 $_files['tmp_name'] ,$upfile_name 文件名 $_files['name'] ,$upfile_size 文件大小$_files['size'];
var $new_upfile_name; # 上傳後的文件名稱 ;
var $fleth, $fileextent; # 文件擴展名(類型) ;
var $f1, $f2, $f3; # 文件保存路徑(多級) upfiles/2008-01/08/;
var $filename; # 文件(帶路徑) ;
var $filepath; #相對路徑用來刪除文件;
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/bmp"; # 允許上傳的文件類型
}
# 創建新文件名 (原文件名)
function createnewfilename($file_name){
$this->fleth = explode(".",$file_name);
$this->fileextent = $this->fleth[(int)count($this->fleth)-1]; # 獲取文件後綴;
$tmps教程tr = date('ymd').rand(0,time()) . "." .$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="網頁特效">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("文件上傳失敗! nn請重新上傳! ");
}
}
# 創建文件夾
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("/www.bKjia.c0m/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." 不是一個有效的圖形文件!nn系統無法生成該文件的縮略圖!");
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關聯的內存
}
}
}
?>
</body>
</html>