This article mainly introduces the implementation of PHP watermark class, support to add pictures, text, fill the color area, interested in the reference of friends, I hope to be helpful to everyone.
* Self-organizing a watermark class *
Support for adding picture, text, fill color area
<?php/** * Image plus watermark class, support text watermark, transparency settings, custom watermark location and so on. * Use Example: * $obj = new Watermask ($imgFileName); Instantiated Object * $obj $waterType = 1; Type: 0 for text watermark, 1 for picture watermark * $obj $transparent = 45; Watermark Transparency * $obj $waterStr = ' icp.niufee.com '; Watermark Text * $obj-$fontSize = 18; Text font size * $obj $fontColor = array (255,255,255); Watermark text Color (RGB) * $obj $fontFile = ' Ahgbold.ttf '; Font file * ... * $obj->output (); Output watermark picture file overlay to input picture file * @modify liuzp111 */class watermask{public $waterTypeImage = false; Watermark type: Enable picture watermark public $waterTypeStr = false; Watermark Type: Enable text watermark public $pos = 0; Watermark Location Public $transparent = 45; Watermark Transparency (0---100) the larger the value the more opaque public $waterStr = ' icp.niufee.com '; Watermark Text Public $fontSize = 14; Text font size Public $fontColor = Array (0,0,0); Watermark text color (RGB) Default black public $fontFile = './font/simfang.ttf '; Font file Public $waterimg = ' logo.png '; Watermark Image Private $srcImg = '; Images that need to be added to the watermark private $im = '; Picture handle private $water _im = '; Watermark Picture handle private $srcImg _info = "; Picture information private $WATERIMG _info = "; Watermark Picture information private $str _w = "; Watermark Text Width private $str _h = "; Watermark Text Height private $x = '; Watermark x Coordinate private $y = '; Watermark y-coordinate public $output _img = "; Store the output picture to where public $is _draw_rectangle = false; Whether to draw a rectangular region (no custom position is currently supported)//public $rectange _color = "; Draws the color of the rectangular area private $result _array = Array (); Result array Public function __construct ($img) {//destructor//$this->srcimg = file_exists ($img)? $img: Die (' "'. $img. '" The source file does not exist! '); if (file_exists ($img)) {$this->srcimg = $img; }else{return Array (' data ' = = ', ' info ' = ' source file does not exist! ', ' status ' =>0); }} Private Function ImginfO () {//Get information about the picture you want to add a watermark to and load the picture. $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 picture ('. $this->srcimg. ') is not formatted, only PNG, JPEG, GIF are supported. '); Return array (' data ' + = ', ' info ' = ' original picture ('. $this->srcimg. ') is not in the format, only PNG, JPEG, GIF are supported. ', ' status ' =>0); }} Private Function Waterimginfo () {//Get information about the watermark picture and load the picture. $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 picture ('. $this->srcimg. ') is not formatted, only PNG, JPEG, GIF are supported. '); Return array (' data ' = = ', ' info ' = ' watermark ' picture ('. $this->srcimg. ') is not in the format, only PNG, JPEG, GIF are supported. ', ' status ' =>0); }} 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://middle 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://middle 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://lower left $this->x = 0; $this->y = $this->srcimg_info[1]-$this->waterimg_info[1]; Break 1; Case 8://$this->x = ($this->srcimg_info[0]-$this->waterimg_info[0])/2; $this->y = $this->srcimg_info[1]-$this->waterimg_info[1]; Break 1; Case 9://Lower 100px $this->x = ($this->srcimg_info[0]-$this->waterimg_info[0])/2; $this->y = $this->srcimg_info[1]-$this->waterimg_info[1]-100; Break 1; Default://Lower right $this->x = $this->srcimg_info[0]-$this->waterimg_inFO[0]; $this->y = $this->srcimg_info[1]-$this->waterimg_info[1]; Break 1; }}/** * watermark text picture position, adjust according to demand */Private function Waterposstr () {$this->x = ($this->srcimg_info[0]-$this Waterimg_info[0])/2; $this->y = $this->srcimg_info[1]-$this->waterimg_info[1]-3; } Private Function waterimg ($type = ") {if ($this->srcimg_info[0] <= $this->waterimg_info[0] | | $this->src IMG_INFO[1] <= $this->waterimg_info[1]) {//die (' The watermark is bigger than the original! '); Return array (' data ' = = ', ' info ' = ' watermark ' is bigger than the original! ', ' status ' =>0); } if ($type = = ' Waterstr ') {$this->waterposstr (); }else{$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,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 ($type = ' waterstr '); } /** *Draw Rectangular Area * bool Imagefilledrectangle (resource $image, int $x 1, int $y 1, int $x 2, int $y 2, int $color) * BOOL Imag Erectangle (Resource $image, int $x 1, int $y 1, int $x 2, int $y 2, int $col) * @author liuzp111 * * Public functi On DrawRectangle () {//imagefill ($im, 0,0, $gray);//fill resource, fill coordinates (similar to PS magic wand), Color/* * 1--------------draw a rectangle-------------- * BOOL Imagerectangle (resource $image, int $x 1, int $y 1, int $x 2, int $y 2, int $col) * Parameter: Canvas resource, upper left corner x coordinate , upper left y coordinate, lower right x coordinate, lower right y coordinate, color */$color = imagecolorallocate ($this->im,255,255,255);//Create Rectangle border color and fill color//=============== ==========================================================//Draw rectangular area and fill//Parameter description://bool imagefilledrectangle (res Ource $image, int $x 1, int $y 1, int $x 2, int $y 2, int $color)//IM: To load an image as an image resource//$x 1: Represents the x-coordinate of the upper-left corner of the rectangle//$y 1: Table The y-coordinate of the upper-left corner of the display rectangle//$x 2: Represents the x-coordinate of the lower-right corner of the rectangle//$y 2: Represents the y-coordinate of the lower-right corner of the rectangle//$color: RGB color for the fill//Imagefilledrectangle ($this->im,3 , $this->srcimg_info[1]-->srcimg_info[0]-3, $this, $this->srcimg_info[1]-3, $color); Do not use the function below to fill, the bottom fill function for magic wand fill, easy to lead to fill incomplete//imagefill ($this->im, $this->srcimg_info[0]/2, $this->srcimg_info[1 ]-8, $color);//Fill the resource, fill the coordinates (magic wand), color} function output () {$this->imginfo (); Whether to create a rectangular region if ($this->is_draw_rectangle) {$this->drawrectangle (); } if ($this->watertypestr) {$this->waterstr (); } if ($this->watertypeimage) {$this->waterimginfo (); $this->waterimg (); } switch ($this->srcimg_info[2]) {case 3: $res _output = imagepng ($this->im, $this->output_img); Break 1; Case 2: $res _output = imagejpeg ($this->im, $this->output_img); Break 1; Case 1: $res _output = imagegif ($this->im, $this->output_img); Break 1; Default://Die (' Add watermark failed! '); Return array (' data ' = = ', ' info ' = ' Add watermark failed! ', ' status ' =>0); Break } Imagedestroy ($this->IM); Imagedestroy ($this->water_im); Return array (' data ' = $res _output, ' info ' + ' Add watermark success! ', ' status ' =>1); }}
How to use:
$file = ' 58368dddc8c51_22 ';//need to add watermark picture $file_ext = '. jpeg ';//extension $imgfilename = './'. $file. $file _ext;//need to add a watermark to the image path $obj = New Watermask ($imgFileName); Instantiate object $obj->watertypestr = true; Turn on text watermark $obj->watertypeimage = true; Open image watermark $obj->pos = 9; Define watermark Picture location $obj->waterimg = './water.png '; Watermark Picture $obj->transparent =; Watermark Transparency $obj->waterstr = ' Insurance broker: Liu Test Phone: 02052552 '; Watermark Text $obj->fontsize = 9; Text font Size $obj->fontcolor = Array (0,0,0); Watermark text color (RGB) $obj->fontfile = './FONT/MSYH.TTC '; Font file, here is Microsoft Jas Black $obj->is_draw_rectangle = TRUE; Open the Draw rectangle area $obj->output_img = './'. $file. ' _n '. $file the picture path of the _ext;//output $obj->output ();
The above is the whole content of this article, I hope that everyone's study has helped.