国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 後端開發(fā) php教程 PHP圖片上傳類別;支援浮水印-日期資料夾-產(chǎn)生縮圖 ,支援多檔案上傳

PHP圖片上傳類別;支援浮水印-日期資料夾-產(chǎn)生縮圖 ,支援多檔案上傳

Jul 25, 2016 am 08:46 AM

} }
可以用{Y}{m}{n}變成現(xiàn)在的日期
  1. set_dir(dirname(__FILE__).'/upload/','{y}/{m} '); //儲存路徑,支援{y}{m}377j5v51b這幾個選項
  2. $up->set_thumb(100,80); //縮圖大小設(shè)定.單位為像素
  3. $ up->set_watermark(dirname(__FILE__).'/jblog/images/watermark.png',6,90); //水印設(shè)定
  4. $fs = $up->execute(); //開始執(zhí)行
  5. var_dump($fs); //測試用查看類別的情況
  6. }
  7. ?>
  8. /////視圖表單---------
  9. test
  10. //支援多張圖片上傳
  11. */
  12. upload {
  13. var $ dir; //附件存放實體目錄
  14. var $time; //自訂檔案上傳時間
  15. var $allow_types; //允許上傳附件類型
  16. var $field; //上傳控制項名稱
  17. var $maxsize; //最大允許檔案大小,單位為KB
  18. var $thumb_width; //縮圖寬度
  19. var $thumb_height; //縮圖高度
  20. var $watermark_file ; //浮水印圖片位址
  21. var $watermark_pos; //水印位置
  22. var $watermark_trans;//水印透明度
  23. //建構(gòu)子
  24. //$types : 允許上傳的檔案類型, $maxsize : 允許大小, $field : 上傳控制項名稱, $time : 自訂上傳時間
  25. function upload($types = 'jpg|png', $maxsize = 1024, $field = 'attach', $ time = '') {
  26. $this->allow_types = explode('|',$types);
  27. $this->maxsize = $maxsize * 1024;
  28. $this->field = $field ;
  29. $this->time = $time ? $time : time();
  30. }
  31. //設(shè)定並建立檔案特定存放的目錄
  32. //$basedir : 基底目錄,必須為實體路徑
  33. //$filedir : 自訂子目錄,可用參數(shù){y}、{m}、377j5v51b
  34. function set_dir($basedir,$filedir = '') {
  35. $dir = $basedir;
  36. !is_dir($dir) && @mkdir($dir,0777);
  37. if (!empty($filedir)) {
  38. $filedir = str_replace(array($filedir)) {
  39. $filedir = str_replace(array($) y}','{m}','377j5v51b'),array(date('Y',$this->time),date('m',$this->time),date('d' ,$this->time)),strtolower($filedir));//用string_replace把{y} {m} 377j5v51b幾個標籤進行替換
  40. $dirs = explode('/',$filedir) ;
  41. foreach ($dirs as $d) {
  42. !empty($d) && $dir .= $d.'/';
  43. !is_dir($dir) && @mkdir($dir, 0777);
  44. }
  45. }
  46. $this->dir = $dir;
  47. }
  48. //圖片縮圖設(shè)置,如果不產(chǎn)生縮圖則不用設(shè)定
  49. //$width : 縮圖寬度, $height : 縮圖高度
  50. function set_thumb ($width = 0, $height = 0) {
  51. $this->thumb_width = $width;
  52. $this
  53. $this
  54. $this
  55. $this
  56. $this
  57. $this
  58. $this
  59. $this $this $this $this $this $this $this $this $this $this thumb_height = $height; } //圖片浮水印設(shè)置,如果不產(chǎn)生添加水印則不用設(shè)置 //$file : 水印圖片, $pos : 水印位置, $trans : 水印透明度 function set_watermark ($file, $pos = 6, $trans = 80) { $this->watermark_file = $file; $this->watermark_pos = $pos; $this->watermark_trans = $trans; }
  60. /*—————————————————————-
  61. 執(zhí)行檔上傳,處理完返回一個包含上傳成功或失敗的檔案資訊數(shù)組,
  62. 其中:name 為檔案名,上傳成功時是上傳到伺服器上的檔案名,上傳失敗則是本地的檔案名稱
  63. dir 為伺服器上存放該附件的實體路徑,上傳失敗則是本地的檔案名稱
  64. dir 為伺服器上存放該附件的實體路徑,上傳失敗不存在該值
  65. size 為附件大小,上傳失敗不存在該值
  66. flag 為狀態(tài)標識,1表示成功,-1表示文件類型不允許,-2表示文件大小超出
  67. —————— ———————————————–*/
  68. function execute() {
  69. $files = array(); //成功上傳的檔案資訊
  70. $field = $ this->field;
  71. $keys = array_keys($_FILES[$field]['name']);
  72. foreach ($keys as $key) {
  73. if (!$_FILES[$field] ['name'][$key]) continue;
  74. $fileext = $this->fileext($_FILES[$field]['name'][$key]); //取得檔案副檔名
  75. $filename = date('Ymdhis',$this->time).mt_rand(10,99).'.'.$fileext; //產(chǎn)生檔名
  76. $filedir = $this->dir; //附件實際存放目錄
  77. $filesize = $_FILES[$field]['size'][$key]; //檔案大小
  78. //檔案類型不允許
  79. if (! in_array($fileext,$this->allow_types)) {
  80. $files[$key]['name'] = $_FILES[$field]['name'][$key];
  81. $files[ $key]['flag'] = -1;
  82. continue;
  83. }
  84. //檔案大小超出
  85. if ($filesize > $this->maxsize) {
  86. $files[$key]['name'] = $_FILES[$field]['name'][$key];
  87. $files[$key]['name'] = $filesize;
  88. $ files[$key]['flag'] = -2;
  89. continue;
  90. }
  91. $files[$key]['name'] = $filename;
  92. $files[ $key]['dir'] = $filedir;
  93. $files[$key]['size'] = $filesize;
  94. //儲存上傳檔案並刪除暫存檔案
  95. if ( is_uploaded_file($_FILES[$field]['tmp_name'][$key])) {
  96. move_uploaded_file($_FILES[$field]['tmp_name'][$key],$filedir.$filename);
  97. @unlink($_FILES[$field]['tmp_name'][$key]);
  98. $files[$key]['flag'] = 1;
  99. //對圖片加浮水印和產(chǎn)生縮圖,這裡示範只支援jpg和png(gif產(chǎn)生的話會沒了幀的)
  100. if (in_array($fileext,array('jpg','png'))) {
  101. if ($this->thumb_width) {
  102. if ($this->create_thumb($filedir.$filename,$filedir.'thumb_'.$filename)) {
  103. $files[$key]['thumb' ] = 'thumb_'.$filename; //縮圖檔名
  104. }
  105. }
  106. $this->create_watermark($filedir.$filename);
  107. }
  108. }
  109. }
  110. }
  111. return $files;
  112. }
  113. //建立縮圖,以相同的副檔名產(chǎn)生縮圖
  114. //$src_file : 來源映像路徑, $thumb_file :縮圖路徑
  115. function create_thumb ($src_file,$thumb_file) {
  116. $t_width = $this->thumb_width;
  117. $t_height = $this->thumb_height;width;
  118. $t_height = $this->thumb_height; ($src_file)) return false;
  119. $src_info = getImageSize($src_file);
  120. //如果來源影像小於或等於縮圖則拷貝來源影像作為縮圖,免去操作
  121. if ($src_info[0] if (!copy($src_file,$thumb_file)) {
  122. return false;
  123. }
  124. return true;
  125. }
  126. //按比例計算縮圖大小
  127. if (($src_info[0]-$t_width) > ($src_info[1]-$ t_height)) {
  128. $t_height = ($t_width / $src_info[0]) * $src_info[1];
  129. } else {
  130. $t_width = ($t_height / $src_info[1]) * $src_info[0]; }
  131. //取得檔案副檔名
  132. $fileext = $this->fileext($src_file);
  133. switch ($fileext) {
  134. case 'jpg' :
  135. $src_img = ImageCreateFromJPEG($src_file); break;
  136. case 'png' :
  137. $src_img = ImageCreateFromPNG($src_file); break;
  138. case 'frem; $src_file); break;
  139. }
  140. //建立一個真彩色的縮圖圖
  141. $thumb_img = @ImageCreateTrueColor($t_width,$t_height);
  142. // ImageCopyResampled函數(shù)拷貝的圖像平滑度較好,優(yōu)先考慮
  143. if (function_exists('imagecopyresampled')) {
  144. @ImageCopyResampled($thumb_img,$src_img,0,0,0,000,0,0$ ,$src_info[0],$src_info[1]);
  145. } else {
  146. @ImageCopyResized($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[000 ],$src_info[1]);
  147. }
  148. //產(chǎn)生縮圖
  149. switch ($fileext) {
  150. case 'jpg' :
  151. ImageJPEG($thumb_img,$ thumb_file); break;
  152. case 'gif' :
  153. ImageGIF($thumb_img,$thumb_file); break;
  154. case 'png' :
  155. ImagePNG($thumb_breakimg,$thumb_breakimg, }
  156. //銷毀臨時圖像
  157. @ImageDestroy($src_img);
  158. @ImageDestroy($thumb_img);
  159. return true;
  160. //為圖片加上浮水印
  161. //$file : 要加入浮水印的檔案
  162. function create_watermark ($file) {
  163. //檔案不存在則回傳
  164. if (!file_exists($this->watermark_file) || !file_exists($file)) return;
  165. if (!function_exists('getImageSize')) return;
  166. //GD檢查支援的檔案類型
  167. $gd_allow_types = array();
  168. if (function_exists('ImageCreateFromGIF')) $gd_allow_types['image/gif'] = 'ImageCreateFromGIF'; if (function_exists('ImageCreateFromJPEG')) $gd_allow_types['image/jpeg'] = 'ImageCreate> 取得檔位;訊息
  169. $fileinfo = getImageSize($file);
  170. $wminfo = getImageSize($this->watermark_file);
  171. if ($fileinfo[0]
  172. if (array_key_exists($fileinfo['mime'],$gd_allow_types)) {
  173. if (array_key_exists($wminfo['mime'] ,$gd_allow_types)) {
  174. //從檔案建立映像
  175. $temp = $gd_allow_types[$fileinfo['mime']]($file);
  176. $temp_wm = $gd_allow_types[$ wminfo['mime']]($this->watermark_file);
  177. //水印位置
  178. switch ($this->watermark_pos) {
  179. case 1 : //頂部居左
  180. $dst_x = 0; $dst_y = 0; break;
  181. case 2 : //頂部居中
  182. $dst_x = ($fileinfo[0] - $wminfo[0])/2; $dst_y = 0; break ;
  183. case 3 : //頂部居右
  184. $dst_x = $fileinfo[0]; $dst_y = 0; break;
  185. case 4 : //底部居左
  186. $dst_x = 0; $ dst_y = $fileinfo[1]; break;
  187. case 5 : //底部居中
  188. $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = $fileinfo[1] ; break;
  189. case 6 : //底部居右邊
  190. $dst_x = $fileinfo[0]-$wminfo[0]; $dst_y = $fileinfo[1]-$wminfo[1]; break;
  191. default : //隨機
  192. $dst_x = mt_rand(0,$fileinfo[0]-$wminfo[0]); $dst_y = mt_rand(0,$fileinfo[1]-$wminfo[1]);
  193. }
  194. if (function_exists('ImageAlphaBlending')) ImageAlphaBlending($temp_wm,True); //設(shè)定影像的混色模式
  195. if (function_exists('ImaveAlalphas('ImaveSphaists('ImaveSphaists('Im))Spha; temp_wm,True); //儲存完整的alpha 頻道資訊
  196. //為影像新增浮水印
  197. if (function_exists('imageCopyMerge')) {
  198. ImageCopyMerge($temp,$temp_wm,$$temp_wm,$ dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1],$this->watermark_trans);
  199. }else {
  200. ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1]);
  201. }
  202. //儲存圖片
  203. switch ($fileinfo['mime']) {
  204. case 'image/jpeg' :
  205. @imageJPEG($temp,$file);
  206. break;
  207. case 'image/png ' :
  208. @imagePNG($temp,$file);
  209. break;
  210. case 'image/gif' :
  211. @imageGIF($temp,$file);
  212. break;
  213. }
  214. //銷毀零時圖片
  215. @imageDestroy($temp);
  216. @imageDestroy($temp_wm);
  217. }
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }
}
} }
}
}
/ /取得檔案副檔名
function fileext($filename) {
return strtolower(substr(strrchr($filename,'.'),1,10));
} }
?>
複製程式碼

