PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/website/longyusoft/meiyiju/include/upload.class.php

https://github.com/isold/longyu
PHP | 378 lines | 303 code | 54 blank | 21 comment | 53 complexity | 3769c78f2a2b6ce693f33f6ec731b526 MD5 | raw file
  1. <?php
  2. if (!defined('IN_PBBLOG'))
  3. {
  4. die('Access Denied');
  5. }
  6. class cls_upload {
  7. var $error='';
  8. function upload($upload,$type='img')
  9. {
  10. //检查文件上传情况
  11. if ($upload['error']==1)
  12. {
  13. $this->error='文件大小超出了服务器的空间大小';
  14. return false;
  15. }
  16. elseif ($upload['error']==2)
  17. {
  18. $this->error='要上传的文件大小超出浏览器限制';
  19. return false;
  20. }
  21. elseif ($upload['error']==3)
  22. {
  23. $this->error='文件仅部分被上传';
  24. return false;
  25. }
  26. elseif ($upload['error']==4)
  27. {
  28. $this->error='没有找到要上传的文件';
  29. return false;
  30. }
  31. elseif ($upload['error']==5)
  32. {
  33. $this->error='服务器临时文件夹丢失';
  34. return false;
  35. }
  36. elseif ($upload['error']==6)
  37. {
  38. $this->error='文件写入到临时文件夹出错';
  39. return false;
  40. }
  41. //对上传类别判断
  42. if (!$this->check_type($upload,$type))
  43. {
  44. $this->error='此文件类别不允许上传';
  45. return false;
  46. }
  47. $dir=PBBLOG_ROOT.'home/upload/'.date('Y-m').'/';
  48. $url_dir='home/upload/'.date('Y-m').'/';
  49. //文件按照月份存档,如果不存在该目录则创建该目录
  50. if (!is_dir($dir))
  51. {
  52. if (!mkdir($dir))
  53. {
  54. $this->error='创建文件上传目录失败';
  55. return false;
  56. }
  57. }
  58. //生成文件名
  59. $file_name=time().rand(10000,60000).'.'.substr($upload['name'], strrpos($upload['name'], '.')+1);
  60. $url_name=$url_dir.$file_name;
  61. $file_name=$dir.$file_name;
  62. if (is_uploaded_file($upload['tmp_name']))
  63. {
  64. if (!move_uploaded_file($upload['tmp_name'], $file_name))
  65. {
  66. $this->error='文件上传失败';
  67. return false;
  68. }
  69. else
  70. {
  71. return $url_name;
  72. }
  73. }
  74. else
  75. {
  76. $this->error='文件上传失败';
  77. return false;
  78. }
  79. }
  80. function check_type($file_name,$type='img')
  81. {
  82. $file_types['img']='gif|jpg|jpeg|png|bmp';
  83. $file_types['flash']='flv|swf';
  84. $file_types['file'] = 'zip|doc|rar|pdf|txt|mp3|wav|mpeg';
  85. $file_mine['img']=array(
  86. 'image/gif',
  87. 'image/jpeg',
  88. 'image/pjpeg',
  89. 'image/png',
  90. 'image/x-png',
  91. 'image/bmp'
  92. );
  93. $file_mine['flash']=array(
  94. 'application/octet-stream',
  95. 'application/x-shockwave-flash'
  96. );
  97. $file_mine['file']=array(
  98. 'application/octet-stream',
  99. 'application/rar',
  100. 'application/zip',
  101. 'application/x-zip-compressed',
  102. 'audio/mpeg',
  103. 'audio/wav',
  104. 'text/plain',
  105. 'application/msword',
  106. 'application/vnd.ms-excel',
  107. 'application/vnd.ms-powerpoint',
  108. 'application/pdf'
  109. );
  110. //文件后缀和类型是否在允许上传范围
  111. $point = strrpos($file_name['name'], '.');
  112. $ext=strtolower(substr($file_name['name'], $point+1, strlen($file_name['name']) - $point));
  113. $types=explode('|',$file_types[$type]);
  114. if (in_array($ext,$types)&&in_array($file_name['type'],$file_mine[$type]))
  115. {
  116. return true;
  117. }
  118. }
  119. function error()
  120. {
  121. return $this->error;
  122. }
  123. }
  124. /*
  125. php缩略图函数:
  126. 等比例无损压缩,可填充补充色 author: 华仔
  127. 主持格式:
  128. bmp 、jpg 、gif、png
  129. param:
  130. @srcimage : 要缩小的图片
  131. @dstimage : 要保存的图片
  132. @dst_width: 缩小宽
  133. @dst_height: 缩小高
  134. @backgroundcolor: 补充色 如:#FFFFFF 支持 6位 不支持3位
  135. */
  136. function imagezoom( $srcimage, $dstimage, $dst_width, $dst_height, $backgroundcolor ) {
  137. // 中文件名乱码
  138. if ( PHP_OS == 'WINNT' ) {
  139. $srcimage = iconv('UTF-8', 'GBK', $srcimage);
  140. $dstimage = iconv('UTF-8', 'GBK', $dstimage);
  141. }
  142. $dstimg = imagecreatetruecolor( $dst_width, $dst_height );
  143. $color = imagecolorallocate($dstimg
  144. , hexdec(substr($backgroundcolor, 1, 2))
  145. , hexdec(substr($backgroundcolor, 3, 2))
  146. , hexdec(substr($backgroundcolor, 5, 2))
  147. );
  148. imagefill($dstimg, 0, 0, $color);
  149. if ( !$arr=getimagesize($srcimage) ) {
  150. echo "要生成缩略图的文件不存在";
  151. exit;
  152. }
  153. $src_width = $arr[0];
  154. $src_height = $arr[1];
  155. $srcimg = null;
  156. $method = getcreatemethod( $srcimage );
  157. if ( $method ) {
  158. eval( '$srcimg = ' . $method . ';' );
  159. }
  160. $dst_x = 0;
  161. $dst_y = 0;
  162. $dst_w = $dst_width;
  163. $dst_h = $dst_height;
  164. if ( ($dst_width / $dst_height - $src_width / $src_height) > 0 ) {
  165. $dst_w = $src_width * ( $dst_height / $src_height );
  166. $dst_x = ( $dst_width - $dst_w ) / 2;
  167. } elseif ( ($dst_width / $dst_height - $src_width / $src_height) < 0 ) {
  168. $dst_h = $src_height * ( $dst_width / $src_width );
  169. $dst_y = ( $dst_height - $dst_h ) / 2;
  170. }
  171. imagecopyresampled($dstimg, $srcimg, $dst_x
  172. , $dst_y, 0, 0, $dst_w, $dst_h, $src_width, $src_height);
  173. // 保存格式
  174. $arr = array(
  175. 'jpg' => 'imagejpeg'
  176. , 'jpeg' => 'imagejpeg'
  177. , 'png' => 'imagepng'
  178. , 'gif' => 'imagegif'
  179. , 'bmp' => 'imagebmp'
  180. );
  181. $suffix = strtolower( array_pop(explode('.', $dstimage ) ) );
  182. if (!in_array($suffix, array_keys($arr)) ) {
  183. echo "保存的文件名错误";
  184. exit;
  185. } else {
  186. eval( $arr[$suffix] . '($dstimg, "'.$dstimage.'");' );
  187. }
  188. imagejpeg($dstimg, $dstimage);
  189. imagedestroy($dstimg);
  190. imagedestroy($srcimg);
  191. }
  192. function getcreatemethod( $file ) {
  193. $arr = array(
  194. '474946' => "imagecreatefromgif('$file')"
  195. , 'FFD8FF' => "imagecreatefromjpeg('$file')"
  196. , '424D' => "imagecreatefrombmp('$file')"
  197. , '89504E' => "imagecreatefrompng('$file')"
  198. );
  199. $fd = fopen( $file, "rb" );
  200. $data = fread( $fd, 3 );
  201. $data = str2hex( $data );
  202. if ( array_key_exists( $data, $arr ) ) {
  203. return $arr[$data];
  204. } elseif ( array_key_exists( substr($data, 0, 4), $arr ) ) {
  205. return $arr[substr($data, 0, 4)];
  206. } else {
  207. return false;
  208. }
  209. }
  210. function str2hex( $str ) {
  211. $ret = "";
  212. for( $i = 0; $i < strlen( $str ) ; $i++ ) {
  213. $ret .= ord($str[$i]) >= 16 ? strval( dechex( ord($str[$i]) ) )
  214. : '0'. strval( dechex( ord($str[$i]) ) );
  215. }
  216. return strtoupper( $ret );
  217. }
  218. // BMP 创建函数 php本身无
  219. function imagecreatefrombmp($filename)
  220. {
  221. if (! $f1 = fopen($filename,"rb")) return FALSE;
  222. $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
  223. if ($FILE['file_type'] != 19778) return FALSE;
  224. $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  225. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  226. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
  227. $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
  228. if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
  229. $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
  230. $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
  231. $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
  232. $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
  233. $BMP['decal'] = 4-(4*$BMP['decal']);
  234. if ($BMP['decal'] == 4) $BMP['decal'] = 0;
  235. $PALETTE = array();
  236. if ($BMP['colors'] < 16777216)
  237. {
  238. $PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
  239. }
  240. $IMG = fread($f1,$BMP['size_bitmap']);
  241. $VIDE = chr(0);
  242. $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
  243. $P = 0;
  244. $Y = $BMP['height']-1;
  245. while ($Y >= 0)
  246. {
  247. $X=0;
  248. while ($X < $BMP['width'])
  249. {
  250. if ($BMP['bits_per_pixel'] == 24)
  251. $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
  252. elseif ($BMP['bits_per_pixel'] == 16)
  253. {
  254. $COLOR = unpack("n",substr($IMG,$P,2));
  255. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  256. }
  257. elseif ($BMP['bits_per_pixel'] == 8)
  258. {
  259. $COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
  260. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  261. }
  262. elseif ($BMP['bits_per_pixel'] == 4)
  263. {
  264. $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  265. if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
  266. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  267. }
  268. elseif ($BMP['bits_per_pixel'] == 1)
  269. {
  270. $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  271. if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
  272. elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
  273. elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
  274. elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
  275. elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
  276. elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
  277. elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
  278. elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
  279. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  280. }
  281. else
  282. return FALSE;
  283. imagesetpixel($res,$X,$Y,$COLOR[1]);
  284. $X++;
  285. $P += $BMP['bytes_per_pixel'];
  286. }
  287. $Y--;
  288. $P+=$BMP['decal'];
  289. }
  290. fclose($f1);
  291. return $res;
  292. }
  293. // BMP 保存函数,php本身无
  294. function imagebmp ($im, $fn = false)
  295. {
  296. if (!$im) return false;
  297. if ($fn === false) $fn = 'php://output';
  298. $f = fopen ($fn, "w");
  299. if (!$f) return false;
  300. $biWidth = imagesx ($im);
  301. $biHeight = imagesy ($im);
  302. $biBPLine = $biWidth * 3;
  303. $biStride = ($biBPLine + 3) & ~3;
  304. $biSizeImage = $biStride * $biHeight;
  305. $bfOffBits = 54;
  306. $bfSize = $bfOffBits + $biSizeImage;
  307. fwrite ($f, 'BM', 2);
  308. fwrite ($f, pack ('VvvV', $bfSize, 0, 0, $bfOffBits));
  309. fwrite ($f, pack ('VVVvvVVVVVV', 40, $biWidth, $biHeight, 1, 24, 0, $biSizeImage, 0, 0, 0, 0));
  310. $numpad = $biStride - $biBPLine;
  311. for ($y = $biHeight - 1; $y >= 0; --$y)
  312. {
  313. for ($x = 0; $x < $biWidth; ++$x)
  314. {
  315. $col = imagecolorat ($im, $x, $y);
  316. fwrite ($f, pack ('V', $col), 3);
  317. }
  318. for ($i = 0; $i < $numpad; ++$i)
  319. fwrite ($f, pack ('C', 0));
  320. }
  321. fclose ($f);
  322. return true;
  323. }
  324. ?>