首頁 >後端開發 >php教程 >一個完整的圖片上傳php類

一個完整的圖片上傳php類

WBOY
WBOY原創
2016-07-25 08:43:061021瀏覽

這個php圖片上傳類別功能非常完善,完全可以滿足各種圖片上傳需求

  1. /********************************
  2. 蹺蹺板同事| http://seesaw.net
  3. 用戶端:
  4. 文件:
  5. 說明:
  6. 版權所有(C) 2008 Matt Kenefick(.com)
  7. ***** ******** ***************************/
  8. class mk_imageUpload{
  9. var $max_size; var $thumb_height;
  10. var $dest_dir;
  11. var $extensions;
  12. var $max_height;
  13. var $max_main_height;
  14. ; var $last_pid;
  15. var $last_uid;
  16. var $image_file;
  17. var $image_field;
  18. function __construct( $maxHeightMain, $maxHeightMain, $maxHeight $this->max_size = (1024/2)*1000; // 750kb
  19. $this->allowed_types = array( 'jpeg', 'jpg', 'pjpeg', 'gif', 'png' );
  20. $this->extensions = array(
  21. ' image/jpeg' => '.jpg',
  22. 'image/gif' => '.gif',
  23. 'image/png' => '.png',
  24. ' image/x-png ' => '.png',
  25. 'image/pjpeg' => '.jpg'
  26. );
  27. $this->dest_dir = $destDir;
  28. $this->max_height = $maxHeightThumb ;
  29. $this->max_main_height = $maxHeightMain;
  30. }
  31. function putImage( $formname, $newName ){
  32. $this-> ;image_field = $formname;
  33. if ( $this->getImage() ){
  34. // 檢查錯誤
  35. if ( !$this->checkType() )
  36. return $this->throwError(2);
  37. if ( !$this->checkFileSize() )
  38. return $this->throwError(1);
  39. // 取得圖片
  40. $img = $this-> image_file;
  41. // 檢查acquire
  42. if ( !$this->checkImageSize() )
  43. return $this -> throwError(3);
  44. // 取得圖片尺寸
  45. $size = $this->getImageSize();
  46. $size['width'] = $size[0] ;
  47. $size['height'] = $size[1];
  48. $ratio = $this->max_height/$size['height'];
  49. $maxheight = $this ->max_height;
  50. $maxwidth = $size['width'] * $ratio;
  51. // 建立
  52. $s_t = $this->resizeImage( $size, $img , $maxwidth, $maxheight,$newName,'s' );
  53. if ( $ $ size['height'] > $this->max_main_height ){
  54. $ratio = $this->max_main_height/$size['height'];
  55. $maxheight = $this->max_main_height;
  56. $ $ maxwidth = $size ['寬度'] * $ratio;
  57. }else{
  58. $maxheight = $size['height'];
  59. $maxwidth = $size['width'];
  60. }
  61. // 建立大的重新縮放
  62. $s_l = $this->resizeImage( $size, $img, $maxwidth, $maxheight,$newName,'l' );
  63. // 刪除暫存檔案
  64. unlink($img['tmp_name']);
  65. if( $s_t && $s_l ){
  66. $nm = split('_', $newName );
  67. $this->last_ext = $this->extensions[$size['mime']];
  68. $this->last_pid = $nm[0];
  69. $this-> last_uid = $ nm[1];
  70. return 'ok';
  71. }else{
  72. return $this->throwError( 4 );
  73. }
  74. }else{
  75. return $this->throwError( 2 );
  76. }
  77. }
  78. function resizeImage($size,$img, $maxwid,$img, $ maxheight,$newName,$ext){
  79. // 建立大概
  80. if($size['mime'] == "image/pjpeg" || $size['mime'] == "image / jpeg "){
  81. $new_img = imagecreatefromjpeg($img['tmp_name']);
  82. }elseif($size['mime'] == "image/x-png" || $size[' mime ' ] == "image/png"){
  83. $new_img = imagecreatefrompng($img['tmp_name']);
  84. }elseif($size['mime'] == "image/gif") {
  85. $new_img = imagecreatefromgif($img['tmp_name']);
  86. }
  87. if (function_exists('imagecreatetruecolor')){
  88. $resized_img = color')){
  89. $resized_img = 著色;
  90. }else{
  91. return("錯誤:請確保您的伺服器有GD 函式庫版本2 ");
  92. }
  93. imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $maxwidth, $maxheight, $size['width'], $size['height']);
  94. if(> if(> if(> if(> if(>) $size['mime'] == "image/pjpeg" || $size['mime'] == "image/jpeg"){
  95. $success = ImageJpeg ($resized_img,$this->dest_dir.$ newName.'_'.$ext.$this->extensions[$size['mime']]);
  96. }elseif($size['mime'] == "image/x-png" | $newName .'_'.$ext.$this->extensions[$size['mime']]);
  97. | $size['mime'] == "image/png"){
  98. $success = ImagePNG ($resized_img,$this->dest_dir.$newName.'_'.$ext.$this-> 擴充[$ size['mime']]);
  99. }elseif($size['mime'] == "image/gif"){
  100. $success = ImageGIF ($resized_img,$this->; dest_dir.$ newName.'_'.$ext.$this->extensions[$size['mime']]);
  101. }
  102. // 刪除臨時映像
  103. ImageDestroy ($ resized_img);
  104. ImageDestroy ($new_img);
  105. return $success;
  106. }
  107. function getImage(){
  108. if( isset($_FILES[$this-image" && is_uploaded_file($_FILES[$this->image_field]['tmp_name']) ){
  109. $this->image_file = $_FILES[$this->image_field];
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. function returnImg(){
  116. return $this->image_file;
  117. }
  118. function getImageSize((MgetImage) $returnImg();
  119. return getimagesize($img['tmp_name']);
  120. }
  121. function checkImageSize( ){
  122. $size = $s ->getImageSize();
  123. if( $size[1] max_height )
  124. return false;
  125. return true;
  126. }
  127. 函數checkFileSize(){
  128. $img = $this->returnImg();
  129. if( $img['size'] > $this->max_size )
  130. return false;
  131. return true;
  132. }
  133. function checkType(){
  134. $img = $this->returnImg();
  135. $type = split('/',$img[' type']);
  136. if( !in_array( $type[ 1], $this->allowed_types ) )
  137. 回傳false;
  138. 回傳true;
  139. }
  140. functionthrow> function throwor ($val){
  141. switch($val){
  142. case 1: return '錯誤:檔案大小專業';
  143. break;
  144. case 2: return '錯誤:檔案格式不正確' ;
  145. break;
  146. 情況3:返回'錯誤:您的圖片太小';
  147. break;
  148. 情況4:返回'錯誤:創建圖片時出錯';
  149. break;
  150. }
  151. }
  152. }
?>
複製程式碼

圖片上傳,php


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn