php支持生成缩略图文件上传代码
<!doctype html> <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="网页特效"> "; echo " alert('" . $error . "'); "; echo " window.history.back(); "; echo "</script> "; 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("文件上传失败! 请重新上传! "); } } // 创建文件夹 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.phprm.com/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 . " 不是一个有效的图形文件! 系统无法生成该文件的缩略图!"); 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>
本文地址:http://www.phprm.com/tuxiang/fs4482.html
转载随意,但请附上文章地址:-)