Add Chinese watermark
<?php
/*-------------------------------------------------------------
* * Description: This is a custom class that is used to add the bottom watermark to the specified picture (without occupying the picture display area), you need to create an object call
* * Version: v1.0
* * Created: 2007-10-09
* * Update: 2007-10-09
* * personnel: Old fat ox ([email]fatkenme@163.com[/email] qq:70177108)
* * Description: 1, need to GD library support, need ICONV support (PHP5 already contains no load)
2, only suitable for three kinds of pictures, jpg/jpeg/gif/png, other types do not deal with
3, note that the image of the directory properties must be writable
4. Call Example:
$OBJIMG = new Mywaterdownchinese ();
$OBJIMG->path = "images/";
$OBJIMG->filename = "1.jpg";
$OBJIMG->text = "Fat Dating network [url]www.ppfriend.com[/url]";
$OBJIMG->font = "./font/simhei.ttf";
$OBJIMG->run ();
* * member function:
----------------------------------------------------------------*/
Class mywaterdownchinese{
var $Path = "./"; The relative path of the picture's directory relative to the page that calls this class
var $FileName = ""; The name of the picture, such as "1.jpg"
var $Text = ""; Images to be added to the watermark text, support Chinese
var $TextColor = "#ffffff"; The color of the text, when the GIF picture, the font color can only be black
var $TextBgColor = "#000000"; The color of the background bar of the text
var $Font = "C://windows//fonts//simhei.ttf"; Font storage directory, relative path
var $OverFlag = true; Whether to overwrite the original image, the default is cover, do not cover, 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 to add the watermark text.
//------------------------------------------------------------------
Function: class constructor (php5.0 above)
Parameters: None
Back: None
function __construct () {;}
//------------------------------------------------------------------
Function: Class destructor (php5.0 above)
Parameters: None
Back: None
function __destruct () {;}
//------------------------------------------------------------------
//------------------------------------
Function: Object run function, add watermark to Picture
Parameters: None
Back: None
function Run ()
{
if ($this->filename = "" | | $this->text = "")
Return
Detect 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 and output picture path names
$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 the base width, not processed
Return
When the picture is not jpg/jpeg/gif/png, 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
Generates an empty picture, and 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 a picture
@imagefill ($OBJTMPIMG, 0,0, $colorBg);
Copy the original to the temporary picture
@imagecopy ($OBJTMPIMG, $image, 0,0,0,0, $imgWidth, $imgHeight);
Create a watermark text object to write
$objText = $this->createtext ($this->text);
Calculates the position of the watermark text to write
$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
}
Releasing resources
@imagedestroy ($OBJTMPIMG);
@imagedestroy ($image);
Renaming files
if ($this->overflag)
{
Cover artwork
@unlink ($INIMG);
@rename ($TMPIMG, $OUTIMG);
}
Else
{
Do not overwrite original artwork
@rename ($TMPIMG, $OUTIMG);
}
}
//--------------------------------------
Features: Creating Watermark Text objects
Parameters: None
Back: The watermark text object created
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
?>