php中png透明背景水印程序代码

给图片加水印是php程序中一个常用的并且很实用的功能,下面我来给大家详细介绍php中png透明背景水印程序代码吧,代码如下:

  1. //原始图像
  2. $dst = "/upload/20120914/20120914040740-0.jpg"; //注意图片路径要正确
  3. //得到原始图片信息
  4. $dst_info = getimagesize($dst);
  5. switch ($dst_info[2])
  6. {
  7. case 1:
  8. $dst_im =imagecreatefromgif($dst);break;
  9. case 2:
  10. $dst_im =imagecreatefromjpeg($dst);break;
  11. case 3:
  12. $dst_im =imagecreatefrompng($dst);break;
  13. case 6:
  14. $dst_im =imagecreatefromwbmp($dst);break;
  15. default:
  16. die("不支持的文件类型1");exit;
  17. }
  18. //水印图像
  19. $src = "/images/shuiyin.png"; //注意路径要写对
  20. $src_info = getimagesize($src);
  21. switch ($src_info[2])
  22. {
  23. case 1:
  24. $src_im =imagecreatefromgif($src);break;
  25. case 2:
  26. $src_im =imagecreatefromjpeg($src);break;
  27. case 3:
  28. $src_im =imagecreatefrompng($src);break;
  29. case 6:
  30. $src_im =imagecreatefromwbmp($src);break;
  31. default:
  32. die("不支持的文件类型1");exit;
  33. }
  34. //半透明格式水印
  35. //$alpha = 50;//水印透明度
  36. //imagecopymerge($dst_im,$src_im,$dst_info[0]-$src_info[0]-10,$dst_info[1]-$src_info[1]-
  37. 10,0,0,$src_info[0],$src_info[1],$alpha);
  38. //支持png本身透明度的方式
  39. imagecopy($dst_im,$src_im,$dst_info[0]-$src_info[0]-10,$dst_info[1]-$src_info[1]-10,0,0,$src_info
  40. [0],$src_info[1]);
  41. //保存图片
  42. switch ($dst_info[2]){
  43. case 1:
  44. imagegif($dst_im,$dst);break;
  45. case 2:
  46. imagejpeg($dst_im,$dst);break;
  47. case 3:
  48. imagepng($dst_im,$dst);break;
  49. //开源代码phpfensi.com
  50. case 6:
  51. imagewbmp($dst_im,$dst);break;
  52. default:
  53. die("不支持的文件类型2");exit;
  54. }
  55. imagedestroy($dst_im);
  56. imagedestroy($src_im);

给一个标准的图片水印类,代码如下:

  1. /*
  2. * 功能:PHP图片水印 (水印支持图片或文字)
  3. * 参数:
  4. * $product_img 背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
  5. * $waterPos 水印位置,有10种状态,0为随机位置;
  6. * 1为顶端居左,2为顶端居中,3为顶端居右;
  7. * 4为中部居左,5为中部居中,6为中部居右;
  8. * 7为底端居左,8为底端居中,9为底端居右;
  9. * $waterImage 图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
  10. * $waterText 文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
  11. * $textFont 文字大小,值为1、2、3、4或5,默认为5;
  12. * $textColor 文字颜色,值为十六进制颜色值,默认为#FF0000(红色);
  13. *
  14. * 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG
  15. * $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。
  16. * 当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。
  17. * 加水印后的图片的文件名和 $product_img 一样。
  18. * 作者:longware @ 2004-11-3 14:15:13
  19. */
  20. function imageWaterMark
  21. ($product_img,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000")
  22. {
  23. $isWaterImage = FALSE;
  24. $formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
  25. //读取水印文件
  26. if(!emptyempty($waterImage) && file_exists($waterImage))
  27. {
  28. $isWaterImage = TRUE;
  29. $water_info = getimagesize($waterImage);
  30. $water_w = $water_info[0];//取得水印图片的宽
  31. $water_h = $water_info[1];//取得水印图片的高
  32. switch($water_info[2])//取得水印图片的格式
  33. {
  34. case 1water_im = imagecreatefromgif($waterImage);break;
  35. case 2water_im = imagecreatefromjpeg($waterImage);break;
  36. case 3water_im = imagecreatefrompng($waterImage);break;
  37. default:die($formatMsg);
  38. }
  39. }
  40. //读取背景图片
  41. if(!emptyempty($product_img) && file_exists($product_img))
  42. {
  43. $ground_info = getimagesize($product_img);
  44. $ground_w = $ground_info[0];//取得背景图片的宽
  45. $ground_h = $ground_info[1];//取得背景图片的高
  46. switch($ground_info[2])//取得背景图片的格式
  47. {
  48. case 1:$ground_im = imagecreatefromgif($product_img);break;
  49. case 2:$ground_im = imagecreatefromjpeg($product_img);break;
  50. case 3:$ground_im = imagecreatefrompng($product_img);break;
  51. default:die($formatMsg);
  52. }
  53. }
  54. else
  55. {
  56. die("需要加水印的图片不存在!");
  57. }
  58. //水印位置
  59. if($isWaterImage)//图片水印
  60. {
  61. $w = $water_w;
  62. $h = $water_h;
  63. $label = "图片的";
  64. }
  65. else//文字水印
  66. {
  67. $temp = imagettfbbox(ceil($textFont*2.5),0,"arial.ttf",$waterText);//取得使用 TrueType 字体的文
  68. 本的范围
  69. $w = $temp[2] - $temp[6];
  70. $h = $temp[3] - $temp[7];
  71. unset($temp);
  72. $label = "文字区域";
  73. }
  74. if( ($ground_w<$w) || ($ground_h<$h) )
  75. {
  76. echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
  77. return;
  78. }
  79. switch($waterPos)
  80. {
  81. case 0://随机
  82. $posX = rand(0,($ground_w - $w));
  83. $posY = rand(0,($ground_h - $h));
  84. break;
  85. case 1://1为顶端居左
  86. $posX = 0;
  87. $posY = 0;
  88. break;
  89. case 2://2为顶端居中
  90. $posX = ($ground_w - $w) / 2;
  91. $posY = 0;
  92. break;
  93. case 3://3为顶端居右
  94. $posX = $ground_w - $w;
  95. $posY = 0;
  96. break;
  97. case 4://4为中部居左
  98. $posX = 0;
  99. $posY = ($ground_h - $h) / 2;
  100. break;
  101. case 5://5为中部居中
  102. $posX = ($ground_w - $w) / 2;
  103. $posY = ($ground_h - $h) / 2;
  104. break;
  105. case 6://6为中部居右
  106. $posX = $ground_w - $w;
  107. $posY = ($ground_h - $h) / 2;
  108. break;
  109. case 7://7为底端居左
  110. $posX = 0;
  111. $posY = $ground_h - $h;
  112. break;
  113. case 8://8为底端居中
  114. $posX = ($ground_w - $w) / 2;
  115. $posY = $ground_h - $h;
  116. break;
  117. case 9://9为底端居右
  118. $posX = $ground_w - $w;
  119. $posY = $ground_h - $h;
  120. break;
  121. default://随机
  122. $posX = rand(0,($ground_w - $w));
  123. $posY = rand(0,($ground_h - $h));
  124. break;
  125. }
  126. //设定图像的混色模式
  127. imagealphablending($ground_im, true);
  128. if($isWaterImage)//图片水印
  129. {
  130. imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
  131. }
  132. else//文字水印
  133. {
  134. if( !emptyempty($textColor) && (strlen($textColor)==7) )
  135. {
  136. $R = hexdec(substr($textColor,1,2));
  137. $G = hexdec(substr($textColor,3,2));
  138. $B = hexdec(substr($textColor,5));
  139. }
  140. else
  141. {
  142. die("水印文字颜色格式不正确!");
  143. }
  144. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im,
  145. $R, $G, $B));
  146. }
  147. //生成水印后的图片
  148. @unlink($product_img);
  149. switch($ground_info[2])//取得背景图片的格式
  150. {
  151. case 1:imagegif($ground_im,$product_img);break;
  152. case 2:imagejpeg($ground_im,$product_img);break;
  153. case 3:imagepng($ground_im,$product_img);break;
  154. default:die($errorMsg);
  155. }
  156. //释放内存
  157. if(isset($water_info)) unset($water_info);
  158. if(isset($water_im)) imagedestroy($water_im);
  159. unset($ground_info);
  160. imagedestroy($ground_im);
  161. }
  162. //---------------------------------------------------------------------------------------
  163. if(isset($_FILES) && !emptyempty($_FILES['userfile']) && $_FILES['userfile']['size']>0)
  164. {
  165. $uploadfile = "./".time()."_".$_FILES['userfile']['name'];
  166. if (copy($_FILES['userfile']['tmp_name'], $uploadfile))
  167. {
  168. echo "OK<br>";
  169. //文字水印
  170. imageWaterMark($uploadfile,0,"",http://www.111cn.net,5,"#FF0000");
  171. //图片水印
  172. //$waterImage="images/bz.gif";//水印图片路径
  173. //imageWaterMark($uploadfile,0,$waterImage);
  174. echo "<img src="".$uploadfile."" >";
  175. }
  176. else
  177. {
  178. echo "Fail<br>";
  179. }
  180. }