PHP image upload class; support for watermark-date folder-generating thumbnails; support for multifile Upload

Source: Internet
Author: User
Tags imagejpeg
PHP image upload class; support for watermark-date folder-generating thumbnails; support for multifile Upload
You can use {Y} {m} {n} to change the current date.

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


Image upload, PHP

This topic was moved by Beckham

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.