PHP image watermark code. Supports text and image watermarks. supports random watermark positions or fixed watermark transparency settings (image and text watermarks are supported). font and color of text watermarks, text watermarks, and image watermarks are supported.
Supports random or fixed watermark positions (nine cells)
Watermark transparency settings (image watermarks and text watermarks are supported)
Font, color, and size settings of text watermarks
Transparency of the image watermark background
The code is as follows:
/**
* Watermarks: Supports the transparency settings of text image watermarks and the transparency of the watermark image background.
* Date: 2011-09-27
* Author: www.jb51.net
* Usage:
* $ Obj = new WaterMask ($ imgFileName); // instantiate an object
* $ Obj-> $ waterType = 1; // type: 0 indicates a text watermark, and 1 indicates an image watermark.
* $ Obj-> $ transparent = 45; // watermark transparency
* $ Obj-> $ waterStr = 'www .jb51.net'; // watermark text
* $ Obj-> $ fontSize = 16; // text font size
* $ Obj-> $ fontColor = array (255, 0255); // watermark text color (RGB)
* $ Obj-> $ fontFile = 'ahgbold. ttf'; // font file
* $ Obj-> output (); // The output watermark image file overwrites the input image file.
*/
Class WaterMask {
Public $ waterType = 1; // watermark type: 0 indicates a text watermark, and 1 indicates an image watermark.
Public $ pos = 0; // Watermark Position
Public $ transparent = 45; // watermark transparency
Public $ waterStr = 'www .jb51.net'; // watermark text
Public $ fontSize = 16; // text font size
Public $ fontColor = array (255, 0, 255); // watermark text color (RGB)
Public $ fontFile = 'ahgbold. ttf'; // font file
Public $ waterImg = 'logo.png '; // watermark image
Private $ srcImg = ''; // image to which a watermark is to be added
Private $ im = ''; // Image handle
Private $ water_im = ''; // watermark image handle
Private $ srcImg_info = ''; // picture information
Private $ waterImg_info = ''; // watermark image information
Private $ str_w = ''; // watermark text width
Private $ str_h = ''; // watermark text height
Private $ x = ''; // watermark X coordinate
Private $ y = ''; // y coordinate of the watermark
Function _ construct ($ img) {// destructor
$ This-> srcImg = file_exists ($ img )? $ Img: die ('"'. $ img. '" source file does not exist! ');
}
Private function imginfo () {// obtain the information of the image to which the watermark is to be added and load the image.
$ This-> srcImg_info = getimagesize ($ this-> srcImg );
Switch ($ this-> srcImg_info [2]) {
Case 3:
$ This-> im = imagecreatefrompng ($ this-> srcImg );
Break 1;
Case 2:
$ This-> im = imagecreatefromjpeg ($ this-> srcImg );
Break 1;
Case 1:
$ This-> im = imagecreatefromgif ($ this-> srcImg );
Break 1;
Default:
Die ('original image ('. $ this-> srcImg.') format is incorrect. only PNG, JPEG, and GIF images are supported. ');
}
}
Private function waterimginfo () {// Obtain the watermark image information and load the image.
$ This-> waterImg_info = getimagesize ($ this-> waterImg );
Switch ($ this-> waterImg_info [2]) {
Case 3:
$ This-> water_im = imagecreatefrompng ($ this-> waterImg );
Break 1;
Case 2:
$ This-> water_im = imagecreatefromjpeg ($ this-> waterImg );
Break 1;
Case 1:
$ This-> water_im = imagecreatefromgif ($ this-> waterImg );
Break 1;
Default:
Die ('watermark image ('. $ this-> srcImg.') format is incorrect. only PNG, JPEG, and GIF images are supported. ');
}
}
Private function waterpos () {// watermark position algorithm
Switch ($ this-> pos ){
Case 0: // random position
$ This-> x = rand (0, $ this-> srcImg_info [0]-$ this-> waterImg_info [0]);
$ This-> y = rand (0, $ this-> srcImg_info [1]-$ this-> waterImg_info [1]);
Break 1;
Case 1: // upper left
$ This-> x = 0;
$ This-> y = 0;
Break 1;
Case 2: // upper
$ This-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2;
$ This-> y = 0;
Break 1;
Case 3: // upper right
$ This-> x = $ this-> srcImg_info [0]-$ this-> waterImg_info [0];
$ This-> y = 0;
Break 1;
Case 4: // center left
$ This-> x = 0;
$ This-> y = ($ this-> srcImg_info [1]-$ this-> waterImg_info [1])/2;
Break 1;
Case 5: // Medium
$ This-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2;
$ This-> y = ($ this-> srcImg_info [1]-$ this-> waterImg_info [1])/2;
Break 1;
Case 6: // right
$ This-> x = $ this-> srcImg_info [0]-$ this-> waterImg_info [0];
$ This-> y = ($ this-> srcImg_info [1]-$ this-> waterImg_info [1])/2;
Break 1;
Case 7: // bottom left
$ This-> x = 0;
$ This-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1];
Break 1;
Case 8: // bottom
$ This-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2;
$ This-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1];
Break 1;
Default: // bottom right
$ This-> x = $ this-> srcImg_info [0]-$ this-> waterImg_info [0];
$ This-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1];
Break 1;
}
}
Private function waterimg (){
If ($ this-> srcImg_info [0] <= $ this-> waterImg_info [0] | $ this-> srcImg_info [1] <= $ this-> waterImg_info [1]) {
Die ('watermark is larger than the source image! ');
}
$ This-> waterpos ();
$ Cut = imagecreatetruecolor ($ this-> waterImg_info [0], $ this-> waterImg_info [1]);
Imagecopy ($ cut, $ this-> im, 0, 0, $ this-> x, $ this-> y, $ this-> waterImg_info [0], $ this-> waterImg_info [1]);
$ Pct = $ this-> transparent;
Imagecopy ($ cut, $ this-> water_im, 0, 0, 0, $ this-> waterImg_info [0], $ this-> waterImg_info [1]);
Imagecopymerge ($ this-> im, $ cut, $ this-> x, $ this-> y, 0, 0, $ this-> waterImg_info [0], $ this-> waterImg_info [1], $ pct );
}
Private function waterstr (){
$ Rect = imagettfbbox ($ this-> fontSize, 0, $ this-> fontFile, $ this-> waterStr );
$ W = abs ($ rect [2]-$ rect [6]);
$ H = abs ($ rect [3]-$ rect [7]);
$ FontHeight = $ this-> fontSize;
$ This-> water_im = imagecreatetruecolor ($ w, $ h );
Imagealphablending ($ this-> water_im, false );
Imagesavealpha ($ this-> water_im, true );
$ White_alpha = imagecolorallocatealpha ($ this-> water_im, 255,255,255,127 );
Imagefill ($ this-> water_im, 0, 0, $ white_alpha );
$ Color = imagecolorallocate ($ this-> water_im, $ this-> fontColor [0], $ this-> fontColor [1], $ this-> fontColor [2]);
Imagettftext ($ this-> water_im, $ this-> fontSize, 0, 0, $ this-> fontSize, $ color, $ this-> fontFile, $ this-> waterStr );
$ This-> waterImg_info = array (0 => $ w, 1 => $ h );
$ This-> waterimg ();
}
Function output (){
$ This-> imginfo ();
If ($ this-> waterType = 0 ){
$ This-> waterstr ();
} Else {
$ This-> waterimginfo ();
$ This-> waterimg ();
}
Switch ($ this-> srcImg_info [2]) {
Case 3:
Imagepng ($ this-> im, $ this-> srcImg );
Break 1;
Case 2:
Imagejpeg ($ this-> im, $ this-> srcImg );
Break 1;
Case 1:
Imagegif ($ this-> im, $ this-> srcImg );
Break 1;
Default:
Die ('failed to add watermark! ');
Break;
}
Imagedestroy ($ this-> im );
Imagedestroy ($ this-> water_im );
}
}
?>
Watermark supports random position or fixed watermark transparency settings (image watermark and text watermark are supported) text watermark font, color ,...