首頁  >  文章  >  後端開發  >  PHP 圖片上傳並產生縮圖類

PHP 圖片上傳並產生縮圖類

WBOY
WBOY原創
2016-07-25 08:45:21895瀏覽
  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. }
  65. }
  66. //取得檔案類型
  67. public function getExt(){
  68. if ($this->canUpload) {
  69. $_name = $_FILES[$this->formName]['name'];
  70. $_nameArr = explode('.',$_name);
  71. $_count = count($_nameArr)-1;
  72. }
  73. return $_nameArr[$_count];
  74. }
  75. //取得檔案名稱
  76. public function getName(){
  77. if ($this->canUpload) {
  78. return $_FILES[$this->formName]['name'];
  79. }
  80. }
  81. //新檔案名稱
  82. public function newName(){
  83. return date('YmdHis').rand(0,9);
  84. }
  85. //上傳檔案
  86. public function upload(){
  87. if ($this->canUpload)
  88. {
  89. $_getSize = $this->getSize('B');
  90. if (!$_getSize) { return $_getSize; break; }
  91. else
  92. {
  93. $_newName = $this->newName();
  94. $_ext = $this->getExt();
  95. $_doUpload = move_uploaded_file($_FILES[$ this- >formName]['tmp_name'], $this->directory.$_newName.".".$_ext);
  96. if ($_doUpload)
  97. {
  98. $this->; doUpFile = $_newName ;
  99. }
  100. return $_doUpload;
  101. }
  102. }
  103. }
  104. //建立小心
  105. public functionthumb($_dstChar=' _m', $max_len=m', $max_len= 320){ //$_dstChar:_m , _s
  106. if ($this->canUpload && $this->doUpFile != "") {
  107. $_ext = $this- >getExt();
  108. $_srcImage = $this->directory.$this->doUpFile.".".$_ext;
  109. //取得圖片資訊流量
  110. $_date = getimagesize($_srcImage, &$info);
  111. $src_w = $_date[0]; //來源圖片寬
  112. $src_h = $_date[1]; //來源圖片高
  113. $src_max_len = max($src_w, $src_h ); //求長邊
  114. $src_min_len = min($src_w, $src_h); //求短邊
  115. $dst_w = ''; //目標圖片寬
  116. $dst_h = ' '; //目標圖
  117. //寬高依縮放比例,寬度不大於$_max_len
  118. if ($src_max_len>$_max_len)
  119. {
  120. $percent = $src_min_len / $ src_max_len;
  121. if ($src_w == $src_max_len)
  122. {
  123. $dst_w = $_max_len;
  124. $dst_h = $percent * $dst_w;
  125. }
  126. {
  127. $dst_h = $_max_len;
  128. $dst_w = $percent * $dst_h;
  129. }
  130. }
  131. else
  132. {
  133. $dst_w = $srcww; $dst_h = $src_h;
  134. }
  135. //建立了幾個時間、來源圖片的位置
  136. $src_x = '';
  137. $src_y = '';
  138. //判斷如果要用於logo,則對其進行裁減
  139. if ('s_' == $_dstChar) {
  140. $src_x = $src_w * 0.10;
  141. $src_y = $src_h * 0.10;
  142. $src_w *= 0.8;
  143. $src_h *= 0.8;
  144. }
  145. //判斷圖片類型並建立對應新圖片
  146. switch ($_date[2 ] ){
  147. 情況1:
  148. $src_im = imagecreatefromgif($_srcImage);
  149. 中斷;
  150. 情況2:
  151. $src_im = imagecreatefromjpeg($srcImage); > 案例3:
  152. $src_im = imagecreatefrompng($_srcImage);
  153. 中斷;
  154. 案例8:
  155. $src_im = imagecreatefromwbmp($_srcImage);
  156. //建立一張新圖片
  157. if ($_date[2]==1) { //gif 無法套用imagecreatetruecolor
  158. $dst_im = imagecreate($dst_w, $dst_h);
  159. }else {
  160. $dst_im = imagecreatetruecolor($dst_w, $dst_h);
  161. }
  162. //對這副圖像進行縮圖copy
  163. // $bg = imagecolorallocate($dst_im,255,255,0);
  164. imagecopyresized($dst_im,$src_im,$src0, $0, $0, $ src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
  165. //對圖片進行抗鋸齒操作
  166. imageantialias($dst_im, true);
  167. case 1:
  168. $cr = imagegif($dst_im, $this->directory.$this->doUpFile.$_dstChar.".".$_ext, 100);
  169. break;
  170. case 2:
  171. $cr = imagejpeg($dst_im, $this->directory.$this->doUpFile.$_dstChar.".".$_ext, 100);
  172. break;
  173. case 3://imagepng有問題,所以在這裡用imagejpg代替
  174. $cr = imagejpeg($dst_im, $this->directory.$this->doUpFile.$_dstChar.".".$_ext, 100);
  175. break;
  176. }
  177. // $cr = imagejpeg($dst_im, $this->directory.$_dstChar.$this->doUpFile, 90);
  178. if ($cr ) {
  179. $this->sm_File = $this->directory.$this->doUpFile.$_dstChar.".".$_ext;
  180. return $this->sm_File;
  181. } else {
  182. return false;
  183. }
  184. }
  185. imagedestroy($dst_im);
  186. imagedestroy($cr);
  187. }
  188. //得到上傳後的檔名
  189. public function getUpFile(){
  190. if ($this->doUpFile!='') {
  191. $_ext = $this->getExt();
  192. return $this->doUpFile .".".$_ext;
  193. }else {
  194. return false;
  195. }
  196. }
  197. //得到上傳後的檔案全路徑
  198. public function getFilePatch( ){
  199. if ($this->doUpFile!='') {
  200. $_ext = $this->getExt();
  201. return $this->directory.$this->doUpFile.". ".$_ext;
  202. }else {
  203. return false;
  204. }
  205. }
  206. //得到縮圖檔案全路徑
  207. public function getThumb(){
  208. if ($this->sm_File!='') {
  209. return $this->sm_File;
  210. }else {
  211. return false;
  212. }
  213. }
  214. //得到上傳檔案的路徑
  215. public function getDirectory(){
  216. if ($this->directory!='') {
  217. return $this->directory;
  218. }else {
  219. return false ;
  220. }
  221. }
  222. }
  223. ?>
複製程式碼

圖片上傳, PHP

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