PHP给图片加上图片水印和文字水印实例
程序员文章站
2023-11-22 15:15:58
下面给大家分享一下PHP给图片加上图片水印和文字水印实例,这也是网站经常用到的功能,把代码加上去,调用就很简单了。 ......
下面给大家分享一下php给图片加上图片水印和文字水印实例,这也是网站经常用到的功能,把代码加上去,调用就很简单了。
核心代码:
1 function imagewatermark($groundimage, $waterpos = 0, $waterimage = "", $watertext = "", $textfont = 5, $textcolor = "#ff0000") { 2 3 $iswaterimage = false; 4 5 $formatmsg = "暂不支持该文件格式,请用图片处理软件将图片转换为gif、jpg、png格式。"; 6 7 //读取水印文件 8 9 if (!empty($waterimage) && file_exists($waterimage)) { 10 11 $iswaterimage = true; 12 13 $water_info = getimagesize($waterimage); 14 15 $water_w = $water_info[0]; //取得水印图片的宽 16 17 $water_h = $water_info[1]; //取得水印图片的高 18 19 switch ($water_info[2]) {//取得水印图片的格式 20 case 1:$water_im = imagecreatefromgif($waterimage); 21 break; 22 23 case 2:$water_im = imagecreatefromjpeg($waterimage); 24 break; 25 26 case 3:$water_im = imagecreatefrompng($waterimage); 27 break; 28 29 default:die($formatmsg); 30 } 31 } 32 33 //读取背景图片 34 35 if (!empty($groundimage) && file_exists($groundimage)) { 36 37 $ground_info = getimagesize($groundimage); 38 39 $ground_w = $ground_info[0]; //取得背景图片的宽 40 41 $ground_h = $ground_info[1]; //取得背景图片的高 42 43 switch ($ground_info[2]) {//取得背景图片的格式 44 case 1:$ground_im = imagecreatefromgif($groundimage); 45 break; 46 47 case 2:$ground_im = imagecreatefromjpeg($groundimage); 48 break; 49 50 case 3:$ground_im = imagecreatefrompng($groundimage); 51 break; 52 53 default:die($formatmsg); 54 } 55 } else { 56 57 die("需要加水印的图片不存在!"); 58 } 59 60 //水印位置 61 62 if ($iswaterimage) {//图片水印 63 $w = $water_w; 64 65 $h = $water_h; 66 67 $label = "图片的"; 68 } else {//文字水印 69 $temp = imagettfbbox(ceil($textfont * 5), 0, "./cour.ttf", $watertext); //取得使用 truetype 字体的文本的范围 70 71 $w = $temp[2] - $temp[6]; 72 73 $h = $temp[3] - $temp[7]; 74 75 unset($temp); 76 77 $label = "文字区域"; 78 } 79 80 if (($ground_w < $w) || ($ground_h < $h)) { 81 82 echo "需要加水印的图片的长度或宽度比水印" . $label . "还小,无法生成水印!"; 83 84 return; 85 } 86 87 switch ($waterpos) { 88 89 case 0://随机 90 91 $posx = rand(0, ($ground_w - $w)); 92 93 $posy = rand(0, ($ground_h - $h)); 94 95 break; 96 97 case 1://1为顶端居左 98 99 $posx = 0; 100 101 $posy = 0; 102 103 break; 104 105 case 2://2为顶端居中 106 107 $posx = ($ground_w - $w) / 2; 108 109 $posy = 0; 110 111 break; 112 113 case 3://3为顶端居右 114 115 $posx = $ground_w - $w; 116 117 $posy = 0; 118 119 break; 120 121 case 4://4为中部居左 122 123 $posx = 0; 124 125 $posy = ($ground_h - $h) / 2; 126 127 break; 128 129 case 5://5为中部居中 130 131 $posx = ($ground_w - $w) / 2; 132 133 $posy = ($ground_h - $h) / 2; 134 135 break; 136 137 case 6://6为中部居右 138 139 $posx = $ground_w - $w; 140 141 $posy = ($ground_h - $h) / 2; 142 143 break; 144 145 case 7://7为底端居左 146 147 $posx = 0; 148 149 $posy = $ground_h - $h; 150 151 break; 152 153 case 8://8为底端居中 154 155 $posx = ($ground_w - $w) / 2; 156 157 $posy = $ground_h - $h; 158 159 break; 160 161 case 9://9为底端居右 162 163 $posx = $ground_w - $w - 10; // -10 是距离右侧10px 可以自己调节 164 165 $posy = $ground_h - $h - 10; // -10 是距离底部10px 可以自己调节 166 167 break; 168 169 default://随机 170 171 $posx = rand(0, ($ground_w - $w)); 172 173 $posy = rand(0, ($ground_h - $h)); 174 175 break; 176 } 177 178 //设定图像的混色模式 179 180 imagealphablending($ground_im, true); 181 182 if ($iswaterimage) {//图片水印 183 imagecopy($ground_im, $water_im, $posx, $posy, 0, 0, $water_w, $water_h); //拷贝水印到目标文件 184 } else {//文字水印 185 if (!emptyempty($textcolor) && (strlen($textcolor) == 7)) { 186 187 $r = hexdec(substr($textcolor, 1, 2)); 188 189 $g = hexdec(substr($textcolor, 3, 2)); 190 191 $b = hexdec(substr($textcolor, 5)); 192 } else { 193 194 die("水印文字颜色格式不正确!"); 195 } 196 197 imagestring($ground_im, $textfont, $posx, $posy, $watertext, imagecolorallocate($ground_im, $r, $g, $b)); 198 } 199 200 //生成水印后的图片 201 202 @unlink($groundimage); 203 204 switch ($ground_info[2]) {//取得背景图片的格式 205 case 1:imagegif($ground_im, $groundimage); 206 break; 207 208 case 2:imagejpeg($ground_im, $groundimage); 209 break; 210 211 case 3:imagepng($ground_im, $groundimage); 212 break; 213 214 default:die($errormsg); 215 } 216 217 //释放内存 218 219 if (isset($water_info)) 220 unset($water_info); 221 222 if (isset($water_im)) 223 imagedestroy($water_im); 224 225 unset($ground_info); 226 227 imagedestroy($ground_im); 228 }
文字水印:
watertext('banner.jpg', 9, '','sucaihuo.com'); //背景图片,$waterpos水印位置,水印图片地址
图片水印:
imagewatermark('404.jpg', 9, 'logo.gif'); //背景图片,$waterpos水印位置,水印图片地址
注意:$waterimage 和 $watertext 最好不要同时使用,选其中之一即可,优先使用 $waterimage。
本文转自: 转载请注明出处!
文字水印