PHP to add watermark to the image class
Src_image_name = Strtolower (Trim ($filename)), $src _image_type = $this->get_type ($this->src_image_name); $src _ Image = $this->createimage ($src _image_type, $this->src_image_name), if (! $src _image) return; $SRC _image_w= Imagesx ($src _image), $src _image_h=imagesy ($src _image), if ($this->wm_image_name) {$this->wm_image_name = Strtolower (Trim ($this->wm_image_name)), $wm _image_type = $this->get_type ($this->wm_image_name); $WM _image = $this->createimage ($wm _image_type, $this->wm_image_name); $wm _image_w=imagesx ($wm _image); $WM _image_h= Imagesy ($wm _image); $temp _wm_image = $this->getpos ($src _image_w, $src _image_h, $this->wm_image_pos, $wm _image) ; $wm _image_x = $temp _wm_image["dest_x"]; $WM _image_y = $temp _wm_image["dest_y"];imagecopymerge ($src _image, $wm _image , $wm _image_x, $wm _image_y,0,0, $wm _image_w, $wm _image_h, $this->wm_image_transition);} if ($this->wm_text) {$this->wm_text = $this->gb2utf8 ($this->wm_text); $temp _wm_text = $this->getpos ($ Src_image_w, $src _image_h, $this->wm_text_pos); $wm _text_x = $temp _wm_text["dest_x"]; $WM _text_y = $temp _wm_text["dest_y"];if (Preg_match ("/ ([a-f0-9][a-f0-9]) ([a-f0-9][a-f0-9]) ([a-f0-9][a-f0-9])/i ", $this->wm_text_color, $color)) {$red = Hexdec ($color [1]); $green = Hexdec ($color [2]); $blue = Hexdec ($color [3]); $WM _text_color = Imagecolorallocate ($src _image, $red, $green, $blue);} else{$wm _text_color = imagecolorallocate ($src _image, 255,255,255);} Imagettftext ($src _image, $this->wm_text_size, $this->wm_angle, $wm _text_x, $wm _text_y, $wm _text_color, $this- >wm_text_font, $this->wm_text);} if ($this->save_file) {switch ($this->output_type) {case ' gif ': $src _img=imagepng ($src _image, $this->save_ file); Break;case ' JPEG ': $src _img=imagejpeg ($src _image, $this->save_file, $this->jpeg_quality); Break;case ' png ': $src _img=imagepng ($src _image, $this->save_file); Break;default: $src _img=imagejpeg ($src _image, $this->save_file, $this->jpeg_quality); Break;}} Else{if ($src _image_type = "jpg") $src _image_tyPe= "JPEG", Header ("Content-type:image/{$src _image_type}"), switch ($src _image_type) {case ' gif ': $SRC _img=imagepng ($ Src_image); Break;case ' jpg ': $src _img=imagejpeg ($src _image, "", $this->jpeg_quality); Break;case ' png ': $SRC _img=imagepng ($ Src_image) Break;default: $src _img=imagejpeg ($src _image, "", $this->jpeg_quality); Imagedestroy ($src _image);} /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/*createimage Create a picture intrinsic function based on file name and type $type: The type of picture, including GIF, Jpg,png$img_name: Picture file name, including path name, for example "./mouse.jpg" */function createimage ($type, $img _name) {if (! $type) {$type = $this Get_type ($img _name);} Switch ($type) {case ' gif ': if (function_exists (' imagecreatefromgif ')) [email protected] ($img _name); Break;case ' JPG ': $tmp _img=imagecreatefromjpeg ($img _name); Break;case ' png ': $tmp _img=imagecreatefrompng ($img _name); break; Default: $tmp _img=imagecreatefromstring ($img _name); return $tmp _img;} /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++getpos according to the source image length, width, position code, watermarkImage ID to create a position where the watermark is placed in the source image the inner function $sourcefile_width: The width $sourcefile_height of the source image: The high $pos of the original image: Position code//0 = middle//1 = top left//2 = Top right//3 = bottom right//4 = bottom left//5 = top middle//6 = middle right//7 = bottom middle//8 = Middle Left$wm_im Age: Watermark Picture id*/function GetPos ($sourcefile _width, $sourcefile _height, $pos, $wm _image= "") {if ($wm _image) {$insertfile _ width = imagesx ($wm _image), $insertfile _height = Imagesy ($wm _image);} else {$lineCount = explode ("\ r \ n", $this->wm_text); $fontSize = Imagettfbbox ($this->wm_text_size, $this->wm_ Text_angle, $this->wm_text_font, $this->wm_text), $insertfile _width = $fontSize [2]-$fontSize [0]; $insertfile _ Height = count ($lineCount) * ($fontSize [1]-$fontSize [3]);} Switch ($pos) {case 0: $dest _x = ($sourcefile _width/2)-($insertfile _width/2); $dest _y = ($sourcefile _height/2) -($insertfile _HEIGHT/2); Break;case 1: $dest _x = 0;if ($this->wm_text) {$dest _y = $insertfile _height;} else{$dest _y = 0;} Break;case 2: $dest _x = $sourcefile _width-$iNsertfile_width;if ($this->wm_text) {$dest _y = $insertfile _height;} else{$dest _y = 0;} Break;case 3: $dest _x = $sourcefile _width-$insertfile _width; $dest _y = $sourcefile _height-$insertfile _height;break; Case 4: $dest _x = 0; $dest _y = $sourcefile _height-$insertfile _height;break;case 5: $dest _x = (($sourcefile _width-$inser Tfile_width)/2), if ($this->wm_text) {$dest _y = $insertfile _height;} else{$dest _y = 0;} Break;case 6: $dest _x = $sourcefile _width-$insertfile _width; $dest _y = ($sourcefile _height/2)-($insertfile _height/ 2); Break;case 7: $dest _x = (($sourcefile _width-$insertfile _width)/2); $dest _y = $sourcefile _height-$insertfile _h Eight;break;case 8: $dest _x = 0; $dest _y = ($sourcefile _height/2)-($insertfile _HEIGHT/2); Break;default: $dest _x = $ sourcefile_width-$insertfile _width, $dest _y = $sourcefile _height-$insertfile _height;break;} Return Array ("dest_x" = $dest _x, "dest_y" and "= $dest _y);} /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Gb2utf8 the specified text to the UTF-8 format, including mixed internal functions */function Gb2utf8 ($GB) {if (!trim ($GB)) return $GB; $filename = "./gb2312.txt"; $tmp =file ($filename); $codetable =array (), while (List ($key, $value) =each ($tmp)) $codetable [Hexdec (substr ($value, 0,6))]= substr ($value, 7,6), $utf 8= "", while ($GB) {if (Ord (substr ($GB, 0,1)) >127) {$tthis =substr ($GB, 0,2); $GB =substr ($GB, 2 , strlen ($GB)-2); $utf 8.= $this->u2utf8 (Hexdec ($codetable [Hexdec (Bin2Hex ($tthis)) -0x8080]));} else{$tthis =substr ($GB, 0,1); $GB =substr ($GB, 1,strlen ($GB)-1); $utf 8.= $this->u2utf8 ($tthis);}} return $UTF 8;} function U2utf8 ($c) {$str = "", if ($c < 0x80) {$str. = $c;} else if ($c < 0x800) {$str. =CHR (0xC0 | $c >>6); $str. =CHR (0x80 | $c & 0x3F);} else if ($c < 0x10000) {$str. =CHR (0xE0 | $c >>12), $str. =CHR (0x80 | $c >>6 & 0x3F); $str. =CHR (0x80 | $c & ; 0x3F);} else if ($c < 0x200000) {$str. =CHR (0xF0 | $c >>18), $str. =CHR (0x80 | $c >>12 & 0x3F); $str. =CHR (0x80 | $c ; >6 & 0x3F); $str. =CHR (0x80 | $c & 0x3F);} return $STR;} /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++get_type get the format of the picture, including the jpg,png,gif intrinsic function $img_name: Picture file name, You can include the path name */function get_type ($img _name)//Get the image file type {$name _array = explode (".", $img _name); if (Preg_match ("/\" ( Jpg|jpeg|gif|png) $/", $img _name, $matches)) {$type = Strtolower ($matches [1]);} else{$type = "string";} return $type;}}? >
How to use:
$img = new Gimage ();
$img->wm_text = "www.discuz.com";
$img->wm_text_font = "./stxinwei. TTF ";
$img->create ("./mouse.jpg");
On it, where
Mouse.jpg is the name of the image on which you want to add a watermark, note that the path name is included
Stxinwei. TTF is the path name of the font file + file name
This is a simple test. If you want to adjust the more complex display effect, just modify the properties in the class, for example, to enlarge the font can be
$img->wm_text_size = 20;
Add a watermark image to
$img->wm_image_name= "filename";