A handy php file Upload processing class

Source: Internet
Author: User
Tags imagejpeg
A handy php file Upload processing class

  1. //-------------------------------------
  2. // File description: File Upload processing class
  3. // File author: Jesse Lee
  4. //-------------------------------------
  5. Class upload {
  6. Var $ dir; // The physical directory where the attachment is stored.
  7. Var $ time; // custom file upload time
  8. Var $ allow_types; // The attachment type that can be uploaded
  9. Var $ field; // The name of the upload control.
  10. Var $ maxsize; // maximum allowed file size, in KB
  11. Var $ thumb_width; // The width of the thumbnail.
  12. Var $ thumb_height; // The height of the thumbnail.
  13. Var $ watermark_file; // watermark image address
  14. Var $ watermark_pos; // Watermark Position
  15. Var $ watermark_trans; // watermark transparency
  16. // Constructor
  17. // $ Types: specifies the file type that can be uploaded. $ maxsize: size allowed. $ field: indicates the name of the upload control. $ time: indicates the custom Upload time.
  18. Function upload ($ types = 'jpg | png ', $ maxsize = 1024, $ field = 'Attach', $ time = ''){
  19. $ This-> allow_types = explode ('|', $ types );
  20. $ This-> maxsize = $ maxsize * 1024;
  21. $ This-> field = $ field;
  22. $ This-> time = $ time? $ Time: time ();
  23. }
  24. // Set and create a directory for storing files
  25. // $ Basedir: Base Directory, which must be a physical path
  26. // $ Filedir: custom subdirectory. available parameters: {y}, {m}, and {d}
  27. Function set_dir ($ basedir, $ filedir = ''){
  28. $ Dir = $ basedir;
  29. ! Is_dir ($ dir) & @ mkdir ($ dir, 0777 );
  30. If (! Empty ($ filedir )){
  31. $ Filedir = str_replace (array ('{y}', '{m}', '{y}'), array (date ('Y ', $ this-> time), date ('M', $ this-> time), date ('D', $ this-> time )), strtolower ($ filedir ));
  32. $ Dirs = explode ('/', $ filedir );
  33. Foreach ($ dirs as $ d ){
  34. ! Empty ($ d) & $ dir. = $ d .'/';
  35. ! Is_dir ($ dir) & @ mkdir ($ dir, 0777 );
  36. }
  37. }
  38. $ This-> dir = $ dir;
  39. }
  40. // Set the image thumbnail. If no thumbnail is generated, you do not need to set it.
  41. // $ Width: thumbnail width, $ height: Thumbnail height
  42. Function set_thumb ($ width = 0, $ height = 0 ){
  43. $ This-> thumb_width = $ width;
  44. $ This-> thumb_height = $ height;
  45. }
  46. // Set the image Watermark. you do not need to set the watermark if no watermark is generated.
  47. // $ File: Watermark Image, $ pos: Watermark Position, $ trans: watermark transparency
  48. Function set_watermark ($ file, $ pos = 6, $ trans = 80 ){
  49. $ This-> watermark_file = $ file;
  50. $ This-> watermark_pos = $ pos;
  51. $ This-> watermark_trans = $ trans;
  52. }
  53. /*----------------------------------------------------------------
  54. After the file is uploaded, an array of file information including successful or failed uploads is returned,
  55. Where: name is the file name. when the upload is successful, it is the file name uploaded to the server. if the upload fails, it is the local file name.
  56. Dir is the physical path where the attachment is stored on the server. this value does not exist when the upload fails.
  57. Size indicates the attachment size. this value does not exist when the upload fails.
  58. Flag indicates the status, 1 indicates the success,-1 indicates that the file type is not allowed, and-2 indicates that the file size exceeds
  59. -----------------------------------------------------------------*/
  60. Function execute (){
  61. $ Files = array (); // information of the successfully uploaded File
  62. $ Field = $ this-> field;
  63. $ Keys = array_keys ($ _ FILES [$ field] ['name']);
  64. Foreach ($ keys as $ key ){
  65. If (! $ _ FILES [$ field] ['name'] [$ key]) continue;
  66. $ Fileext = $ this-> fileext ($ _ FILES [$ field] ['name'] [$ key]); // get the file extension
  67. $ Filename = $ this-> time. mt_rand (100,999). '.'. $ fileext; // Generate a file name
  68. $ Filedir = $ this-> dir; // actual directory of the attachment
  69. $ Filesize = $ _ FILES [$ field] ['size'] [$ key]; // file size
  70. // The file type is not allowed.
  71. If (! In_array ($ fileext, $ this-> allow_types )){
  72. $ Files [$ key] ['name'] =_ _ FILES [$ field] ['name'] [$ key];
  73. $ Files [$ key] ['flag'] =-1;
  74. Continue;
  75. }
  76. // The file size exceeds
  77. If ($ filesize> $ this-> maxsize ){
  78. $ Files [$ key] ['name'] =_ _ FILES [$ field] ['name'] [$ key];
  79. $ Files [$ key] ['flag'] =-2;
  80. Continue;
  81. }
  82. $ Files [$ key] ['name'] = $ filename;
  83. $ Files [$ key] ['dir'] = $ filedir;
  84. $ Files [$ key] ['size'] = $ filesize;
  85. // Save the uploaded file and delete the temporary file
  86. If (is_uploaded_file ($ _ FILES [$ field] ['tmp _ name'] [$ key]) {
  87. Move_uploaded_file ($ _ FILES [$ field] ['tmp _ name'] [$ key], $ filedir. $ filename );
  88. @ Unlink ($ _ FILES [$ field] ['tmp _ name'] [$ key]);
  89. $ Files [$ key] ['flag'] = 1;
  90. // Add a watermark to the image and generate a thumbnail
  91. If (in_array ($ fileext, array ('jpg ', 'PNG', 'GIF '))){
  92. If ($ this-> thumb_width ){
  93. If ($ this-> create_thumb ($ filedir. $ filename, $ filedir. 'thumb _ '. $ filename )){
  94. $ Files [$ key] ['thumb'] = 'thumb _ '. $ filename; // Thumbnail file name
  95. }
  96. }
  97. $ This-> create_watermark ($ filedir. $ filename );
  98. }
  99. }
  100. }
  101. Return $ files;
  102. }
  103. // Create a thumbnail to generate a thumbnail with the same extension
  104. // Php. aspx_file: source image path, $ thumb_file: thumbnail path
  105. Function create_thumb (Php. aspx_file, $ thumb_file ){
  106. $ T_width = $ this-> thumb_width;
  107. $ T_height = $ this-> thumb_height;
  108. If (! File_exists (Php. aspx_file) return false;
  109. Php. aspx_info = getImageSize (Php. aspx_file );
  110. // If the source image is smaller than or equal to the thumbnail, copy the source image as the thumbnail.
  111. If (Php. aspx_info [0] <= $ t_width & Php. aspx_info [1] <= $ t_height ){
  112. If (! Copy (Php. aspx_file, $ thumb_file )){
  113. Return false;
  114. }
  115. Return true;
  116. }
  117. // Calculate the thumbnail size proportionally
  118. If (Php. aspx_info [0]-$ t_width> Php. aspx_info [1]-$ t_height ){
  119. $ T_height = ($ t_width/Php. aspx_info [0]) * Php. aspx_info [1];
  120. } Else {
  121. $ T_width = ($ t_height/Php. aspx_info [1]) * Php. aspx_info [0];
  122. }
  123. // Get the file extension
  124. $ Fileext = $ this-> fileext (Php. aspx_file );
  125. Switch ($ fileext ){
  126. Case 'jpg ':
  127. Php. aspx_img = ImageCreateFromJPEG (Php. aspx_file); break;
  128. Case 'PNG ':
  129. Php. aspx_img = ImageCreateFromPNG (Php. aspx_file); break;
  130. Case 'GIF ':
  131. Php. aspx_img = ImageCreateFromGIF (Php. aspx_file); break;
  132. }
  133. // Create a real-color thumbnail image
  134. $ Thumb_img = @ ImageCreateTrueColor ($ t_width, $ t_height );
  135. // The image copied by the ImageCopyResampled function has good smoothness and is preferred.
  136. If (function_exists ('imagecopyresampled ')){
  137. @ ImageCopyResampled ($ thumb_img, Php. aspx_img, 0, 0, 0, $ t_width, $ t_height, Php. aspx_info [0], Php. aspx_info [1]);
  138. } Else {
  139. @ ImageCopyResized ($ thumb_img, Php. aspx_img, 0, 0, 0, $ t_width, $ t_height, Php. aspx_info [0], Php. aspx_info [1]);
  140. }
  141. // Generate a thumbnail
  142. Switch ($ fileext ){
  143. Case 'jpg ':
  144. ImageJPEG ($ thumb_img, $ thumb_file); break;
  145. Case 'GIF ':
  146. ImageGIF ($ thumb_img, $ thumb_file); break;
  147. Case 'PNG ':
  148. ImagePNG ($ thumb_img, $ thumb_file); break;
  149. }
  150. // Destroy temporary images
  151. @ ImageDestroy (Php. aspx_img );
  152. @ ImageDestroy ($ thumb_img );
  153. Return true;
  154. }
  155. // Add a watermark to the image
  156. // $ File: the file to add a watermark
  157. Function create_watermark ($ file ){
  158. // If the object does not exist, return
  159. If (! File_exists ($ this-> watermark_file) |! File_exists ($ file) return;
  160. If (! Function_exists ('getimagesize') return;
  161. // Check the file types supported by GD
  162. $ Gd_allow_types = array ();
  163. If (function_exists ('imagecreatefromgif ') $ gd_allow_types ['image/GIF'] = 'imagecreatefromgif ';
  164. If (function_exists ('imagecreatefrompng ') $ gd_allow_types ['image/PNG'] = 'imagecreatefrompng ';
  165. If (function_exists ('imagecreatefromjpeg ') $ gd_allow_types ['image/jpeg'] = 'imagecreatefromjpeg ';
  166. // Obtain the file information
  167. $ Fileinfo = getImageSize ($ file );
  168. $ Wminfo = getImageSize ($ this-> watermark_file );
  169. If ($ fileinfo [0] <$ wminfo [0] | $ fileinfo [1] <$ wminfo [1]) return;
  170. If (array_key_exists ($ fileinfo ['Mime '], $ gd_allow_types )){
  171. If (array_key_exists ($ wminfo ['Mime '], $ gd_allow_types )){
  172. // Create an image from a file
  173. $ Temp = $ gd_allow_types [$ fileinfo ['Mime '] ($ file );
  174. $ Temp_wm = $ gd_allow_types [$ wminfo ['Mime '] ($ this-> watermark_file );
  175. // Watermark Position
  176. Switch ($ this-> watermark_pos ){
  177. Case 1: // top left
  178. $ Dst_x = 0; $ dst_y = 0; break;
  179. Case 2: // center the top
  180. $ Dst_x = ($ fileinfo [0]-$ wminfo [0])/2; $ dst_y = 0; break;
  181. Case 3: // top right
  182. $ Dst_x = $ fileinfo [0]; $ dst_y = 0; break;
  183. Case 4: // bottom left
  184. $ Dst_x = 0; $ dst_y = $ fileinfo [1]; break;
  185. Case 5: // center at the bottom
  186. $ Dst_x = ($ fileinfo [0]-$ wminfo [0])/2; $ dst_y = $ fileinfo [1]; break;
  187. Case 6: // bottom right
  188. $ Dst_x = $ fileinfo [0]-$ wminfo [0]; $ dst_y = $ fileinfo [1]-$ wminfo [1]; break;
  189. Default: // random
  190. $ Dst_x = mt_rand (0, $ fileinfo [0]-$ wminfo [0]); $ dst_y = mt_rand (0, $ fileinfo [1]-$ wminfo [1]);
  191. }
  192. If (function_exists ('imagealphablending ') ImageAlphaBlending ($ temp_wm, True); // sets the mixed color mode of the image.
  193. If (function_exists ('imagesavealpha ') ImageSaveAlpha ($ temp_wm, True); // Save the complete alpha channel information
  194. // Add a watermark to the image
  195. If (function_exists ('imagecopymerge ')){
  196. ImageCopyMerge ($ temp, $ temp_wm, $ dst_x, $ dst_y, 0, 0, $ wminfo [0], $ wminfo [1], $ this-> watermark_trans );
  197. } Else {
  198. ImageCopyMerge ($ temp, $ temp_wm, $ dst_x, $ dst_y, 0, 0, $ wminfo [0], $ wminfo [1]);
  199. }
  200. // Save the image
  201. Switch ($ fileinfo ['Mime ']) {
  202. Case 'image/jpeg ':
  203. @ ImageJPEG ($ temp, $ file );
  204. Break;
  205. Case 'image/png ':
  206. @ ImagePNG ($ temp, $ file );
  207. Break;
  208. Case 'image/GIF ':
  209. @ ImageGIF ($ temp, $ file );
  210. Break;
  211. }
  212. // Destroy the zero-time image
  213. @ ImageDestroy ($ temp );
  214. @ ImageDestroy ($ temp_wm );
  215. }
  216. }
  217. }
  218. // Obtain the file extension
  219. Function fileext ($ filename ){
  220. Return strtolower (substr (strrchr ($ filename, '.'), 1, 10 ));
  221. }
  222. }
  223. ?>

Call example:

  1. If ($ _ GET ['action'] = 'save '){
  2. $ Up = new upload ();
  3. $ Up-> set_dir (dirname (_ FILE _). '/upload/', '{y}/{m }');
  4. $ Up-> set_thumb (100,80 );
  5. $ Up-> set_watermark (dirname (_ FILE _). '/jblog/images/watermark.png', 6, 90 );
  6. $ Fs = $ up-> execute ();
  7. Var_dump ($ fs );
  8. }
  9. ?>
  10. Test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.