搜尋
首頁後端開發php教程php上傳檔案並加入文字與圖片浮水印的程式碼

  1. /*************************************

  2. 參數說明:
  3. $max_file_size : 上傳檔案大小限制, 單位BYTE
  4. $destination_folder : 上傳檔案路徑
  5. $watermark : 是否附加浮水印(1為加水印,其他為不加水印);
  6. 使用說明:

  7. 1. 將PHP.INI檔案裡面的"extension=php_gd2.dll"一行前面的;號碼去掉,因為我們要用到GD函式庫;
  8. 2. 將extension_dir =改為你的php_gd2.dll所在目錄;
  9. *******************/
  10. //上傳檔案類型列表

  11. $uptypes=array(
  12. 'image/jpg',
  13. 'image/jpeg',
  14. 'image/png',
  15. 'image/pjpeg',
  16. 'image /gif',
  17. 'image/bmp',
  18. 'image/x-png'
  19. );
  20. $max_file_size=2000000; //大約2M,上傳檔案大小限制, 單位BYTE

  21. $destination_folder="uploadimg/"; //上傳檔案路徑
  22. $watermark=1; //是否附加水印(1為加水印,其他為不加浮水印);
  23. $watertype=1; //浮水印型別(1為文字,2為圖片)
  24. $waterposition=1; //浮水印位置(1為左下角,2為右下角,3為左上角,4為右上角,5為居中);
  25. $waterstring="JYS studio"; //水印字串
  26. $waterimg="xplore.gif";//水印圖片
  27. $imgpreview=1; //是否產生預覽圖(1為生成,其他為不生成);
  28. $imgpreviewsize=1/2; //縮圖比例
  29. ?>
  30. ZwelL圖片上傳程式
  31. 上傳檔案:

  32. 允許上傳的檔案類型為:=implode(', ',$uptypes)?>
  33. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  34. {
  35. //是否存在檔案
  36. if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
  37. {
  38. echo "圖片不存在!";
  39. exit;
  40. }
  41. $file = $_FILES["upfile"];
  42. //檢查檔案大小
  43. if($max_file_size {

  44. echo "文件太大!不能超過2M! ";
  45. exit;
  46. }
  47. //檢查檔案類型

  48. if(!in_array($file["type"], $uptypes))
  49. {
  50. echo "檔案類型不符!".$file["type"];
  51. exit;
  52. }
  53. //上傳到的資料夾不存在則自動建立

  54. if(!file_exists($destination_folder))
  55. {
  56. mkdir($destination_folder);
  57. }
  58. $filename=$file["tmp_name"]; //系統自動產生的暫存檔案名稱

  59. $filenamecustom = $file["name"]; //使用者上傳的檔案名稱
  60. $image_size = getimagesize($filename); //圖片大小
  61. $ pinfo=pathinfo($file["name"]); //上傳檔案的路徑資訊
  62. $ftype=$pinfo['extension']; //上傳檔案的副檔名
  63. //$destination = $ destination_folder.time().".".$ftype;//上傳檔案的目錄+檔案名稱+檔案類型,檔案名稱由time()產生
  64. $destination = $destination_folder.$filenamecustom.".".$ ftype;//上傳檔案的目錄+使用者檔案名稱+檔案類型
  65. //檢查同名檔案是否存在

  66. if (file_exists($destination) && $overwrite != true)
  67. {
  68. echo "同名檔案已經存在了";
  69. exit;
  70. }
  71. //移動檔案到指定目錄

  72. if(!move_uploaded_file ($uploaded_file ($ filename, $destination))
  73. {
  74. echo "移動檔案出錯";
  75. exit;
  76. }
  77. $pinfo=pathinfo($destination); //上傳到伺服器上的檔案的路徑資訊

  78. $fname=$pinfo[basename]; //上傳到伺服器上的檔案名稱
  79. // echo " 已經成功上傳!
    檔名: ".$destination_folder.$fname."
    ";
  80. // echo " 已經成功上傳!
    檔名: ".$destination."
    ";

  81. echo " 已經成功上傳!
    檔名: ".$destination_folder.$filenamecustom."
    ";
  82. echo " 寬度:".$image_size[0];
  83. echo " 長度:".$image_size[1];
  84. echo "
    大小:".$file["size"]." bytes";
  85. if($watermark==1)
  86. {
  87. $iinfo=getimagesize($destination,$iinfo); //取得圖片大小、類型
  88. $nimage=imagecreatetruecolor($image_size[0],$image_size[1]); //新建一個真彩色圖像,傳回一個圖像標識符,
  89. //代表了一幅大小為x_size 和y_size 的黑色圖像。 $black=imagecolorallocate($nimage,0,0,0);
  90. $red=imagecolorallocate($nimage,255,0,0);
  91. imagefill($nimage,0,0,$white); / /在nimage 影像的座標x,y(影像左上角為0, 0)處以color 顏色執行區域填入
  92. //(即與x, y 點顏色相同且相鄰的點都會被填滿)。 🎜> /*
  93. 1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),
  94. 9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM
  95. */
  96. switch ($iinfo[2])
  97. {
  98. case 1:
  99. $simage =imagecreatefromgif($destination); //從給定的檔案名稱取得的圖片
  100. break;
  101. case 2:
  102. $simage =imagecreatefromjpeg ($destination);
  103. break;
  104. case 3:
  105. $simage =imagecreatefrompng($destination);
  106. break;
  107. case 6:
  108. $simage =imagecreatefromwpmp($dede) ;
  109. break;
  110. default:
  111. die("不支援的檔案類型"); //Equivalent to exit()
  112. exit;
  113. }
  114. imagecopy($nimage,$ simage,0,0,0,0,$image_size[0],$image_size[1]);//將simage從0,0開始,$image_size[0]寬、$image_size[1]高
  115. / /的部分拷貝到nimage中座標為0,0的位置上
  116. imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white );//在nimage 圖片中用white顏色畫一個左上角座標為1,$image_size[1]-15

  117. //右下角座標為80,$image_size[1]的矩形
  118. switch($水印字串
  119. {
  120. case 1: //加浮水印字串
  121. imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);//用黑色將waterstring畫到nimage的3,$image_size[1]-15座標處,字體為內建字體2
  122. break;
  123. case 2: //加水印圖片
  124. $simage1 =imagecreatefromgif("xplore. ");
  125. imagecopy($nimage,$simage1,0,0,0,0,85,15);
  126. imagedestroy($simage1); //釋放與simagel 相關的記憶體
  127. break;
  128. }
  129. switch ($iinfo[2])
  130. {
  131. case 1:
  132. //imagegif($nimage, $destination);
  133. imagejpeg($nimage, $destination); //從nimage 映像以destination 為檔案名稱建立一個JPEG 影像。 nimage 參數是 imagecreatetruecolor() 函數的回傳值。
  134. break;
  135. case 2:

  136. imagejpeg($nimage, $destination);
  137. break;
  138. case 3:

  139. imagepng($nimage, $destination);
  140. break;
  141. case 6:

  142. imagewbmp($nimage, $destination);
  143. //imagejpeg($nimage, $destination);
  144. break;
  145. }
  146. //釋放記憶體
  147. imagedestroy($nimage);
  148. imagedestroy($simage);
  149. }
  150. if($imgpreviewif($imgpreview =1)
  151. {
  152. echo "
    圖片預覽:
    ";
  153. echo "php上傳檔案並加入文字與圖片浮水印的程式碼";
  154. }
  155. }
  156. ?>
  157. 複製程式碼



陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP如何識別用戶的會話?PHP如何識別用戶的會話?May 01, 2025 am 12:23 AM

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

確保PHP會議的一些最佳實踐是什麼?確保PHP會議的一些最佳實踐是什麼?May 01, 2025 am 12:22 AM

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

PHP會話文件默認存儲在哪裡?PHP會話文件默認存儲在哪裡?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

您如何從PHP會話中檢索數據?您如何從PHP會話中檢索數據?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

您如何使用會議來實施購物車?您如何使用會議來實施購物車?May 01, 2025 am 12:10 AM

利用會話構建高效購物車系統的步驟包括:1)理解會話的定義與作用,會話是服務器端的存儲機制,用於跨請求維護用戶狀態;2)實現基本的會話管理,如添加商品到購物車;3)擴展到高級用法,支持商品數量管理和刪除;4)優化性能和安全性,通過持久化會話數據和使用安全的會話標識符。

您如何在PHP中創建和使用接口?您如何在PHP中創建和使用接口?Apr 30, 2025 pm 03:40 PM

本文解釋瞭如何創建,實施和使用PHP中的接口,重點關注其對代碼組織和可維護性的好處。

crypt()和password_hash()有什麼區別?crypt()和password_hash()有什麼區別?Apr 30, 2025 pm 03:39 PM

本文討論了PHP中的crypt()和password_hash()的差異,以進行密碼哈希,重點介紹其實施,安全性和對現代Web應用程序的適用性。

如何防止PHP中的跨站點腳本(XSS)?如何防止PHP中的跨站點腳本(XSS)?Apr 30, 2025 pm 03:38 PM

文章討論了通過輸入驗證,輸出編碼以及使用OWASP ESAPI和HTML淨化器之類的工具來防止PHP中的跨站點腳本(XSS)。

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

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

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Safe Exam Browser

Safe Exam Browser

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

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具