首頁 >後端開發 >php教程 >php圖片加浮水印與上傳圖片加浮水印類

php圖片加浮水印與上傳圖片加浮水印類

WBOY
WBOY原創
2016-07-25 09:07:401228瀏覽
  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="
  18. http://www.xplore.cn/
  19. "; //浮水印字串
  20. $waterimg="xplore.gif"; //水印圖片
  21. $imgpreview=1; //是否產生預覽圖(1為生成,其他為不生成);
  22. $imgpreviewsize=1/2; //縮圖比例
  23. ?>
  24. ZwelL圖片上傳程式
  25. method="post" name="upform">
  26. 上傳檔案:

  27. 允許上傳的檔案類型為:=implode(', ',$uptypes) ?>
  28. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  29. {
  30. if (!is_uploaded_file($_FILES["upfile "]
  31. [tmp_name]))
  32. //是否存在檔案
  33. {
  34. echo "圖片不存在!";
  35. exit;
  36. }
  37. $file = $_FILES ["upfile"];
  38. if($max_file_size //檢查檔案大小
  39. {
  40. echo "檔案太大!";
  41. exit;
  42. }
  43. if(!in_array($file["type"], $uptypes))
  44. //檢查檔案類型
  45. {
  46. echo "檔案類型不符!".$file[ "type"];
  47. exit;
  48. }
  49. if(!file_exists($destination_folder))
  50. {
  51. mkdir($destination_folder);
  52. }
  53. mkdir($destination_folder);
  54. }
  55. mkdir($destination_filename=$ file["tmp_name"];
  56. $image_size = getimagesize($filename);
  57. $pinfo=pathinfo($file["name"]);
  58. $ftype=$pinfo['extension'];
  59. $destination = $destination_folder.
  60. time().".".$ftype;
  61. if (file_exists($destination) &&
  62. $overwrite != true)
  63. {
  64. {
  65. echo "同名檔案已經存在了";
  66. exit;
  67. }
  68. if(!move_uploaded_file ($filename,
  69. $destination))
  70. {
  71. echo "移動檔案出錯"";
  72. exit;
  73. }
  74. $pinfo=pathinfo($destination);
  75. $fname=$pinfo[basename];
  76. echo " 已經成功上傳

  77. 檔名:
  78. ".$destination_folder.
  79. $fname."

  80. ";
  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]
  88. ,$image_size[1]);
  89. $white=imagecolorallocate($nimage,255,255,255);
  90. $black=imagecolorallocate($nimage,0,0,0);
  91. $red=imagecolorallocate($nimage,255,0,0);imagefill($nimage,0,0,$white);
  92. switch ($iinfo[2])
  93. {
  94. case 1:
  95. $simage =imagecreatefromgif($destination);
  96. break;
  97. case 2:
  98. $simage =imagecreatefromjpeg($destination);
  99. break;
  100. case 3:
  101. $simage =imagecreatefrompng($destination);; >case 6:
  102. $simage =imagecreatefromwbmp($destination);
  103. break;
  104. default:
  105. die("不支援的檔案類型");
  106. exit;
  107. }
  108. imagecopy($nimage,$simage,0,0,0,0,
  109. $image_size[0],$image_size[1]);
  110. imagefilledrectangle($nimage,1,
  111. $image_size[ 1]-15,80,$image_size[1],$white);
  112. switch($watertype)
  113. {
  114. case 1: //加浮水印字串
  115. imagestring($nimage,2 ,3,$image_size[1]-15,
  116. $waterstring,$black);
  117. break;
  118. case 2: //加水印圖片
  119. $simage1 =imagecreatefromgif("xplore.gif" );
  120. imagecopy($nimage,$simage1,0,0,0,0,85,15);
  121. imagedestroy($simage1);
  122. break;
  123. }
  124. switch ($ iinfo[2])
  125. {
  126. case 1:
  127. //imagegif($nimage, $destination);
  128. imagejpeg($nimage, $destination);
  129. break;
  130. casecase 2:
  131. imagejpeg($nimage, $destination);
  132. break;
  133. case 3:
  134. imagepng($nimage, $destination);
  135. break;
  136. case 6:
  137. imagewbmp($nimage, $destination);
  138. //imagejpeg($nimage, $destination);
  139. break;
  140. }
  141. //覆寫原上傳檔案
  142. imagedestroy($nimage) ;
  143. imagedestroy($simage);
  144. }
  145. if($imgpreview==1)
  146. {
  147. echo "
    圖片預覽:
    ";
  148. echo "height=".($image_size[1]*$imgpreviewsize);"
  149. echo " alt="圖片預覽:r檔案名稱:".
  150. $destination."r上傳時間:" />";
  151. }
  152. }
  153. ?>
複製代碼



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn