A full picture upload PHP class

Source: Internet
Author: User
Tags php class upload php

This PHP image upload class function is very complete, can meet a variety of image upload needs

  1. /**************************************
  2. Seesaw Associates | Http://seesaw.net
  3. Client
  4. File
  5. Description
  6. Copyright (C) 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 have GD library ver.");
  95. }
  96. Imagecopyresized ($resized _img, $new _img, 0, 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 is an Error creating the images ';
  151. Break
  152. }
  153. }
  154. }
  155. ?>
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.