PHP image upload class, support watermark-date folder-Generate thumbnails, support multi-file upload

Source: Internet
Author: User
Tags imagejpeg watermark images
You can use {Y}{m}{n} to become the current date
  1. Set_dir (DirName (__file__). ' /upload/', ' {y}/{m} '); Save path, support {y}{m}{d} these several options
  2. $up->set_thumb (100,80); Thumbnail size setting. Units 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); Scenarios for testing the view class
  6. }
  7. ?>
  8. View Sheet---------
  9. Test
  10. Support Multiple image uploads
  11. */
  12. Class Upload {
  13. var $dir; Attachment Storage Physical Directory
  14. var $time; Custom File Upload Time
  15. var $allow _types; Allow uploading of attachment types
  16. var $field; Upload Control Name
  17. var $maxsize; Maximum allowable file size, in kilobytes per kb
  18. var $thumb _width; Thumbnail width
  19. var $thumb _height; Thumbnail height
  20. var $watermark _file; Watermark Image Address
  21. var $watermark _pos; Watermark Location
  22. var $watermark _trans;//Watermark Transparency
  23. constructor function
  24. $types: Allowed file types to upload, $maxsize: Allowed size, $field: Upload control name, $time: Custom upload Time
  25. function upload ($types = ' jpg|png ', $maxsize = 1024x768, $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 up and create a directory where files are specifically stored
  32. $basedir: Base directory, must be a physical path
  33. $filedir: Custom subdirectory, available with parameters {Y}, {m}, {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 {y} {m} {D} 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. Picture thumbnail settings, not set if no thumbnail is generated
  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. Picture watermark settings, if not generated add watermark do not set
  54. $file: Watermark image, $pos: Watermark location, $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. Performs a file upload, and returns an array of file information containing the upload success or failure.
  62. Where: Name is the file name, upload success is uploaded to the server filename, upload failure is the local file name
  63. Dir is the physical path where the attachment is stored on the server and the upload failure does not exist for this value
  64. Size is an attachment, upload failure does not exist for this value
  65. Flag is the status ID, 1 indicates 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 (); Successfully uploaded file information
  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 file name extension
  74. $filename = Date (' Ymdhis ', $this->time). Mt_rand (10,99). '. '. $fileext; Generate file name
  75. $filedir = $this->dir; Attachment Actual Storage Directory
  76. $filesize = $_files[$field [' Size '] [$key]; File size
  77. File type does not allow
  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. File size exceeded
  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 upload 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. Watermark images and generate thumbnails, where the demo only supports JPG and PNG (GIF generated words will be out of frame)
  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 thumbnails to generate thumbnails 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. Copy the source image as a thumbnail if the source image is less than or equal to the thumbnail, eliminating the action
  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 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 file name 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 true-color thumbnail image
  142. $thumb _img = @ImageCreateTrueColor ($t _width, $t _height);
  143. The image smoothness of the imagecopyresampled function copy is better, and the first consideration
  144. if (function_exists (' imagecopyresampled ')) {
  145. @ImageCopyResampled ($thumb _img, $src _img,0,0,0,0, $t _width, $t _height, $src _info[0], $src _info[1]);
  146. } else {
  147. @ImageCopyResized ($thumb _img, $src _img,0,0,0,0, $t _width, $t _height, $src _info[0], $src _info[1]);
  148. }
  149. Generate thumbnail images
  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. Destroying temporary images
  159. @ImageDestroy ($src _img);
  160. @ImageDestroy ($thumb _img);
  161. return true;
  162. }
  163. Add a watermark to a picture
  164. $file: The file to add the watermark to
  165. function Create_watermark ($file) {
  166. The file does not exist and returns
  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. Get 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 Location
  184. Switch ($this->watermark_pos) {
  185. Case 1://Top Left
  186. $DST _x = 0; $DST _y = 0; Break
  187. Case 2://Top Center
  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://Bottom Center
  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); To set the color blending mode of an image
  201. if (function_exists (' Imagesavealpha ')) Imagesavealpha ($temp _wm,true); To save the full alpha channel information
  202. Add a watermark to an 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 picture
  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 Zero image
  221. @imageDestroy ($temp);
  222. @imageDestroy ($temp _wm);
  223. }
  224. }
  225. }
  226. Get file name extension
  227. function Fileext ($filename) {
  228. Return Strtolower (substr (STRRCHR ($filename, '. '), 1,10));
  229. }
  230. }
  231. ?>
Copy Code
Image upload, PHP This topic was moved by Beckham on 2015-11-18 08:23
  • 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.