Add Chinese watermark
<? Php
/*-------------------------------------------------------------
** Description: This is a custom class used to add a bottom watermark (not occupying the image display area) to a specified image. You need to create an object to call this class.
** Version: v1.0
** Creation: 2007-10-09
** Updated: 2007-10-09
** Staff: old fat ox ([email] fatkenme@163.com [/email] QQ: 70177108)
** Note: 1. the gd library is required and iconv is required (php5 already contains and does not need to be loaded)
2. Only applicable to three types of images: jpg, jpeg, gif, and png. Do not process other types of images.
3. Note that the attribute of the image directory must be writable.
4. Call example:
$ ObjImg = new MyWaterDownChinese ();
$ ObjImg-> Path = "images /";
$ ObjImg-> FileName = "1.jpg ";
$ ObjImg-> Text = "fat friend network [url] www.ppfriend.com [/url]";
$ ObjImg-> Font = "./font/simhei. ttf ";
$ ObjImg-> Run ();
** Member functions:
----------------------------------------------------------------*/
Class MyWaterDownChinese {
Var $ Path = "./"; // relative Path of the image directory to the page that calls this class
Var $ FileName = ""; // The name of the image, for example, JPG"
Var $ Text = ""; // The watermark Text to be added to the image. Chinese characters are supported.
Var $ TextColor = "# ffffff"; // text color. The font color must be black when the GIF image is used.
Var $ TextBgColor = "#000000"; // the color of the text background bar
Var $ Font = "c: // windows // fonts // simhei. ttf"; // directory for storing fonts, relative path
Var $ OverFlag = true; // specifies whether to overwrite the source image. If the source image is not overwritten by default, the system automatically adds "_ water_down" after the source image file name, for example, “1.jpg "=>" 1_water_down.jpg"
Var $ BaseWidth = 200; // The watermark text is added only when the image width is greater than or equal to 200.
//------------------------------------------------------------------
// Function: constructor of the class (php5.0 or above)
// Parameter: None
// Return: None
Function _ construct (){;}
//------------------------------------------------------------------
// Function: Class destructor (php5.0 or above)
// Parameter: None
// Return: None
Function _ destruct (){;}
//------------------------------------------------------------------
//------------------------------------
// Function: run the object function and add a watermark to the image.
// Parameter: None
// Return: None
Function Run ()
{
If ($ this-> FileName = "" | $ this-> Text = "")
Return;
// Check whether the GD library is installed
If (false = function_exists ("gd_info "))
{
Echo "the GD library is not installed in the system, and watermarks cannot be added to images .";
Return;
}
// Set the input and output image pathnames
$ 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]; // temporarily processed image, very important
If (! $ This-> OverFlag)
$ OutImg = $ this-> Path. $ arr_in_name [0]. "_ water_down.". $ arr_in_name [1];
// Check whether the image exists
If (! File_exists ($ inImg ))
Return;
// Obtain image attributes
$ GroundImageType = @ getimagesize ($ inImg );
$ ImgWidth = $ groundImageType [0];
$ ImgHeight = $ groundImageType [1];
$ ImgType = $ groundImageType [2];
If ($ imgWidth <$ this-> BaseWidth) // smaller than the basic width, not processed
Return;
// When the image is not jpg/jpeg/gif/png, It is not processed
Switch ($ imgType)
{
Case 1:
$ Image = imagecreatefromgif ($ inImg );
$ This-> TextBgColor = "# ffffff"; // The GIF image font can only be black, so the background color is white.
Break;
Case 2:
$ Image = imagecreatefromjpeg ($ inImg );
Break;
Case 3:
$ Image = imagecreatefrompng ($ inImg );
Break;
Default:
Return;
Break;
}
// Create a 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 image. 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 in the background color of the image
@ Imagefill ($ objTmpImg, 0, 0, $ colorBg );
// Copy the source image to the temporary Image
@ Imagecopy ($ objTmpImg, $ image, 0, 0, 0, $ imgWidth, $ imgHeight );
// Create the watermark Text object to be written
$ ObjText = $ this-> createText ($ this-> Text );
// Calculate the position 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 );
// Generate a new image and a temporary Image
Switch ($ imgType)
{
Case 1:
Imagegif ($ objTmpImg, $ tmpImg );
Break;
Case 2:
Imagejpeg ($ objTmpImg, $ tmpImg );
Break;
Case 3:
Imagepng ($ objTmpImg, $ tmpImg );
Break;
Default:
Return;
Break;
}
// Release resources
@ Imagedestroy ($ objTmpImg );
@ Imagedestroy ($ image );
// Rename the file
If ($ this-> OverFlag)
{
// Overwrite the source Image
@ Unlink ($ inImg );
@ Rename ($ tmpImg, $ outImg );
}
Else
{
// Do not overwrite the source Image
@ Rename ($ tmpImg, $ outImg );
}
}
//--------------------------------------
// Function: Create a watermark Text object
// Parameter: None
// Return: The 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
?>