- /*
- * Function: PHP image watermark (watermark supports picture or text)
- Parameters
- * $groundimage background image, that is, need to add a watermark image, temporarily only support gif,jpg,png format;
- * $waterpos watermark position, there are 10 kinds of states, 0 is a random position;
- * For the top of the left, 2 for the top center, 3 for the top right;
- In the middle of the left, 5 for the Middle center, 6 for the middle right;
- *7 for the bottom of the left, 8 for the bottom center, 9 for the bottom of the right;
- * $waterimage image watermark, that is, as a watermark image, temporarily only support gif,jpg,png format;
- * $watertext text watermark, that is, the text as a watermark, support ASCII code, does not support Chinese;
- * $textfont Text size, value 1, 2, 3, 4 or 5, default is 5;
- * $textcolor text color, value is hexadecimal color value, default is #ff0000 (red);
- *
- * Note: Support GD 2.0,support freetype, GIF read, GIF create, JPG, png
- * $waterimage and $watertext best not to use at the same time, choose one of them, priority to use the $waterimage.
- * When the $waterimage is valid, the parameters $waterstring, $stringfont, $stringcolor are not effective.
- * The watermark image has the same file name as the $groundimage.
- * Author: longware @ 2004-11-3 14:15:13
- */
- function Imagewatermark ($groundimage, $waterpos =0, $waterimage = "", $watertext = "", $textfont =5, $textcolor = "#ff0000")
- {
- $iswaterimage = false;
- $formatmsg = "Does not support this file format, please use the picture processing software to convert the picture to GIF, JPG, PNG format. ";
- Read Watermark File
- if (!empty ($waterimage) && file_exists ($waterimage))
- {
- $iswaterimage = true;
- $water _info = getimagesize ($waterimage);
- $water _w = $water _info[0];//To obtain the width of the watermark picture
- $water _h = $water _info[1];//To obtain a high watermark image
- Switch ($water _info[2])//Get the format of the watermark picture
- {
- Case 1: $water _im = Imagecreatefromgif ($waterimage);
- Case 2: $water _im = Imagecreatefromjpeg ($waterimage);
- Case 3: $water _im = Imagecreatefrompng ($waterimage);
- Default:die ($FORMATMSG);
- }
- }
- Read background picture
- if (!empty ($groundimage) && file_exists ($groundimage))
- {
- $ground _info = getimagesize ($groundimage);
- $ground _w = $ground _info[0];//Get the width of the background image
- $ground _h = $ground _info[1];//get a high background picture
- Switch ($ground _info[2])//Get the format of the background picture
- {
- Case 1: $ground _im = Imagecreatefromgif ($groundimage);
- Case 2: $ground _im = Imagecreatefromjpeg ($groundimage);
- Case 3: $ground _im = Imagecreatefrompng ($groundimage);
- Default:die ($FORMATMSG);
- }
- }
- Else
- {
- Die ("The picture that needs watermark does not exist!") ");
- }
- Watermark Location
- if ($iswaterimage)//Picture watermark
- {
- $w = $water _w;
- $h = $water _h;
- $label = "Picture";
- }
- else//text watermark
- {
- $temp = Imagettfbbox (ceil ($textfont *2.5), 0, "./cour.ttf", $watertext);//Gets the range of text that uses TrueType fonts
- $w = $temp [2]-$temp [6];
- $h = $temp [3]-$temp [7];
- Unset ($temp);
- $label = "Text Area";
- }
- if ($ground _w< $w) | | ($ground _h< $h))
- {
- echo "The length or width of the picture that needs to be watermarked is more than a watermark". $label. " Still small, unable to generate watermark! ";
- Return
- }
- Switch ($WATERPOS)
- {
- Case 0://Random
- $POSX = rand (0, ($ground _w-$w));
- $posy = rand (0, ($ground _h-$h));
- Break
- Case 1://1 for top left
- $POSX = 0;
- $posy = 0;
- Break
- Case 2://2 centered on the top
- $posx = ($ground _w-$w)/2;
- $posy = 0;
- Break
- Case 3://3 for top right
- $POSX = $ground _w-$w;
- $posy = 0;
- Break
- Case 4://4 for the middle left
- $POSX = 0;
- $posy = ($ground _h-$h)/2;
- Break
- Case 5://5 centered in the middle
- $posx = ($ground _w-$w)/2;
- $posy = ($ground _h-$h)/2;
- Break
- Case 6://6 for the middle right
- $POSX = $ground _w-$w;
- $posy = ($ground _h-$h)/2;
- Break
- Case 7://7 for Bottom left
- $POSX = 0;
- $posy = $ground _h-$h;
- Break
- Case 8://8 centered at bottom
- $posx = ($ground _w-$w)/2;
- $posy = $ground _h-$h;
- Break
- Case 9://9 for bottom right
- $POSX = $ground _w-$w;
- $posy = $ground _h-$h;
- Break
- default://Random
- $POSX = rand (0, ($ground _w-$w));
- $posy = rand (0, ($ground _h-$h));
- Break
- }
- To set the color blending mode of an image
- Imagealphablending ($ground _im, true);
- if ($iswaterimage)//Picture watermark
- {
- Imagecopy ($ground _im, $water _im, $posx, $posy, 0, 0, $water _w, $water _h);//copy watermark to target file
- }
- else//text watermark
- {
- if (!empty ($textcolor) && (strlen ($textcolor) ==7))
- {
- $r = Hexdec (substr ($textcolor,));
- $g = Hexdec (substr ($textcolor, 3,2));
- $b = Hexdec (substr ($textcolor, 5));
- }
- Else
- {
- Die ("Watermark text color format is not correct! ");
- }
- Imagestring ($ground _im, $textfont, $posx, $posy, $watertext, Imagecolorallocate ($ground _im, $r, $g, $b));
- }
- Create a picture after a watermark
- @unlink ($groundimage);
- Switch ($ground _info[2])//Get the format of the background picture
- {
- Case 1:imagegif ($ground _im, $groundimage);
- Case 2:imagejpeg ($ground _im, $groundimage);
- Case 3:imagepng ($ground _im, $groundimage);
- Default:die ($ERRORMSG);
- }
- Freeing memory
- if (Isset ($water _info)) unset ($water _info);
- if (Isset ($water _im)) Imagedestroy ($water _im);
- unset ($ground _info);
- Imagedestroy ($ground _im);
- }
- //--------
- if (Isset ($_files) &&!empty ($_files[' userfile ']) && $_files[' userfile ' [' Size ']>0]
- {
- $uploadfile = "./". Time (). " _ ". $_files[' userfile ' [' name '];
- if (copy ($_files[' userfile ' [' tmp_name '], $uploadfile))
- {
- echo "OK
";
- Text watermark
- Imagewatermark ($uploadfile, 0, "", "http://blog.csdn.net/longware/", 5, "#ff0000");
- Image watermark
- $waterimage = "./hanweb_shuiyin.gif";//Watermark Picture Path
- Imagewatermark ($uploadfile, 0, $waterimage);
- echo "";
- }
- Else
- {
- echo "fail
";
- }
- }
- ?>
Copy Code |