A handy php file upload processing class

Source: Internet
Author: User
Tags imagejpeg php file upload
  1. //-------------------------------------
  2. File Description: File Upload processing class
  3. File Author: Jesse Lee
  4. //-------------------------------------
  5. Class Upload {
  6. var $dir; Attachment Storage Physical Directory
  7. var $time; Custom File Upload Time
  8. var $allow _types; Allow uploading of attachment types
  9. var $field; Upload Control Name
  10. var $maxsize; Maximum allowable file size, in kilobytes per kb
  11. var $thumb _width; Thumbnail width
  12. var $thumb _height; Thumbnail height
  13. var $watermark _file; Watermark Image Address
  14. var $watermark _pos; Watermark Location
  15. var $watermark _trans;//Watermark Transparency
  16. constructor function
  17. $types: Allowed file types to upload, $maxsize: Allowed size, $field: Upload control name, $time: Custom upload Time
  18. function upload ($types = ' jpg|png ', $maxsize = 1024x768, $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 up and create a directory where files are specifically stored
  25. $basedir: Base directory, must be a physical path
  26. $filedir: Custom subdirectory, available with parameters {Y}, {m}, {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. Picture thumbnail settings, not set if no thumbnail is generated
  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. Picture watermark settings, if not generated add watermark do not set
  47. $file: Watermark image, $pos: Watermark location, $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. Performs a file upload, and returns an array of file information containing the upload success or failure.
  55. Where: Name is the file name, upload success is uploaded to the server filename, upload failure is the local file name
  56. Dir is the physical path where the attachment is stored on the server and the upload failure does not exist for this value
  57. Size is an attachment, upload failure does not exist for this value
  58. 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
  59. ----------------------------------------------------------------- */
  60. function Execute () {
  61. $files = Array (); Successfully uploaded file information
  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 file name extension
  67. $filename = $this->time.mt_rand (100,999). '. '. $fileext; Generate file name
  68. $filedir = $this->dir; Attachment Actual Storage Directory
  69. $filesize = $_files[$field [' Size '] [$key]; File size
  70. File type does not allow
  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. File size exceeded
  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 upload 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. Watermark and generate thumbnails for pictures
  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 thumbnails to generate thumbnails 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. Copy source image as thumbnail if source image is less than or equal to 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 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 file name 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 true-color thumbnail image
  134. $thumb _img = @ImageCreateTrueColor ($t _width, $t _height);
  135. The image smoothness of the imagecopyresampled function copy is better, and the first consideration
  136. if (function_exists (' imagecopyresampled ')) {
  137. @ImageCopyResampled ($thumb _img,php.aspx_img,0,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,0, $t _width, $t _height,php.aspx_info[0],php.aspx_info[1]);
  140. }
  141. Generate thumbnail images
  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. Destroying temporary images
  151. @ImageDestroy (PHP.ASPX_IMG);
  152. @ImageDestroy ($thumb _img);
  153. return true;
  154. }
  155. Add a watermark to a picture
  156. $file: The file to add the watermark to
  157. function Create_watermark ($file) {
  158. The file does not exist and returns
  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. Get 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 Location
  176. Switch ($this->watermark_pos) {
  177. Case 1://Top Left
  178. $DST _x = 0; $DST _y = 0; Break
  179. Case 2://Top Center
  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://Bottom Center
  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); To set the color blending mode of an image
  193. if (function_exists (' Imagesavealpha ')) Imagesavealpha ($temp _wm,true); To save the full alpha channel information
  194. Add a watermark to an 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 picture
  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 Zero image
  213. @imageDestroy ($temp);
  214. @imageDestroy ($temp _wm);
  215. }
  216. }
  217. }
  218. Get file name extension
  219. function Fileext ($filename) {
  220. Return Strtolower (substr (STRRCHR ($filename, '. '), 1,10));
  221. }
  222. }
  223. ?>
Copy Code

Invocation 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
Copy Code
  • Related Article

    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.