Looking for a long time, the results are not automatically wrapped, and go online to add the automatic line
Tired, don't want it.
<?phpheader ("content-type:text/html; Charset=utf-8 "), class watermask{public $waterType = 0; Watermark Type: 0 for text watermark, 1 for picture watermark public $pos = 5; Watermark Location Public $transparent = 45; Watermark Transparency Public $waterStr = ' www.codefans.net '; Watermark Text Public $fontSize = 16; Text font size Public $fontColor = Array (255,255,255); Watermark text color (RGB) Public $fontFile = ' arial.ttf ';//font file public $waterImg = ' logo.png ';//Watermark Picture PU Blic $srcImg = ";//need to add a watermark image 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 function __construct ($img) {$this->srcimg = file_exists ($img)? $img: Die (' ". $img. '" Sorry, the watermark file does not exist! ‘); } Private Function Imginfo () {//Get watermark picture information and load. $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 (' Watermark picture ('. $this->srcimg. ') The watermark picture is not formatted, please select a PNG, JPEG, GIF format. ‘); }} Private Function Waterimginfo () {//Get watermark picture and load. $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. ') Format is not correct, only PNG, JPEG, GIF 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://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; 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; }} Private Function Waterimg () {if ($this->srcimg_info[0] <= $this->waterimg_info[0] | | $this SRCIMG_INFO[1] <= $this->waterimg_info[1]) {die (' the watermark is bigger than the original! ‘); } $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 () {$this->waterstr = $this->autowrap ($this->fontsize, 0, $this->fontfile, $this->waterstr, 250); Wrap-Wrapping echo $this->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 (); } functionAutoWrap ($fontsize, $angle, $fontface, $string, $width) {//These variables are font size, angle, font name, string, preset width $content = ""; The string is split into a single word saved to the array letter for ($i =0; $i <mb_strlen ($string); $i + +) {$letter [] = Mb_substr ($stri Ng, $i, 1); } foreach ($letter as $l) {$teststr = $content. " ". $l; $testbox = Imagettfbbox ($fontsize, $angle, $fontface, $TESTSTR); Determines whether the stitched string exceeds the preset width if ($testbox [2] > $width) && ($content!== "")) {$content. = "\ n"; } $content. = $l; } return $content; } 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 (' Reason unknown: watermark addition failed! ‘); Break } Imagedestroy ($this->im); Imagedestroy ($this->water_im); }}?>
Here is the page code
Include ("watermask.php"); $imgFileName = "$loadpath"; $obj = new Watermask ($imgFileName); $obj->watertype = 0;//watermark Type: 0 for text watermark, 1 for picture watermark $obj->transparent = 100;//watermark Transparency $obj->waterstr = $_post[" Content "];//watermark text $obj->fontsize = 20;//font size $obj->fontcolor = Array (0,0,0);//Watermark text color (RGB value) $obj- >fontfile = ' D:/www/wenzi1.ttf '; Font name $obj->output ();//Output watermark Picture
PHP Text watermark function