圖片上傳, PHP 本主題由 小貝 於 2015-11-18 08:23 移動
本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔相應(yīng)的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

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

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

對基於PHP的API進行版本控制的最佳實踐是什麼? 對基於PHP的API進行版本控制的最佳實踐是什麼? Jun 14, 2025 am 12:27 AM

基於toversionaphp,useUrl deuseUrl specteringforclarityAndEsofRouting,單獨的codetoavoidConflicts,dremecateOldVersionswithClearCommunication,andConsiderCustomHeadeSerlySerallyWhennEnncelsy.startbyplacingtheversionIntheUrl(E.G.,epi/api/v

如何在PHP中實施身份驗證和授權(quán)? 如何在PHP中實施身份驗證和授權(quán)? Jun 20, 2025 am 01:03 AM

tosecurelyhandleauthenticationandationallizationInphp,lofterTheSesteps:1.AlwaysHashPasswordSwithPassword_hash()andverifyusingspasspassword_verify(),usepreparedStatatementStopreventsqlineptions,andStoreSeruserDatain usseruserDatain $ _sessiveferterlogin.2.implementrole-2.imaccessccsccccccccccccccccccccccccc.

PHP中的程序和麵向?qū)ο蟮木幊坦犂g有什麼區(qū)別? PHP中的程序和麵向?qū)ο蟮木幊坦犂g有什麼區(qū)別? Jun 14, 2025 am 12:25 AM

