php上傳檔案並添加文字與圖片浮水印的代碼

來源:互聯網
上載者:User
  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. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  32. {
  33. //是否存在檔案
  34. if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
  35. {
  36. echo "圖片不存在!";
  37. exit;
  38. }
  39. $file = $_FILES["upfile"];
  40. //檢查檔案大小

  41. if($max_file_size < $file["size"])

  42. {
  43. echo "檔案太大!不能超過2M!";
  44. exit;
  45. }

  46. //檢查檔案類型

  47. if(!in_array($file["type"], $uptypes))
  48. {
  49. echo "檔案類型不符!".$file["type"];
  50. exit;
  51. }

  52. //上傳到的檔案夾不存在則自動建立

  53. if(!file_exists($destination_folder))
  54. {
  55. mkdir($destination_folder);
  56. }

  57. $filename=$file["tmp_name"]; //系統自動產生的臨時檔案名稱

  58. $filenamecustom = $file["name"]; //使用者上傳的檔案名稱
  59. $image_size = getimagesize($filename); //映像大小
  60. $pinfo=pathinfo($file["name"]); //上傳檔案的路徑資訊
  61. $ftype=$pinfo['extension']; //上傳檔案的副檔名
  62. //$destination = $destination_folder.time().".".$ftype;//上傳檔案的目錄+檔案名稱+檔案類型,檔案名稱由time()產生
  63. $destination = $destination_folder.$filenamecustom.".".$ftype;//上傳檔案的目錄+使用者檔案名稱+檔案類型

  64. //檢查同名檔案是否存在

  65. if (file_exists($destination) && $overwrite != true)
  66. {
  67. echo "同名檔案已經存在了";
  68. exit;
  69. }

  70. //移動檔案到指定目錄

  71. if(!move_uploaded_file ($filename, $destination))
  72. {
  73. echo "移動檔案出錯";
  74. exit;
  75. }

  76. $pinfo=pathinfo($destination); //上傳到伺服器上的檔案的路徑資訊

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

  79. // echo " 已經成功上傳!
    檔案名稱: ".$destination."
    ";

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

  118. imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);//在nimage 映像中用white顏色畫一個左上方座標為1,$image_size[1]-15

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

  137. case 2:

  138. imagejpeg($nimage, $destination);
  139. break;

  140. case 3:

  141. imagepng($nimage, $destination);
  142. break;

  143. case 6:

  144. imagewbmp($nimage, $destination);
  145. //imagejpeg($nimage, $destination);
  146. break;
  147. }
  148. //釋放記憶體
  149. imagedestroy($nimage);
  150. imagedestroy($simage);
  151. }
  152. if($imgpreview==1)
  153. {
  154. echo "
    圖片預覽:
    ";
  155. echo " echo " alt=\"圖片預覽:\r檔案名稱:".$destination."\r上傳時間:\">";
  156. }
  157. }
  158. ?>
複製代碼

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.