PHP image upload class and generate thumbnail image

Source: Internet
Author: User
Tags imagejpeg
  1. /**
  2. * Upload Images
  3. */
  4. Class imgupload{
  5. static protected $a;
  6. protected $formName; Form name
  7. protected $directory; File Upload to Directory
  8. protected $maxSize; Maximum file Upload size
  9. protected $canUpload; Whether you can upload
  10. protected $doUpFile; File name of the upload
  11. protected $SM _file; Thumbnail name
  12. Private function __construct ($_formname= ' file ', $_directory= './images/uploads/', $_maxsize=1048576) {//1024*1024=1m
  13. Initialize parameters
  14. $this->formname = $_formname;
  15. $this->directory = $_directory;
  16. $this->maxsize = $_maxsize;
  17. $this->canupload = true;
  18. $this->doupfile = ";
  19. $this->sm_file = ";
  20. }
  21. Determine if the picture is in the allowed format
  22. static public Function Type ($_formname= ' file ') {
  23. $_type = $_files[$_formname][' type '];
  24. Switch ($_type) {
  25. Case ' image/gif ':
  26. if (self:: $a ==null)
  27. Self:: $a = new Imgupload ($_formname);
  28. Break
  29. Case ' Image/pjpeg ':
  30. if (self:: $a ==null)
  31. Self:: $a = new Imgupload ($_formname);
  32. Break
  33. Case ' image/x-png ':
  34. if (self:: $a ==null)
  35. Self:: $a = new Imgupload ($_formname);
  36. Break
  37. Default
  38. Self:: $a = false;
  39. }
  40. Return self:: $a;
  41. }
  42. Get File size
  43. Public Function GetSize ($_format= ' K ') {
  44. if ($this->canupload) {
  45. if (0 = = $_files[$this->formname][' size ') {
  46. $this->canupload = false;
  47. return $this->canupload;
  48. Break
  49. }
  50. Switch ($_format) {
  51. Case ' B ':
  52. return $_files[$this->formname][' size ');
  53. Break
  54. Case ' K ':
  55. Return round ($_files[$this->formname][' size ')/1024);
  56. Break
  57. Case ' M ':
  58. Return round ($_files[$this->formname][' size ')/(1024*1024), 2);
  59. Break
  60. }
  61. }
  62. }
  63. Get file type
  64. Public Function Getext () {
  65. if ($this->canupload) {
  66. $_name = $_files[$this->formname][' name '];
  67. $_namearr = Explode ('. ', $_name);
  68. $_count = count ($_namearr)-1;
  69. }
  70. return $_namearr[$_count];
  71. }
  72. Get file name
  73. Public Function GetName () {
  74. if ($this->canupload) {
  75. return $_files[$this->formname][' name '];
  76. }
  77. }
  78. New file name
  79. Public Function NewName () {
  80. Return date (' Ymdhis '). Rand (0,9);
  81. }
  82. Uploading files
  83. Public function upload () {
  84. if ($this->canupload)
  85. {
  86. $_getsize = $this->getsize (' B ');
  87. if (!$_getsize)
  88. {
  89. return $_getsize;
  90. Break
  91. }
  92. Else
  93. {
  94. $_newname = $this->newname ();
  95. $_ext = $this->getext ();
  96. $_doupload = Move_uploaded_file ($_files[$this->formname][' tmp_name '), $this->directory.$_newname. ".". $_ext);
  97. if ($_doupload)
  98. {
  99. $this->doupfile = $_newname;
  100. }
  101. return $_doupload;
  102. }
  103. }
  104. }
  105. Create a thumbnail image
  106. Public function thumb ($_dstchar= ' _m ', $_max_len=320) {//$_dstchar:_m, _s
  107. if ($this->canupload && $this->doupfile! = "") {
  108. $_ext = $this->getext ();
  109. $_srcimage = $this->directory. $this->doupfile. ".". $_ext;
  110. Get an array of picture information
  111. $_date = getimagesize ($_srcimage, & $info);
  112. $SRC _w = $_date[0]; Source Picture Width
  113. $SRC _h = $_date[1]; SOURCE Picture High
  114. $SRC _max_len = max ($src _w, $src _h); Long Edge
  115. $src _min_len = min ($src _w, $src _h); Short Edge
  116. $DST _w = "; Target Image width
  117. $DST _h = "; Target Picture High
  118. Wide-height scaling, longest side not greater than $_max_len
  119. if ($src _max_len>$_max_len)
  120. {
  121. $percent = $src _min_len/$src _max_len;
  122. if ($src _w = = $src _max_len)
  123. {
  124. $DST _w = $_max_len;
  125. $DST _h = $percent * $DST _w;
  126. }
  127. Else
  128. {
  129. $DST _h = $_max_len;
  130. $DST _w = $percent * $DST _h;
  131. }
  132. }
  133. Else
  134. {
  135. $DST _w = $src _w;
  136. $DST _h = $src _h;
  137. }
  138. The location of the source picture when you create a thumbnail image
  139. $SRC _x = ";
  140. $src _y = ";
  141. Determine if the thumbnail is used for the logo, it will be cut
  142. if (' s_ ' = = $_dstchar) {
  143. $src _x = $src _w * 0.10;
  144. $src _y = $src _h * 0.10;
  145. $SRC _w *= 0.8;
  146. $SRC _h *= 0.8;
  147. }
  148. Determine the type of picture and create a new picture
  149. Switch ($_date[2]) {
  150. Case 1:
  151. $src _im = imagecreatefromgif ($_srcimage);
  152. Break
  153. Case 2:
  154. $src _im = Imagecreatefromjpeg ($_srcimage);
  155. Break
  156. Case 3:
  157. $src _im = imagecreatefrompng ($_srcimage);
  158. Break
  159. Case 8:
  160. $src _im = imagecreatefromwbmp ($_srcimage);
  161. Break
  162. }
  163. Create a new image
  164. if ($_date[2]==1) {//gif cannot be applied Imagecreatetruecolor
  165. $DST _im = imagecreate ($dst _w, $dst _h);
  166. }else {
  167. $DST _im = Imagecreatetruecolor ($dst _w, $dst _h);
  168. }
  169. Thumbnail copy of this image
  170. $BG = Imagecolorallocate ($dst _im,255,255,0);
  171. Imagecopyresized ($dst _im, $src _im, 0, 0, $src _x, $src _y, $dst _w, $dst _h, $src _w, $src _h);
  172. antialiasing a picture
  173. Imageantialias ($dst _im, true);
  174. Switch ($_date[2]) {
  175. Case 1:
  176. $CR = Imagegif ($dst _im, $this->directory. $this->doupfile.$_dstchar. ".". $_ext, 100);
  177. Break
  178. Case 2:
  179. $CR = imagejpeg ($dst _im, $this->directory. $this->doupfile.$_dstchar. ".". $_ext, 100);
  180. Break
  181. Case 3://imagepng has a problem, so use imagejpg instead
  182. $CR = imagejpeg ($dst _im, $this->directory. $this->doupfile.$_dstchar. ".". $_ext, 100);
  183. Break
  184. }
  185. $CR = imagejpeg ($dst _im, $this->directory.$_dstchar. $this->doupfile, 90);
  186. if ($CR) {
  187. $this->sm_file = $this->directory. $this->doupfile.$_dstchar. ".". $_ext;
  188. return $this->sm_file;
  189. }else {
  190. return false;
  191. }
  192. }
  193. Imagedestroy ($dst _im);
  194. Imagedestroy ($CR);
  195. }
  196. Get the file name after uploading
  197. Public Function Getupfile () {
  198. if ($this->doupfile!= ") {
  199. $_ext = $this->getext ();
  200. return $this->doupfile. ".". $_ext;
  201. }else {
  202. return false;
  203. }
  204. }
  205. Get the file full path after uploading
  206. Public Function Getfilepatch () {
  207. if ($this->doupfile!= ") {
  208. $_ext = $this->getext ();
  209. Return $this->directory. $this->doupfile. ".". $_ext;
  210. }else {
  211. return false;
  212. }
  213. }
  214. Get thumbnail file full path
  215. Public Function Getthumb () {
  216. if ($this->sm_file!= ") {
  217. return $this->sm_file;
  218. }else {
  219. return false;
  220. }
  221. }
  222. Get the path to the uploaded file
  223. Public Function getdirectory () {
  224. if ($this->directory!= ") {
  225. return $this->directory;
  226. }else {
  227. return false;
  228. }
  229. }
  230. }
  231. ?>
Copy Code
Image upload, PHP
  • 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.