当前位置:Gxlcms > PHP教程 > php图片文件上传类(可自动生成缩略图)

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

时间:2021-07-01 10:21:17 帮助过:5人阅读

分享一个php图片上传类,且可以自动生成缩略图,有需要的朋友,可以作个参考,希望可以对您有用。

php实现的图片文件上传类,代码:

  1. <!--?php
  2. /**
  3. 功 能:文件上传类 支持文件夹自动分组保存;
  4. 创建类:参数(文件域,文件原名,文件大小);
  5. $myupload = new upfileClass($upfile,$upfile_name,$upfile_size);
  6. $myupload--->savefile(); # 保存方法 并返回保存路径附带文件名;
  7. @ echo MakeBuild($BuildFile,$newFile,$File_width);
  8. 生成指定文件的缩略图;
  9. $myupload->MakeBuild("images/a.jpg","news/b.jpg","100");
  10. */
  11. class upfileClass
  12. {
  13. var $upfile, $upfile_name, $upfile_size;
  14. var $new_upfile_name; # 上传后的文件名称 ;
  15. var $fleth, $fileExtent; # 文件扩展名(类型) ;
  16. var $f1, $f2, $f3; # 文件保存路径(多级) upfiles/2008-01/08/;
  17. var $filename; # 文件(带路径) ;
  18. var $maxSize, $File_type; # 允许上传文件的大小 允许上传文件的类型 ;
  19. var $BuildFile,$newFile,$File_width,$File_height,$rate;
  20. function upfileClass($upfile,$upfile_name,$upfile_size)
  21. {
  22. $this->upfile = $upfile;
  23. $this->upfile_name = $upfile_name;
  24. $this->upfile_size = $upfile_size;
  25. $this->new_upfile_name = $this->CreateNewFilename($this->upfile_name);
  26. $this->f1 = "upfiles";
  27. $this->f2 = $this->f1."/".date('Y')."-".date('m');
  28. $this->f3 = $this->f2."/".date('d');
  29. $this->filename = $this->f3 . "/" . $this->new_upfile_name;
  30. $this->maxSize = 500*1024; # 文件大小 500KB
  31. $this->File_type = "gif/jpg/jpeg/png"; # 允许上传的文件类型
  32. }
  33. # 创建新文件名 (原文件名)
  34. function CreateNewFilename($file_name)
  35. {
  36. $this->fleth = explode(".",$file_name);
  37. $this->fileExtent = $this->fleth[(int)count($this->fleth)-1]; # 获取文件后缀;
  38. $tmpstr = date('YmdHis') . "." .$this->fileExtent; # 创建新文件名;
  39. return $tmpstr;
  40. }
  41. # 检测文件类型是否正确
  42. function chk_fileExtent()
  43. {
  44. $iwTrue = 0;
  45. $fle = explode("/",$this->File_type);
  46. for($i=0; $i < count($fle); $i++){
  47. if( $this->fileExtent == $fle[$i] )
  48. {
  49. $iwTrue = (int) $iwTrue + 1;
  50. }
  51. }
  52. if( $iwTrue == 0 ){
  53. $this->msg("文件不符合 ".$this->File_type." 格式!");
  54. }
  55. }
  56. # 提示错误信息并终止操作
  57. function msg($Error)
  58. {
  59. echo "/n";
  60. die();
  61. }
  62. # 保存文件
  63. function savefile()
  64. {
  65. $this->chk_fileExtent();
  66. $this->chk_fileSize();
  67. $this->CreateFolder( "../".$this->f1 );
  68. $this->CreateFolder( "../".$this->f2 );
  69. $this->CreateFolder( "../".$this->f3 );
  70. return $this->chk_savefile();
  71. }
  72. # 检测上传结果是否成功
  73. function chk_savefile()
  74. {
  75. $copymsg = copy($this->upfile,"../".$this->filename);
  76. if( $copymsg ){
  77. return $this->filename;
  78. }
  79. else{
  80. $this->msg("文件上传失败! /n/n请重新上传! ");
  81. }
  82. }
  83. # 创建文件夹
  84. function CreateFolder($foldername)
  85. {
  86. if( !is_dir($foldername) ){
  87. mkdir($foldername,0777);
  88. }
  89. }
  90. # 检测文件大小
  91. function chk_fileSize()
  92. {
  93. if( $this->upfile_size > $this->maxSize ){
  94. $this->msg("目标文件不能大于". $this->maxSize/1024 ." KB");
  95. }
  96. }
  97. # 删除文件($filePath 文件相对路径)
  98. function Deletefile($filePath)
  99. {
  100. if( !is_file($filePath) ){
  101. return false;
  102. }
  103. else{
  104. $ending = @unlink($filePath);
  105. return $ending;
  106. }
  107. }
  108. /*
  109. 函数:生成缩略图
  110. MakeBuild("images/a.jpg","news/b.jpg","100");
  111. 参数:
  112. echo $BuildFile; 原图 带路径
  113. echo $newFile; 生成的缩略图 带路径
  114. echo $File_width; 缩略图宽度值
  115. echo $File_height; 缩略图高度值 (默认为宽度的比例值)
  116. echo $rate; 缩略图象品质;
  117. */
  118. function MakeBuild($BuildFile,$newFile,$File_width,$File_height=0,$rate=100)
  119. {
  120. if(!is_file($BuildFile)){
  121. $this->msg("文件 ".$BuildFile." 不是一个有效的图形文件!/n/n系统无法生成该文件的缩略图!");
  122. return false;
  123. }
  124. $data = GetImageSize($BuildFile);
  125. switch($data[2]){
  126. case 1:
  127. $im = @ImageCreateFromGIF($BuildFile);
  128. break;
  129. case 2:
  130. $im = @ImageCreateFromJPEG($BuildFile);
  131. break;
  132. case 3:
  133. $im = @ImageCreateFromPNG($BuildFile);
  134. break;
  135. }
  136. if(!$im){
  137. return false;
  138. }
  139. else{
  140. $srcW=ImageSX($im); # 取得原图宽度;
  141. $srcH=ImageSY($im); # 取得原图高度;
  142. $dstX=0;
  143. $dstY=0;
  144. if($File_height==0){
  145. $File_height = $File_width/$srcW*$srcH;
  146. }
  147. if ($srcW*$File_height>$srcH*$File_width){
  148. $fFile_height = round($srcH*$File_width/$srcW);
  149. $dstY = floor(($File_height-$fFile_height)/2);
  150. $fFile_width = $File_width;
  151. }
  152. else {
  153. $fFile_width = round($srcW*$File_height/$srcH);
  154. $dstX = floor(($File_width-$fFile_width)/2);
  155. $fFile_height = $File_height;
  156. }
  157. $ni = ImageCreateTrueColor($File_width,$File_height);
  158. $dstX = ($dstX<0)?0:$dstX;
  159. $dstY = ($dstX<0)?0:$dstY;
  160. $dstX = ($dstX>($File_width/2))?floor($File_width/2):$dstX;
  161. $dstY = ($dstY>($File_height/2))?floor($File_height/s):$dstY;
  162. ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fFile_width,$fFile_height,$srcW,$srcH);
  163. ImageJpeg($ni,$newFile,$rate); # 生成缩略图;
  164. imagedestroy($im); # imagedestroy(resource) 释放image关联的内存
  165. }
  166. }
  167. }
  168. ?>

人气教程排行