php支持生成缩略图文件上传代码

  1. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=gb2312" />
  5. <title>无标题文档</title>
  6. </head>
  7. <body>
  8. <?php
  9. class upfileclass {
  10. var $upfile, $upfile_name, $upfile_size;
  11. # $upfile 临时文件名 $_files['tmp_name'] ,$upfile_name 文件名 $_files['name'] ,$upfile_size 文件大小$_files['size'];
  12. var $new_upfile_name; # 上传后的文件名称 ;
  13. var $fleth, $fileextent; # 文件扩展名(类型) ;
  14. var $f1, $f2, $f3; # 文件保存路径(多级) upfiles/2008-01/08/;
  15. var $filename; # 文件(带路径) ;
  16. var $filepath; #相对路径用来删除文件;
  17. var $maxsize, $file_type; # 允许上传文件的大小 允许上传文件的类型 ;
  18. var $buildfile,$newfile,$file_width,$file_height,$rate;
  19. function upfileclass($upfile,$upfile_name,$upfile_size){
  20. $this->upfile = $upfile;
  21. $this->upfile_name = $upfile_name;
  22. $this->upfile_size = $upfile_size;
  23. $this->new_upfile_name = $this->createnewfilename($this->upfile_name);
  24. $this->f1 = "upfiles";
  25. $this->f2 = $this->f1."/".date('y')."-".date('m');
  26. $this->f3 = $this->f2."/".date('d');
  27. $this->filename = $this->f3 . "/" . $this->new_upfile_name;
  28. $this->maxsize = 500*1024; # 文件大小 500kb
  29. $this->file_type = "gif/jpg/jpeg/png/bmp"; # 允许上传的文件类型
  30. }
  31. # 创建新文件名 (原文件名)
  32. function createnewfilename($file_name){
  33. $this->fleth = explode(".",$file_name);
  34. $this->fileextent = $this->fleth[(int)count($this->fleth)-1]; # 获取文件后缀;
  35. $tmps教程tr = date('ymd').rand(0,time()) . "." .$this->fileextent; # 创建新文件名;
  36. return $tmpstr;
  37. }
  38. # 检测文件类型是否正确
  39. function chk_fileextent(){
  40. $iwtrue = 0;
  41. $fle = explode("/",$this->file_type);
  42. for($i=0; $i < count($fle); $i++){
  43. if($this->fileextent == $fle[$i]){
  44. $iwtrue = (int) $iwtrue + 1;
  45. }
  46. }
  47. if( $iwtrue == 0 ){
  48. $this->msg("文件不符合 ".$this->file_type." 格式!");
  49. }
  50. }
  51. # 提示错误信息并终止操作
  52. function msg($error){
  53. echo "<script language="网页特效"> ";
  54. echo " alert('".$error."'); ";
  55. echo " window.history.back(); ";
  56. echo "</script> ";
  57. die();
  58. }
  59. # 保存文件
  60. function savefile(){
  61. $this->chk_fileextent();
  62. $this->chk_filesize();
  63. $this->createfolder( "../".$this->f1 );
  64. $this->createfolder( "../".$this->f2 );
  65. $this->createfolder( "../".$this->f3 );
  66. return $this->chk_savefile();
  67. }
  68. # 检测上传结果是否成功
  69. function chk_savefile(){
  70. $copymsg = copy($this->upfile,"../".$this->filename);
  71. if( $copymsg ){
  72. return $this->filename;
  73. }
  74. else{
  75. $this->msg("文件上传失败! 请重新上传! ");
  76. }
  77. }
  78. # 创建文件夹
  79. function createfolder($foldername){
  80. if( !is_dir($foldername) ){
  81. mkdir($foldername,0777);
  82. }
  83. }
  84. # 检测文件大小
  85. function chk_filesize(){
  86. if( $this->upfile_size > $this->maxsize ){
  87. $this->msg("目标文件不能大于". $this->maxsize/1024 ." kb");
  88. }
  89. }
  90. # 删除文件($filepath 文件相对路径)
  91. function deletefile($filepath){
  92. if( !is_file($filepath) ){
  93. return false;
  94. }
  95. else{
  96. $ending = @unlink($filepath);
  97. return $ending;
  98. }
  99. }
  100. /*
  101. 函数:生成缩略图
  102. makebuild("/www.phpfensi.com/a.jpg","news/b.jpg","100");
  103. 参数:
  104. echo $buildfile; 原图 带路径
  105. echo $newfile; 生成的缩略图 带路径
  106. echo $file_width; 缩略图宽度值
  107. echo $file_height; 缩略图高度值 (默认为宽度的比例值)
  108. echo $rate; 缩略图象品质;
  109. */
  110. function makebuild($buildfile,$newfile,$file_width,$file_height=0,$rate=100) {
  111. if(!is_file($buildfile)){
  112. $this->msg("文件 ".$buildfile." 不是一个有效的图形文件! 系统无法生成该文件的缩略图!");
  113. return false;
  114. }
  115. $data = getimagesize($buildfile);
  116. switch($data[2]){
  117. case 1:
  118. $im = @imagecreatefromgif($buildfile);
  119. break;
  120. case 2:
  121. $im = @imagecreatefromjpeg($buildfile);
  122. break;
  123. case 3:
  124. $im = @imagecreatefrompng($buildfile);
  125. break;
  126. }
  127. if(!$im){
  128. return false;
  129. }
  130. else{
  131. $srcw = imagesx($im); # 取得原图宽度;
  132. $srch = imagesy($im); # 取得原图高度;
  133. $dstx = 0;
  134. $dsty = 0;
  135. if($file_height==0){
  136. $file_height = $file_width/$srcw*$srch;
  137. }
  138. if ($srcw*$file_height>$srch*$file_width){
  139. $ffile_height = round($srch*$file_width/$srcw);
  140. $dsty = floor(($file_height-$ffile_height)/2);
  141. $ffile_width = $file_width;
  142. }
  143. else {
  144. $ffile_width = round($srcw*$file_height/$srch);
  145. $dstx = floor(($file_width-$ffile_width)/2);
  146. $ffile_height = $file_height;
  147. }
  148. $ni = imagecreatetruecolor($file_width,$file_height);
  149. $dstx = ($dstx<0)?0:$dstx;
  150. $dsty = ($dstx<0)?0:$dsty;
  151. $dstx = ($dstx>($file_width/2))?floor($file_width/2):$dstx;
  152. $dsty = ($dsty>($file_height/2))?floor($file_height/s):$dsty;
  153. imagecopyresized($ni,$im,$dstx,$dsty,0,0,$ffile_width,$ffile_height,$srcw,$srch);
  154. imagejpeg($ni,$newfile,$rate); # 生成缩略图;
  155. imagedestroy($im); # imagedestroy(resource) 释放image关联的内存
  156. }
  157. }
  158. }
  159. ?>
  160. //开源代码phpfensi.com
  161. </body>
  162. </html>