procemal and object-tiriendedprogromming(oop)inphpdiffersimplessintustructure,可重複使用性和datahandling.1.procedural-Progrogursmingusesfunctimesfunctionsormanized sequalized sequalized sequiential,poiperforsmallscripts.2.OpporganizesCodeOrganizescodeOdeIntsocloceSandObjects,ModelingReal-Worlden-Worlden

PHP中有哪些弱參考(弱圖),何時有用? PHP中有哪些弱參考(弱圖),何時有用? Jun 14, 2025 am 12:25 AM

PHPdoesnothaveabuilt-inWeakMapbutoffersWeakReferenceforsimilarfunctionality.1.WeakReferenceallowsholdingreferenceswithoutpreventinggarbagecollection.2.Itisusefulforcaching,eventlisteners,andmetadatawithoutaffectingobjectlifecycles.3.YoucansimulateaWe

如何在PHP中安全地處理文件上傳? 如何在PHP中安全地處理文件上傳? Jun 19, 2025 am 01:05 AM

要安全處理PHP中的文件上傳,核心在於驗證文件類型、重命名文件並限制權(quán)限。 1.使用finfo_file()檢查真實MIME類型,僅允許特定類型如image/jpeg;2.用uniqid()生成隨機文件名,存儲至非Web根目錄;3.通過php.ini和HTML表單限製文件大小,設(shè)置目錄權(quán)限為0755;4.使用ClamAV掃描惡意軟件,增強安全性。這些步驟有效防止安全漏洞,確保文件上傳過程安全可靠。

