PHP adds Chinese watermark _php tutorial to images

Source: Internet
Author: User
Tags add watermark to picture
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
* * Version: v1.0
* * Created: 2007-10-09
* * Updated: 2007-10-09
* * Person: Old fat cow ([email]fatkenme@163.com[/email] qq:70177108)
* * 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 = "Chubby dating net [Url]www.ppfriend.com[/url]";
$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
?>


http://www.bkjia.com/PHPjc/445139.html www.bkjia.com true http://www.bkjia.com/PHPjc/445139.html techarticle Add a Chinese watermark? PHP/*-------------------------------------------------------------* * Description: This is a custom image to add a bottom watermark to the specified picture (without occupying the picture display area) ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.