- Header ("Content-type:image/png"); /* Notify browser, to output image */
- $im = Imagecreate (400, 300); /* Define the size of the image */
- $gray = Imagecolorallocate ($im, 235, 235, 235);
- $pink = Imagecolorallocate ($im, 255, 128, 255);
- $fontfile = "Simkai.ttf";
- /* $fontfile the path to the font, depending on the operating system, can be Simhei.ttf (bold), Simkai. TTF (in italics), Simfang. TTF (imitation), SimSun. TTC (Arial & XXFarEastFont-Arial) and other GD-supported Chinese fonts */
- $str = Iconv (' GB2312 ', ' UTF-8 ', ' Chinese watermark '); /* Convert the gb2312 character set to UTF-8 characters */
- Imagettftext ($im, 0, $pink, $fontfile, $STR);
- /* Add Chinese watermark */
- Imagepng ($im);
- Imagedestroy ($im);
- ?>
Copy CodeExample 2,php image watermark code.
- // **************************************** //
- Function: Add text to a picture
- Parameters: $img picture file name
- $new _img Save picture file name, if empty indicates no picture is saved
- $text string Content
- Text_size string Size
- Text_angle Font string Output angle
- text_x string output x-coordinate
- text_y string output y-coordinate
- $text _font Font Name
- $r, $g, $b string color RGB value
- // **************************************** //
- function Img_text ($img, $new _img, $text, $text _size, $text _angle, $text _x, $text _y, $text _font, $r, $g, $b) {
- $text =iconv ("gb2312", "UTF-8", $text);
- Header ("Content-type:image/gif");
- $im = @imagecreatefromstring (file_get_contents ($img)) or Die ("Open picture Failed!");
- $color = Imagecolorallocate ($im, $r, $g, $b);
- Imagettftext (int im, int size, int angle, int x, int y, int col, string fontfile, string text):
- This function writes the TTF (TrueType Fonts) font text to the picture.
- Parameter: size is the dimension of the glyph;
- Angle for the angle of the font, clockwise calculation, 0 degrees to the horizontal (from left to right), 90 degrees from the bottom to the top of the text;
- The x, y two parameter is the coordinate value of the text (the origin is the upper left corner);
- Col is the color of the word;
- Fontfile is the font file name;
- Text is the string content.
- Imagettftext ($im, $text _size, $text _angle, $text _x, $text _y, $color, $text _font, $text);
- if ($new _img== ""):
- Imagegif ($im); Do not save pictures, only show
- Else
- Imagegif ($im, $new _img); Save picture, but not display
- endif
- Imagedestroy ($im); End graph, free memory space
- }
- ?>
Copy CodeExample 3,php image watermark, support PHP text watermark effect.
- /*
- * 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;
- * 1 for the top left, 2 for the top center, 3 for the top right;
- * 4 for 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 (!emptyempty ($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 (!emptyempty ($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), 0, "./cour.ttf", $waterText);//Get 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 (!emptyempty ($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);
- }
- —————————————————————————————
- $id =$_request[' id '];
- $num = count ($_files[' userfile ' [' name ']);
- Print_r ($_files[' userfile ');
- Print_r ($_files[' userfile ' [' name ']);
- Echo $num;
- echo "
”;
- if (Isset ($id)) {
- for ($i =0; $i < $id; $i + +) {
- if (Isset ($_files) &&!emptyempty ($_files[' userfile ']) && $_files[' userfile ' [' Size ']>0]
- {
- $uploadfile = "./". Time (). " _ ". $_files[' UserFile '][name][$i];
- echo "
”;
- Echo $uploadfile;
- if (copy ($_files[' userfile ' [' tmp_name '] [$i], $uploadfile))
- {
- echo "OK
”;
- Text watermark
- Imagewatermark ($uploadfile, 5, "", "HTTP://www.lvye.info", 5, "#cccccc");
- Image watermark
- $waterImage = "logo_ok1.gif";//Watermark Picture Path
- Imagewatermark ($uploadfile, 9, $waterImage);
- echo "";
- }
- Else
- {
- echo "Fail
”;
- }
- }
- }
- }
- ?>
Copy CodeCode 4 Add Chinese watermark
- /*-----
- * * Description: This is a custom class used to add a bottom watermark to the specified picture (without occupying the picture display area), and to create an object call
- * * Created: 2007-10-09
- * * Updated: 2007-10-09
- * * Description: 1, the need for GD library support, need ICONV support (PHP5 already included without loading)
- 2, only suitable for three kinds of pictures, jpg/jpeg/gif/png, other types do not handle
- 3. Note that the attributes of the directory in which the picture is located must be writable
- 4. Call Example:
- $OBJIMG = new Mywaterdownchinese ();
- $OBJIMG->path = "images/";
- $OBJIMG->filename = "1.jpg";
- $OBJIMG->text = "HAHAKONGJIANHTTP://HI.BAIDU.COM/LYSONCN";
- $OBJIMG->font = "./font/simhei.ttf";
- $OBJIMG->run ();
- * * member function:
- --------------*/
- Class mywaterdownchinese{
- var $Path = "./"; The relative path of the directory in which the picture is located relative to the page calling this class
- var $FileName = ""; The name of the picture, such as "1.jpg"
- var $Text = ""; Watermark text to be added to the image, support Chinese
- var $TextColor = "#ffffff"; Text color, GIF picture, font color can only be black
- var $TextBgColor = "#000000"; The color of the text's background bar
- var $Font = "C://windows//fonts//simhei.ttf"; Font storage directory, relative path
- var $OverFlag = true; Whether to overwrite the original, default to overwrite, when not covered, automatically after the original file name + "_water_down", such as "1.jpg" = "1_water_down.jpg"
- var $BaseWidth = 200; The width of the picture must be at least >=200, and the watermark text will be added.
- //--------------
- Function: constructor of Class (Form php5.0 or more)
- Parameters: None
- return: None
- function __construct () {;}
- //------------------------
- Function: destructor of class (form above php5.0)
- Parameters: None
- return: None
- function __destruct () {;}
- //----------------------
- Function: Object run function, add watermark to Picture
- Parameters: None
- return: None
- function Run ()
- {
- if ($this->filename = = "" | | $this->text = = "")
- Return
- Detects if the GD library is installed
- if (false = = Function_exists ("Gd_info"))
- {
- echo "System does not have the GD library installed, can not add watermark to the picture.";
- Return
- }
- Set input, output picture path name
- $arr _in_name = Explode (".", $this->filename);
- //
- $INIMG = $this->path. $this->filename;
- $OUTIMG = $INIMG;
- $TMPIMG = $this->path. $arr _in_name[0]. " _tmp. ". $arr _in_name[1]; Temporary processing of pictures, very important
- if (! $this->overflag)
- $OUTIMG = $this->path. $arr _in_name[0]. " _water_down. ". $arr _in_name[1];
- Detect if a picture exists
- if (!file_exists ($INIMG))
- return;
- Get the properties of a picture
- $groundImageType = @getimagesize ($INIMG);
- $imgWidth = $groundImageType [0];
- $imgHeight = $groundImageType [1];
- $imgType = $groundImageType [2];
- if ($imgWidth < $this->basewidth)//Less than basic width, not processed
- Return
- The picture is not jpg/jpeg/gif/png when it is not processed
- Switch ($imgType)
- {
- Case 1:
- $image = Imagecreatefromgif ($INIMG);
- $this->textbgcolor = "#ffffff"; GIF picture font can only be black, so the background color is set to white
- Break
- Case 2:
- $image = Imagecreatefromjpeg ($INIMG);
- Break
- Case 3:
- $image = Imagecreatefrompng ($INIMG);
- Break
- Default
- Return
- Break
- }
- Create color
- $color = @imagecolorallocate ($image, Hexdec (substr ($this->textcolor,1,2)), Hexdec (substr ($this->textcolor, 3,2)), Hexdec (substr ($this->textcolor,5,2))); Text color
- Generate an empty picture, its height increases the watermark height at the bottom
- $newHeight = $imgHeight +20;
- $OBJTMPIMG = @imagecreatetruecolor ($imgWidth, $newHeight);
- $colorBg = @imagecolorallocate ($objTmpImg, Hexdec (substr ($this->textbgcolor,1,2)), Hexdec (substr ($this- textbgcolor,3,2)), Hexdec (substr ($this->textbgcolor,5,2))); Background color
- Fill the background color of the picture
- @imagefill ($OBJTMPIMG, 0,0, $colorBg);
- Copy the original to a temporary image
- @imagecopy ($OBJTMPIMG, $image, 0,0,0,0, $imgWidth, $imgHeight);
- Create a watermark text object to write to
- $objText = $this->createtext ($this->text);
- Calculate the location of the watermark text to be written
- $x = 5;
- $y = $newHeight-5;
- Write a text watermark
- @imagettftext ($OBJTMPIMG, 10,0, $x, $y, $color, $this->font, $objText);
- Create a new picture, temporary picture
- Switch ($imgType)
- {
- Case 1:
- Imagegif ($OBJTMPIMG, $TMPIMG);
- Break
- Case 2:
- Imagejpeg ($OBJTMPIMG, $TMPIMG);
- Break
- Case 3:
- Imagepng ($OBJTMPIMG, $TMPIMG);
- Break
- Default
- Return
- Break
- }
- Freeing resources
- @imagedestroy ($OBJTMPIMG);
- @imagedestroy ($image);
- Renaming files
- if ($this->overflag)
- {
- Overwrite the original
- @unlink ($INIMG);
- @rename ($TMPIMG, $OUTIMG);
- }
- Else
- {
- Do not overwrite the original
- @rename ($TMPIMG, $OUTIMG);
- }
- }
- //----------
- Function: Create watermark Text Object
- Parameters: None
- Return: Created Watermark text Object
- function CreateText ($instring)
- {
- $outstring = "";
- $max =strlen ($instring);
- for ($i =0; $i < $max; $i + +)
- {
- $h =ord ($instring [$i]);
- if ($h >=160 && $i < $max-1)
- {
- $outstring. = "Base_convert" (Bin2Hex (Iconv ("gb2312", "Ucs-2", substr ($instring, $i, 2)), 16,10). ";";
- $i + +;
- }
- Else
- {
- $outstring. = $instring [$i];
- }
- }
- return $outstring;
- }
- }//class
- ?>
Copy Code |