搜尋
首頁後端開發php教程PHP 圖片上傳並產生縮圖類

  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
unset()和session_destroy()有什麼區別?unset()和session_destroy()有什麼區別?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

在負載平衡的情況下,什麼是粘性會話(會話親和力)?在負載平衡的情況下,什麼是粘性會話(會話親和力)?May 04, 2025 am 12:16 AM

stickysessensureuserRequestSarerOutedTothesMeServerForsessionDataConsisterency.1)sessionIdentificeAssificationAssigeaSsignAssignSignSuserServerServerSustersusiseCookiesorUrlModifications.2)一致的ententRoutingDirectSsssssubsequeSssubsequeSubsequestrequestSameSameserver.3)loadBellankingDisteributesNebutesneNewuserEreNevuseRe.3)

PHP中有哪些不同的會話保存處理程序?PHP中有哪些不同的會話保存處理程序?May 04, 2025 am 12:14 AM

phpoffersvarioussessionsionsavehandlers:1)文件:默認,簡單的ButMayBottLeneckonHigh-trafficsites.2)Memcached:高性能,Idealforsforspeed-Criticalapplications.3)REDIS:redis:similartomemememememcached,withddeddeddedpassistence.4)withddeddedpassistence.4)databases:gelifforcontrati forforcontrati,有用

PHP中的會話是什麼?為什麼使用它們?PHP中的會話是什麼?為什麼使用它們?May 04, 2025 am 12:12 AM

PHP中的session是用於在服務器端保存用戶數據以在多個請求之間保持狀態的機制。具體來說,1)session通過session_start()函數啟動,並通過$_SESSION超級全局數組存儲和讀取數據;2)session數據默認存儲在服務器的臨時文件中,但可通過數據庫或內存存儲優化;3)使用session可以實現用戶登錄狀態跟踪和購物車管理等功能;4)需要注意session的安全傳輸和性能優化,以確保應用的安全性和效率。

說明PHP會話的生命週期。說明PHP會話的生命週期。May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

絕對會話超時有什麼區別?絕對會話超時有什麼區別?May 03, 2025 am 12:21 AM

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

如果會話在服務器上不起作用,您將採取什麼步驟?如果會話在服務器上不起作用,您將採取什麼步驟?May 03, 2025 am 12:19 AM

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()函數的意義是什麼?session_start()函數的意義是什麼?May 03, 2025 am 12:18 AM

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。