当前位置:Gxlcms > PHP教程 > php多图片上传添加水印的代码

php多图片上传添加水印的代码

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

  1. //php水印函数
  2. function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000")
  3. {
  4. $isWaterImage = FALSE;
  5. $formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
  6. //读取水印文件
  7. if(!empty($waterImage) && file_exists($waterImage))
  8. {
  9. $isWaterImage = TRUE;
  10. $water_info = getimagesize($waterImage); //得到的是一个数组
  11. $water_w = $water_info[0];//取得水印图片的宽
  12. $water_h = $water_info[1];//取得水印图片的高
  13. switch($water_info[2])//取得水印图片的格式
  14. {
  15. case 1:$water_im = imagecreatefromgif($waterImage);break; //将图片转化为PHP可识别的编码过程
  16. case 2:$water_im = imagecreatefromjpeg($waterImage);break; //将图片转化为PHP可识别的编码过程
  17. case 3:$water_im = imagecreatefrompng($waterImage);break; //将图片转化为PHP可识别的编码过程
  18. default:die($formatMsg);
  19. }
  20. }
  21. //读取背景图片
  22. if(!empty($groundImage) && file_exists($groundImage))
  23. {
  24. $ground_info = getimagesize($groundImage);
  25. $ground_w = $ground_info[0];//取得背景图片的宽
  26. $ground_h = $ground_info[1];//取得背景图片的高
  27. switch($ground_info[2])//取得背景图片的格式
  28. {
  29. case 1:$ground_im = imagecreatefromgif($groundImage);break;
  30. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  31. case 3:$ground_im = imagecreatefrompng($groundImage);break;
  32. default:die($formatMsg);
  33. }
  34. }
  35. else
  36. {
  37. die("需要加水印的图片不存在!");
  38. }
  39. //水印位置
  40. if($isWaterImage)//图片水印
  41. {
  42. $w = $water_w;
  43. $h = $water_h;
  44. $label = "图片的";
  45. }
  46. else//文字水印
  47. {
  48. $temp = imagettfbbox(ceil($textFont*2.5),0,"c:/windows/fonts/stcaiyun.ttf",$waterText);//取得使用 TrueType 字体的文本的范围
  49. $w = $temp[2] - $temp[6];
  50. $h = $temp[3] - $temp[7];
  51. unset($temp);
  52. $label = "文字区域";
  53. }
  54. if( ($ground_w<$w) || ($ground_h<$h) )
  55. {
  56. echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
  57. return;
  58. }
  59. switch($waterPos)
  60. {
  61. case 0://随机
  62. $posX = rand(0,($ground_w - $w));
  63. $posY = rand(0,($ground_h - $h));
  64. break;
  65. case 1://1为顶端居左
  66. $posX = 0;
  67. $posY = 0;
  68. break;
  69. case 2://2为顶端居中
  70. $posX = ($ground_w - $w) / 2;
  71. $posY = 0;
  72. break;
  73. case 3://3为顶端居右
  74. $posX = $ground_w - $w;
  75. $posY = 0;
  76. break;
  77. case 4://4为中部居左
  78. $posX = 0;
  79. $posY = ($ground_h - $h) / 2;
  80. break;
  81. case 5://5为中部居中
  82. $posX = ($ground_w - $w) / 2;
  83. $posY = ($ground_h - $h) / 2;
  84. break;
  85. case 6://6为中部居右
  86. $posX = $ground_w - $w;
  87. $posY = ($ground_h - $h) / 2;
  88. break;
  89. case 7://7为底端居左
  90. $posX = 0;
  91. $posY = $ground_h - $h;
  92. break;
  93. case 8://8为底端居中
  94. $posX = ($ground_w - $w) / 2;
  95. $posY = $ground_h - $h;
  96. break;
  97. case 9://9为底端居右
  98. $posX = $ground_w - $w;
  99. $posY = $ground_h - $h;
  100. break;
  101. default://随机
  102. $posX = rand(0,($ground_w - $w));
  103. $posY = rand(0,($ground_h - $h));
  104. break;
  105. }
  106. //设定图像的混色模式
  107. imagealphablending($ground_im, true);
  108. if($isWaterImage)//图片水印
  109. {
  110. imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
  111. }
  112. else//文字水印
  113. {
  114. if( !empty($textColor) && (strlen($textColor)==7) )
  115. {
  116. $R = hexdec(substr($textColor,1,2));
  117. $G = hexdec(substr($textColor,3,2));
  118. $B = hexdec(substr($textColor,5));
  119. }
  120. else
  121. {
  122. die("水印文字颜色格式不正确!");
  123. }
  124. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  125. }
  126. //生成水印后的图片
  127. @unlink($groundImage);
  128. switch($ground_info[2])//取得背景图片的格式
  129. {
  130. case 1:imagegif($ground_im,$groundImage);break; //创建gif格式的图片
  131. case 2:imagejpeg($ground_im,$groundImage);break; //创建jpeg格式的图片
  132. case 3:imagepng($ground_im,$groundImage);break; //创建png格式的图片
  133. default:die($errorMsg);
  134. }
  135. //释放内存
  136. if(isset($water_info)) unset($water_info);
  137. if(isset($water_im)) imagedestroy($water_im);
  138. unset($ground_info);
  139. imagedestroy($ground_im);
  140. }
  141. ?>
  142. php图片上传代码:
  143. for ($i=0;$i{
  144. $upfile="./img/".($i+1).".png";//此处路径换成你的
  145. if(move_uploaded_file($_FILES['userfile']['tmp_name'][$i],$upfile)){
  146. imageWaterMark($upfile,9,"./shuiyin.png","Made By Chenduan",5,"#FF0000");
  147. /*
  148. * 功能:图片水印 (水印支持图片或文字)
  149. * imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000")
  150. * 参数:
  151. * $groundImage 背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
  152. * $waterPos 水印位置,有10种状态,0为随机位置;
  153. * 1为顶端居左,2为顶端居中,3为顶端居右;
  154. * 4为中部居左,5为中部居中,6为中部居右;
  155. * 7为底端居左,8为底端居中,9为底端居右;
  156. * $waterImage 图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
  157. * $waterText 文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
  158. * $textFont 文字大小,值为1、2、3、4或5,默认为5;
  159. * $textColor 文字颜色,值为十六进制颜色值,默认为#FF0000(红色);
  160. */
  161. echo "";
  162. echo "第".($i+1)."张图片操作成功
    ";
  163. }
  164. else{
  165. echo "第".($i+1)."张图片上传不了
    ";
  166. }
  167. }
  168. ?>

人气教程排行