PHP adds image and text watermark codes, and PHP adds watermarks. It supports the transparency setting of text image watermarks and the transparency of the watermark image background. A class written by myself, because a CMS developed by myself will be used in it, it is always difficult to use it on the Internet. I hope everyone will also like this class, followed by the class usage method.
001
002 class WaterMask {
003 public $ waterType = 1; // watermark type: 0 indicates a text watermark, and 1 indicates an image watermark.
004 public $ pos = 0; // watermark position
005 public $ transparent = 45; // watermark transparency
006 public $ waterStr = 'www .codefans.net '; // watermark text
007 public $ fontSize = 16; // text font size
008 public $ fontColor = array (255, 0, 255); // watermark text color (RGB)
009 public $ fontFile = 'ahgbold. ttf'; // font file
010 public $ waterImg = 'logo.png '; // watermark image
011 private $ srcImg = ''; // image to which a watermark is to be added
012 private $ im = ''; // image handle
013 private $ water_im = ''; // watermark image handle
014 private $ srcImg_info = ''; // picture information
015 private $ waterImg_info = ''; // watermark image information
016 private $ str_w = ''; // watermark text width
017 private $ str_h = ''; // watermark text height
018 private $ x = ''; // watermark X coordinate
019 private $ y = ''; // y coordinate of the watermark
020 function _ construct ($ img ){
021 $ this-> srcImg = file_exists ($ img) $ img: die ('"'. $ img. '" sorry, the watermark file does not exist! ');
022}
023 private function imginfo () {// obtain and load the watermark image information.
024 $ this-> srcImg_info = getimagesize ($ this-> srcImg );
025 switch ($ this-> srcImg_info [2]) {
026 case 3:
027 $ this-> im = imagecreatefrompng ($ this-> srcImg );
028 break 1;
029 case 2:
030 $ this-> im = imagecreatefromjpeg ($ this-> srcImg );
031 break 1;
032 case 1:
033 $ this-> im = imagecreatefromgif ($ this-> srcImg );
034 break 1;
035 default:
036 die ('watermark image ('. $ this-> srcImg.') watermark image format is incorrect. Please select images in PNG, JPEG, and GIF formats. ');
037}
038}
039 private function waterimginfo () {// obtain and load the watermark image.
040 $ this-> waterImg_info = getimagesize ($ this-> waterImg );
041 switch ($ this-> waterImg_info [2]) {
042 case 3:
043 $ this-> water_im = imagecreatefrompng ($ this-> waterImg );
044 break 1;
045 case 2:
046 $ this-> water_im = imagecreatefromjpeg ($ this-> waterImg );
047 break 1;
048 case 1:
049 $ this-> water_im = imagecreatefromgif ($ this-> waterImg );
050 break 1;
051 default:
052 die ('watermark image ('. $ this-> srcImg.') format is incorrect. Only PNG, JPEG, and GIF images are supported. ');
053}
054}
055 private function waterpos () {// watermark position Algorithm
056 switch ($ this-> pos ){
057 case 0: // random position
058 $ this-> x = rand (0, $ this-> srcImg_info [0]-$ this-> waterImg_info [0]);
059 $ this-> y = rand (0, $ this-> srcImg_info [1]-$ this-> waterImg_info [1]);
060 break 1;
061 case 1: // upper left
062 $ this-> x = 0;
063 $ this-> y = 0;
064 break 1;
065 case 2: // upper
066 $ this-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2;
067 $ this-> y = 0;
068 break 1;
069 case 3: // upper right
070 $ this-> x = $ this-> srcImg_info [0]-$ this-> waterImg_info [0];
071 $ this-> y = 0;
072 break 1;
073 case 4: // center left
074 $ this-> x = 0;
075 $ this-> y = ($ this-> srcImg_info [1]-$ this-> waterImg_info [1])/2;
076 break 1;
077 case 5: // Medium
078 $ this-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2;
079 $ this-> y = ($ this-> srcImg_info [1]-$ this-> waterImg_info [1])/2;
080 break 1;
081 case 6: // middle right
082 $ this-> x = $ this-> srcImg_info [0]-$ this-> waterImg_info [0];
083 $ this-> y = ($ this-> srcImg_info [1]-$ this-> waterImg_info [1])/2;
084 break 1;
085 case 7: // bottom left
086 $ this-> x = 0;
087 $ this-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1];
088 break 1;
089 case 8: // bottom
090 $ this-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2;
091 $ this-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1];
092 break 1;
093 default: // bottom right
094 $ this-> x = $ this-> srcImg_info [0]-$ this-> waterImg_info [0];
095 $ this-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1];
096 break 1;
097}
098}
099 private function waterimg (){
100 if ($ this-> srcImg_info [0] <= $ this-> waterImg_info [0] | $ this-> srcImg_info [1] <= $ this-> waterImg_info [1 ]) {
101 die ('watermark is larger than the source image! ');
102}
103 $ this-> waterpos ();
104 $ cut = imagecreatetruecolor ($ this-> waterImg_info [0], $ this-> waterImg_info [1]);
105 imagecopy ($ cut, $ this-> im, $ this-> x, $ this-> y, $ this-> waterImg_info [0], $ this-> waterImg_info [1]);
106 $ pct = $ this-> transparent;
107 imagecopy ($ cut, $ this-> water_im, $ this-> waterImg_info [0], $ this-> waterImg_info [1]);
108 imagecopymerge ($ this-> im, $ cut, $ this-> x, $ this-> y, $ this-> waterImg_info [0], $ this-> waterImg_info [1], $ pct );
109}
110 private function waterstr (){
111 $ rect = imagettfbbox ($ this-> fontSize, 0, $ this-> fontFile, $ this-> waterStr );
112 $ w = abs ($ rect [2]-$ rect [6]);
113 $ h = abs ($ rect [3]-$ rect [7]);
114 $ fontHeight = $ this-> fontSize;
115 $ this-> water_im = imagecreatetruecolor ($ w, $ h );
116 imagealphablending ($ this-> water_im, false );
117 imagesavealpha ($ this-> water_im, true );
118 $ white_alpha = imagecolorallocatealpha ($ this-> water_im, 255,255,255,127 );
119 imagefill ($ this-> water_im, $ white_alpha );
120 $ color = imagecolorallocate ($ this-> water_im, $ this-> fontColor [0], $ this-> fontColor [1], $ this-> fontColor [2]);
121 imagettftext ($ this-> water_im, $ this-> fontSize, $ this-> fontSize, $ color, $ this-> fontFile, $ this-> waterStr );
122 $ this-> waterImg_info = array (0 => $ w, 1 => $ h );
123 $ this-> waterimg ();
124}
125 function output (){
126 $ this-> imginfo ();
127 if ($ this-> waterType = 0 ){
128 $ this-> waterstr ();
129} else {
130 $ this-> waterimginfo ();
131 $ this-> waterimg ();
132}
133 switch ($ this-> srcImg_info [2]) {
134 case 3:
135 imagepng ($ this-> im, $ this-> srcImg );
136 break 1;
137 case 2:
138 imagejpeg ($ this-> im, $ this-> srcImg );
139 break 1;
140 case 1:
141 imagegif ($ this-> im, $ this-> srcImg );
142 break 1;
Default 143:
144 die ('unknown reason: watermark addition failed! ');
145 break;
146}
147 imagedestroy ($ this-> im );
148 imagedestroy ($ this-> water_im );
149}
150}
151?>
PHP watermark generation usage:
View sourceprint? 01
02 $ obj = new WaterMask ($ imgFileName );
03 $ obj-> $ waterType = 1; // watermark type: 0 indicates a text watermark, and 1 indicates an image watermark.
04 $ obj-> $ transparent = 45; // watermark transparency
05 $ obj-> $ waterStr = 'www .codefans.net '; // watermark text
06 $ obj-> $ fontSize = 16; // font size
07 $ obj-> $ fontColor = array (255, 0,); // watermark text color (RGB value)
08 $ obj-> $ fontFile = 'ahgbold. ttf'; // font name
09 $ obj-> output (); // output watermark image
10?>