php圖片浮水印代碼,php上傳圖片加浮水印執行個體代碼

來源:互聯網
上載者:User
  1. //上傳檔案類型列表
  2. $uptypes=array(
  3. 'image/jpg',
  4. 'image/jpeg',
  5. 'image/png',
  6. 'image/pjpeg',
  7. 'image/gif',
  8. 'image/bmp',
  9. 'image/x-png'
  10. );
  11. $max_file_size=2000000; //上傳檔案大小限制, 單位BYTE
  12. $destination_folder="uploadimg/"; //上傳檔案路徑
  13. $watermark=1; //是否附加浮水印(1為加浮水印,其他為不加浮水印);
  14. $watertype=1; //浮水印類型(1為文字,2為圖片)
  15. $waterposition=1; //浮水印位置(1為左下角,2為右下角
  16. ,3為左上方,4為右上方,5為置中);
  17. $waterstring="http://bbs.it-home.org/"; //浮水印字串
  18. $waterimg="xplore.gif"; //浮水印圖片
  19. $imgpreview=1; //是否產生預覽圖(1為產生,其他為不產生);
  20. $imgpreviewsize=1/2; //縮圖比例
  21. ?>
  22. ZwelL圖片上傳程式
  23. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  24. {
  25. if (!is_uploaded_file($_FILES["upfile"]
  26. [tmp_name]))
  27. //是否存在檔案
  28. {
  29. echo "圖片不存在!";
  30. exit;
  31. }
  32. $file = $_FILES["upfile"];
  33. if($max_file_size <$file["size"])
  34. //檢查檔案大小
  35. {
  36. echo "檔案太大!";
  37. exit;
  38. }
  39. if(!in_array($file["type"], $uptypes))
  40. //檢查檔案類型
  41. {
  42. echo "檔案類型不符!".$file["type"];
  43. exit;
  44. }
  45. if(!file_exists($destination_folder))
  46. {
  47. mkdir($destination_folder);
  48. }
  49. $filename=$file["tmp_name"];
  50. $image_size = getimagesize($filename);
  51. $pinfo=pathinfo($file["name"]);
  52. $ftype=$pinfo['extension'];
  53. $destination = $destination_folder.
  54. time().".".$ftype;
  55. if (file_exists($destination) &&
  56. $overwrite != true)
  57. {
  58. echo "同名檔案已經存在了";
  59. exit;
  60. }
  61. if(!move_uploaded_file ($filename,
  62. $destination))
  63. {
  64. echo "移動檔案出錯";
  65. exit;
  66. }
  67. $pinfo=pathinfo($destination);
  68. $fname=$pinfo[basename];
  69. echo " 已經成功上傳

  70. 檔案名稱:
  71. ".$destination_folder.
  72. $fname."
    ";
  73. echo " 寬度:".$image_size[0];
  74. echo " 長度:".$image_size[1];
  75. echo "
    大小:".$file["size"]." bytes";
  76. if($watermark==1)
  77. {
  78. $iinfo=getimagesize($destination,$iinfo);
  79. $nimage=imagecreatetruecolor($image_size[0]
  80. ,$image_size[1]);
  81. $white=imagecolorallocate($nimage,255,255,255);
  82. $black=imagecolorallocate($nimage,0,0,0);
  83. $red=imagecolorallocate($nimage,255,0,0);
  84. imagefill($nimage,0,0,$white);
  85. switch ($iinfo[2])
  86. {
  87. case 1:
  88. $simage =imagecreatefromgif($destination);
  89. break;
  90. case 2:
  91. $simage =imagecreatefromjpeg($destination);
  92. break;
  93. case 3:
  94. $simage =imagecreatefrompng($destination);
  95. break;
  96. case 6:
  97. $simage =imagecreatefromwbmp($destination);
  98. break;
  99. default:
  100. die("不支援的檔案類型");
  101. exit;
  102. }
  103. imagecopy($nimage,$simage,0,0,0,0,
  104. $image_size[0],$image_size[1]);
  105. imagefilledrectangle($nimage,1,
  106. $image_size[1]-15,80,$image_size[1],$white);
  107. switch($watertype)
  108. {
  109. case 1: //加浮水印字串
  110. imagestring($nimage,2,3,$image_size[1]-15,
  111. $waterstring,$black);
  112. break;
  113. case 2: //加浮水印圖片
  114. $simage1 =imagecreatefromgif("xplore.gif");
  115. imagecopy($nimage,$simage1,0,0,0,0,85,15);
  116. imagedestroy($simage1);
  117. break;
  118. }
  119. switch ($iinfo[2])
  120. {
  121. case 1:
  122. //imagegif($nimage, $destination);
  123. imagejpeg($nimage, $destination);
  124. break;
  125. case 2:
  126. imagejpeg($nimage, $destination);
  127. break;
  128. case 3:
  129. imagepng($nimage, $destination);
  130. break;
  131. case 6:
  132. imagewbmp($nimage, $destination);
  133. //imagejpeg($nimage, $destination);
  134. break;
  135. }
  136. //覆蓋原上傳檔案
  137. imagedestroy($nimage);
  138. imagedestroy($simage);
  139. }
  140. if($imgpreview==1)
  141. {
  142. echo "
    圖片預覽:
    ";
  143. echo "height=".($image_size[1]*$imgpreviewsize);"
  144. echo " alt=\"圖片預覽:\r檔案名稱:".
  145. $destination."\r上傳時間:\" />";
  146. }
  147. }
  148. ?>
複製代碼
  • 聯繫我們

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