PHP 圖片上傳類並產生縮圖

來源:互聯網
上載者:User
  1. /**
  2. * 上傳圖片
  3. */
  4. class imgUpload{
  5. static protected $a;
  6. protected $formName; //表單名稱
  7. protected $directory; //檔案上傳至目錄
  8. protected $maxSize; //最大檔案上傳大小
  9. protected $canUpload; //是否可以上傳
  10. protected $doUpFile; //上傳的檔案名稱
  11. protected $sm_File; //縮圖名稱
  12. private function __construct($_formName='file', $_directory='./images/uploads/', $_maxSize=1048576){ //1024*1024=1M
  13. //初始化參數
  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. //判斷圖片是否屬於允許格式內
  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. //擷取檔案大小
  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. //擷取檔案類型
  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. //擷取檔案名稱
  73. public function getName(){
  74. if ($this->canUpload) {
  75. return $_FILES[$this->formName]['name'];
  76. }
  77. }
  78. //建立檔案名稱
  79. public function newName(){
  80. return date('YmdHis').rand(0,9);
  81. }
  82. //上傳檔案
  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. //建立縮圖
  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. //得到圖片資訊數組
  111. $_date = getimagesize($_srcImage, &$info);
  112. $src_w = $_date[0]; //源圖片寬
  113. $src_h = $_date[1]; //源圖片高
  114. $src_max_len = max($src_w, $src_h); //求得長邊
  115. $src_min_len = min($src_w, $src_h); //求得短邊
  116. $dst_w = ''; //靶心圖表片寬
  117. $dst_h = ''; //靶心圖表片高
  118. //寬高按比例縮放,最長邊不大於$_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. //建立縮圖時,源圖片的位置
  139. $src_x = '';
  140. $src_y = '';
  141. //判斷如果縮圖用於logo,將對其進行裁減
  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. //判斷圖片類型並建立對應新圖片
  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. //建立一幅新映像
  164. if ($_date[2]==1) { //gif無法應用imagecreatetruecolor
  165. $dst_im = imagecreate($dst_w, $dst_h);
  166. }else {
  167. $dst_im = imagecreatetruecolor($dst_w, $dst_h);
  168. }
  169. //對這副映像進行縮圖copy
  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. //對圖片進行消除鋸齒操作
  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有問題,所以在這裡用imagejpg代替
  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. //得到上傳後的檔案名稱
  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. //得到上傳後的檔案全路徑
  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. //得到縮圖檔案全路徑
  215. public function getThumb(){
  216. if ($this->sm_File!='') {
  217. return $this->sm_File;
  218. }else {
  219. return false;
  220. }
  221. }
  222. //得到上傳檔案的路徑
  223. public function getDirectory(){
  224. if ($this->directory!='') {
  225. return $this->directory;
  226. }else {
  227. return false;
  228. }
  229. }
  230. }
  231. ?>
複製代碼
圖片上傳, PHP
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.