Maison > développement back-end > tutoriel php > 一个完整的图片上传php类

一个完整的图片上传php类

WBOY
Libérer: 2016-07-25 08:43:06
original
1019 Les gens l'ont consulté

这个php图片上传类功能非常完善,完全可以满足各种图片上传需求

  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, 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] 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. ?>
复制代码

图片上传, php


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal