A complete php class for uploading images-php Tutorial

Source: Internet
Author: User
A complete php class for uploading images

This php image upload function is very complete and can fully meet the needs of various image uploads

  1. /**************************************
  2. Seesaw associates | http://seesaw.net
  3. Client:
  4. File:
  5. Description:
  6. Copyright (C) 2008 Matt Kenefick (. com)
  7. **************************************/
  8. Class mk_imageUpload {
  9. Var $ max_size;
  10. Var $ allowed_types;
  11. Var $ thumb_height;
  12. Var $ dest_dir;
  13. Var $ extensions;
  14. Var $ max_height;
  15. Var $ max_main_height;
  16. Var $ last_ext;
  17. Var $ last_pid;
  18. Var $ last_uid;
  19. Var $ image_file;
  20. Var $ image_field;
  21. Function _ construct ($ maxHeightMain, $ maxHeightThumb, $ destDir ){
  22. $ This-> max_size = (1024/2) * 1000; // 750kb
  23. $ This-> allowed_types = array ('jpeg ', 'jpg', 'pjpeg ', 'GIF', 'PNG ');
  24. $ This-> extensions = array (
  25. 'Image/jpeg '=> '.jpg ',
  26. 'Image/GIF' => '.gif ',
  27. 'Image/png '=> '.png ',
  28. 'Image/x-png '=> '.png ',
  29. 'Image/pjpeg '=> '.jpg'
  30. );
  31. $ This-> dest_dir = $ destDir;
  32. $ This-> max_height = $ maxHeightThumb;
  33. $ This-> max_main_height = $ maxHeightMain;
  34. }
  35. Function putImage ($ formname, $ newName ){
  36. $ This-> image_field = $ formname;
  37. If ($ this-> getImage ()){
  38. // Check for errors
  39. If (! $ This-> checkType ())
  40. Return $ this-> throwError (2 );
  41. If (! $ This-> checkFileSize ())
  42. Return $ this-> throwError (1 );
  43. // Get Image
  44. $ Img = $ this-> image_file;
  45. // Check seize
  46. If (! $ This-> checkImageSize ())
  47. Return $ this-> throwError (3 );
  48. // Get image dimensinos
  49. $ Size = $ this-> getImageSize ();
  50. $ Size ['width'] = $ size [0];
  51. $ Size ['height'] = $ size [1];
  52. $ Ratio = $ this-> max_height/$ size ['height'];
  53. $ Maxheight = $ this-> max_height;
  54. $ Maxwidth = $ size ['width'] * $ ratio;
  55. // Create Thumbnail
  56. $ S_t = $ this-> resizeImage ($ size, $ img, $ maxwidth, $ maxheight, $ newName,'s ');
  57. If ($ size ['height']> $ this-> max_main_height ){
  58. $ Ratio = $ this-> max_main_height/$ size ['height'];
  59. $ Maxheight = $ this-> max_main_height;
  60. $ Maxwidth = $ size ['width'] * $ ratio;
  61. } Else {
  62. $ Maxheight = $ size ['height'];
  63. $ Maxwidth = $ size ['width'];
  64. }
  65. // Create Large rescaled
  66. $ S_l = $ this-> resizeImage ($ size, $ img, $ maxwidth, $ maxheight, $ newName, 'L ');
  67. // Remove temporary file
  68. Unlink ($ img ['tmp _ name']);
  69. If ($ s_t & $ s_l ){
  70. $ Nm = split ('_', $ newName );
  71. $ This-> last_ext = $ this-> extensions [$ size ['Mime '];
  72. $ This-> last_pid = $ nm [0];
  73. $ This-> last_uid = $ nm [1];
  74. Return 'OK ';
  75. } Else {
  76. Return $ this-> throwError (4 );
  77. }
  78. } Else {
  79. Return $ this-> throwError (2 );
  80. }
  81. }
  82. Function resizeImage ($ size, $ img, $ maxwidth, $ maxheight, $ newName, $ ext ){
  83. // Create a thumbnail
  84. If ($ size ['Mime '] = "image/pjpeg" | $ size ['Mime'] = "image/jpeg "){
  85. $ New_img = imagecreatefromjpeg ($ img ['tmp _ name']);
  86. } Elseif ($ size ['Mime '] = "image/x-png" | $ size ['Mime'] = "image/png "){
  87. $ New_img = imagecreatefrompng ($ img ['tmp _ name']);
  88. } Elseif ($ size ['Mime '] = "image/gif "){
  89. $ New_img = imagecreatefromgif ($ img ['tmp _ name']);
  90. }
  91. If (function_exists ('imagecreatetruecolor ')){
  92. $ Resized_img = imagecreatetruecolor ($ maxwidth, $ maxheight );
  93. } Else {
  94. Return ("Error: Please make sure your server has GD library ver 2 + ");
  95. }
  96. Imagecopyresized ($ resized_img, $ new_img, 0, 0, 0, $ maxwidth, $ maxheight, $ size ['width'], $ size ['height']);
  97. If ($ size ['Mime '] = "image/pjpeg" | $ size ['Mime'] = "image/jpeg "){
  98. $ Success = ImageJpeg ($ resized_img, $ this-> dest_dir. $ newName. '_'. $ ext. $ this-> extensions [$ size ['Mime ']);
  99. } Elseif ($ size ['Mime '] = "image/x-png" | $ size ['Mime'] = "image/png "){
  100. $ Success = ImagePNG ($ resized_img, $ this-> dest_dir. $ newName. '_'. $ ext. $ this-> extensions [$ size ['Mime ']);
  101. } Elseif ($ size ['Mime '] = "image/gif "){
  102. $ Success = ImageGIF ($ resized_img, $ this-> dest_dir. $ newName. '_'. $ ext. $ this-> extensions [$ size ['Mime ']);
  103. }
  104. // Remove temporary images
  105. ImageDestroy ($ resized_img );
  106. ImageDestroy ($ new_img );
  107. Return $ success;
  108. }
  109. Function getImage (){
  110. If (isset ($ _ FILES [$ this-> image_field]) & is_uploaded_file ($ _ FILES [$ this-> image_field] ['tmp _ name']) {
  111. $ This-> image_file =$ _ FILES [$ this-> image_field];
  112. Return true;
  113. }
  114. Return false;
  115. }
  116. Function returnImg (){
  117. Return $ this-> image_file;
  118. }
  119. Function getImageSize (){
  120. $ Img = $ this-> returnImg ();
  121. Return getimagesize ($ img ['tmp _ name']);
  122. }
  123. Function checkImageSize (){
  124. $ Size = $ this-> getImageSize ();
  125. If ($ size [1] <$ this-> max_height)
  126. Return false;
  127. Return true;
  128. }
  129. Function checkFileSize (){
  130. $ Img = $ this-> returnImg ();
  131. If ($ img ['size']> $ this-> max_size)
  132. Return false;
  133. Return true;
  134. }
  135. Function checkType (){
  136. $ Img = $ this-> returnImg ();
  137. $ Type = split ('/', $ img ['type']);
  138. If (! In_array ($ type [1], $ this-> allowed_types ))
  139. Return false;
  140. Return true;
  141. }
  142. Function throwError ($ val ){
  143. Switch ($ val ){
  144. Case 1: return 'Error: File size is too large ';
  145. Break;
  146. Case 2: return 'Error: Improper file format ';
  147. Break;
  148. Case 3: return 'Error: Your image is too small ';
  149. Break;
  150. Case 4: return 'Error: There was an Error creating the images ';
  151. Break;
  152. }
  153. }
  154. }
  155. ?>


Image upload, php

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.