PHP中==(鬆散比較)和===(嚴格的比較)之間有什麼區(qū)別? PHP中==(鬆散比較)和===(嚴格的比較)之間有什麼區(qū)別? Jun 19, 2025 am 01:07 AM

在PHP中,==與===的主要區(qū)別在於類型檢查的嚴格程度。 ==在比較前會進行類型轉(zhuǎn)換,例如5=="5"返回true,而===要求值和類型都相同才會返回true,例如5==="5"返回false。使用場景上,===更安全應(yīng)優(yōu)先使用,==僅在需要類型轉(zhuǎn)換時使用。

如何與PHP的NOSQL數(shù)據(jù)庫(例如MongoDB,Redis)進行交互? 如何與PHP的NOSQL數(shù)據(jù)庫(例如MongoDB,Redis)進行交互? Jun 19, 2025 am 01:07 AM

是的,PHP可以通過特定擴展或庫與MongoDB和Redis等NoSQL數(shù)據(jù)庫交互。首先,使用MongoDBPHP驅(qū)動(通過PECL或Composer安裝)創(chuàng)建客戶端實例並操作數(shù)據(jù)庫及集合,支持插入、查詢、聚合等操作;其次,使用Predis庫或phpredis擴展連接Redis,執(zhí)行鍵值設(shè)置與獲取,推薦phpredis用於高性能場景,Predis則便於快速部署;兩者均適用於生產(chǎn)環(huán)境且文檔完善。

如何在PHP( - , *, /,%)中執(zhí)行算術(shù)操作? 如何在PHP( - , *, /,%)中執(zhí)行算術(shù)操作? Jun 19, 2025 pm 05:13 PM

PHP中使用基本數(shù)學(xué)運算的方法如下:1.加法用 號,支持整數(shù)和浮點數(shù),也可用於變量,字符串數(shù)字會自動轉(zhuǎn)換但不推薦依賴;2.減法用-號,變量同理,類型轉(zhuǎn)換同樣適用;3.乘法用*號,適用於數(shù)字及類似字符串;4.除法用/號,需避免除以零,並註意結(jié)果可能是浮點數(shù);5.取模用%號,可用於判斷奇偶數(shù),處理負數(shù)時餘數(shù)符號與被除數(shù)一致。正確使用這些運算符的關(guān)鍵在於確保數(shù)據(jù)類型清晰並處理好邊界情況。

See all articles