Heim > Backend-Entwicklung > PHP-Tutorial > php多图片上传添加水印的代码

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

WBOY
Freigeben: 2016-07-25 08:51:49
Original
955 Leute haben es durchsucht
  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{
  55. echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
  56. return;
  57. }
  58. switch($waterPos)
  59. {
  60. case 0://随机
  61. $posX = rand(0,($ground_w - $w));
  62. $posY = rand(0,($ground_h - $h));
  63. break;
  64. case 1://1为顶端居左
  65. $posX = 0;
  66. $posY = 0;
  67. break;
  68. case 2://2为顶端居中
  69. $posX = ($ground_w - $w) / 2;
  70. $posY = 0;
  71. break;
  72. case 3://3为顶端居右
  73. $posX = $ground_w - $w;
  74. $posY = 0;
  75. break;
  76. case 4://4为中部居左
  77. $posX = 0;
  78. $posY = ($ground_h - $h) / 2;
  79. break;
  80. case 5://5为中部居中
  81. $posX = ($ground_w - $w) / 2;
  82. $posY = ($ground_h - $h) / 2;
  83. break;
  84. case 6://6为中部居右
  85. $posX = $ground_w - $w;
  86. $posY = ($ground_h - $h) / 2;
  87. break;
  88. case 7://7为底端居左
  89. $posX = 0;
  90. $posY = $ground_h - $h;
  91. break;
  92. case 8://8为底端居中
  93. $posX = ($ground_w - $w) / 2;
  94. $posY = $ground_h - $h;
  95. break;
  96. case 9://9为底端居右
  97. $posX = $ground_w - $w;
  98. $posY = $ground_h - $h;
  99. break;
  100. default://随机
  101. $posX = rand(0,($ground_w - $w));
  102. $posY = rand(0,($ground_h - $h));
  103. break;
  104. }
  105. //设定图像的混色模式
  106. imagealphablending($ground_im, true);
  107. if($isWaterImage)//图片水印
  108. {
  109. imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
  110. }
  111. else//文字水印
  112. {
  113. if( !empty($textColor) && (strlen($textColor)==7) )
  114. {
  115. $R = hexdec(substr($textColor,1,2));
  116. $G = hexdec(substr($textColor,3,2));
  117. $B = hexdec(substr($textColor,5));
  118. }
  119. else
  120. {
  121. die("水印文字颜色格式不正确!");
  122. }
  123. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  124. }
  125. //生成水印后的图片
  126. @unlink($groundImage);
  127. switch($ground_info[2])//取得背景图片的格式
  128. {
  129. case 1:imagegif($ground_im,$groundImage);break; //创建gif格式的图片
  130. case 2:imagejpeg($ground_im,$groundImage);break; //创建jpeg格式的图片
  131. case 3:imagepng($ground_im,$groundImage);break; //创建png格式的图片
  132. default:die($errorMsg);
  133. }
  134. //释放内存
  135. if(isset($water_info)) unset($water_info);
  136. if(isset($water_im)) imagedestroy($water_im);
  137. unset($ground_info);
  138. imagedestroy($ground_im);
  139. }
  140. ?>
  141. php图片上传代码:
  142. for ($i=0;$i{
  143. $upfile="./img/".($i+1).".png";//此处路径换成你的
  144. if(move_uploaded_file($_FILES['userfile']['tmp_name'][$i],$upfile)){
  145. imageWaterMark($upfile,9,"./shuiyin.png","Made By Chenduan",5,"#FF0000");
  146. /*
  147. * 功能:图片水印 (水印支持图片或文字)
  148. * imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000")
  149. * 参数:
  150. * $groundImage 背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
  151. * $waterPos 水印位置,有10种状态,0为随机位置;
  152. * 1为顶端居左,2为顶端居中,3为顶端居右;
  153. * 4为中部居左,5为中部居中,6为中部居右;
  154. * 7为底端居左,8为底端居中,9为底端居右;
  155. * $waterImage 图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
  156. * $waterText 文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
  157. * $textFont 文字大小,值为1、2、3、4或5,默认为5;
  158. * $textColor 文字颜色,值为十六进制颜色值,默认为#FF0000(红色);
  159. */
  160. echo "php多图片上传添加水印的代码 ";
  161. echo "第".($i+1)."张图片操作成功
    ";
  162. }
  163. else{
  164. echo "第".($i+1)."张图片上传不了
    ";
  165. }
  166. }
  167. ?>
复制代码


Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage