">
当前位置:Gxlcms > PHP教程 > php图片加水印与上传图片加水印类

php图片加水印与上传图片加水印类

时间:2021-07-01 10:21:17 帮助过:26人阅读

  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. $waterimg="xplore.gif"; //水印图片
  20. $imgpreview=1; //是否生成预览图(1为生成,其他为不生成);
  21. $imgpreviewsize=1/2; //缩略图比例
  22. ?>
  23. ZwelL图片上传程序
  24. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  25. {
  26. if (!is_uploaded_file($_FILES["upfile"]
  27. [tmp_name]))
  28. //是否存在文件
  29. {
  30. echo "图片不存在!";
  31. exit;
  32. }
  33. $file = $_FILES["upfile"];
  34. if($max_file_size <$file["size"])
  35. //检查文件大小
  36. {
  37. echo "文件太大!";
  38. exit;
  39. }
  40. if(!in_array($file["type"], $uptypes))
  41. //检查文件类型
  42. {
  43. echo "文件类型不符!".$file["type"];
  44. exit;
  45. }
  46. if(!file_exists($destination_folder))
  47. {
  48. mkdir($destination_folder);
  49. }
  50. $filename=$file["tmp_name"];
  51. $image_size = getimagesize($filename);
  52. $pinfo=pathinfo($file["name"]);
  53. $ftype=$pinfo['extension'];
  54. $destination = $destination_folder.
  55. time().".".$ftype;
  56. if (file_exists($destination) &&
  57. $overwrite != true)
  58. {
  59. echo "同名文件已经存在了";
  60. exit;
  61. }
  62. if(!move_uploaded_file ($filename,
  63. $destination))
  64. {
  65. echo "移动文件出错";
  66. exit;
  67. }
  68. $pinfo=pathinfo($destination);
  69. $fname=$pinfo[basename];
  70. echo " 已经成功上传

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

人气教程排行