PHP Picture plus Chinese watermark implementation code

Source: Internet
Author: User
Tags character set int size rand

Example 1

The code is as follows Copy Code

<?php
Header ("Content-type:image/png"); /* Notification browser, to output the 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 of the font, depending on the operating system, can be Simhei.ttf (bold), Simkai. TTF (italics), Simfang. TTF (imitation), SimSun. TTC (XXFarEastFont-Arial & neo-Arial) and other GD-supported Chinese fonts * *
$str = Iconv (' GB2312 ', ' UTF-8 ', ' Chinese watermark '); /* Convert gb2312 Character set to UTF-8 character * *
Imagettftext ($im, 0, MB, $pink, $fontfile, $STR);
/* Add Chinese Watermark * *
Imagepng ($im);
Imagedestroy ($im);
?>

Example 2

The code is as follows Copy Code

<?php
// **************************************** //
Function: Add text to a picture
Parameters: $img picture file name
$new _img Save the picture file name, if blank indicates no save picture
$text String Contents
Text_size string Size
Text_angle Font string Output angle
text_x string Output x coordinates
text_y string output y-coordinate
$text _font Font file 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 TTF (TrueType Fonts) font text to a picture.
Parameter: size is the dimension of zigzag;
Angle for the angle of the font, clockwise calculation, 0 degrees to the level (from left to right), 90 degrees to the top of the text;
X,y two parameter is the coordinate value of the text (the origin point is upper left corner);
Col is the color of the word;
Fontfile is the name of the font file;
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, show only
Else
Imagegif ($im, $new _img); Save picture, but not show
endif

Imagedestroy ($im); End graphics, freeing up memory space
}
?>

Example 3

The code is as follows Copy Code

<?php
/*
* Features: PHP image watermark (watermark support picture or text)
* Parameters:
* $groundImage background image, that is, the need to add a watermark picture, temporarily support gif,jpg,png format;
* $waterPos watermark position, there are 10 kinds of states, 0 is 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 of the right;
* 7 to the bottom of the left, 8 for the bottom center, 9 for the bottom right;
* $waterImage image watermark, that is, as a watermark picture, only support gif,jpg,png format;
* $waterText text watermark, which is the text as a watermark, support ASCII code, does not support Chinese;
* $textFont text size, value is 1, 2, 3, 4 or 5, default to 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 $waterImage.
* When the $waterimage is valid, the parameters $waterstring, $stringFont, $stringColor do not take effect.
* The file name of the picture after the watermark is the same 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 = "Temporarily 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];//Gets the width of the watermark picture
$water _h = $water _info[1];//Gets the high watermark picture

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);
}
}

Reading background pictures
if (!emptyempty ($groundImage) && file_exists ($groundImage))
{
$ground _info = getimagesize ($groundImage);
$ground _w = $ground _info[0];//Gets the width of the background picture
$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 ("Need to watermark the picture does not exist!") ”);
}

Watermark Location
if ($isWaterImage)//Picture watermark
{
$w = $water _w;
$h = $water _h;
$label = "Picture";
}
else//text watermark
{
$temp = Imagettfbbox (ceil ($textFont *5), 0, "./cour.ttf", $waterText);//Get a 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 you want to watermark is more than the 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 for top center
$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 Central left
$posX = 0;
$posY = ($ground _h-$h)/2;
Break
Case 5://5 centered in middle
$posX = ($ground _w-$w)/2;
$posY = ($ground _h-$h)/2;
Break
Case 6://6 for Middle right
$posX = $ground _w-$w;
$posY = ($ground _h-$h)/2;
Break
Case 7://7 to 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 to 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
}

Set the blending mode of the 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 destination file
}
else//text watermark
{
if (!emptyempty ($textColor) && (strlen ($textColor) ==7))
{
$R = Hexdec (substr ($textColor, 1,2));
$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));
}

Picture after Watermark is generated
@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);
}

Free 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 "<bR>";
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 "<br>";
Echo $uploadfile;
if (copy ($_files[' userfile '] [' tmp_name '] [$i], $uploadfile))
{
echo "Ok<br>";

Text watermark
Imagewatermark ($uploadfile, 5, "" "," HTTP://www.lvye.info ", 5," #cccccc ");

Picture watermark
$waterImage = "logo_ok1.gif";//Watermark Picture Path
Imagewatermark ($uploadfile, 9, $waterImage);

echo "}
Else
{
echo "Fail<br>";
}
}
}
}

?>
<form enctype= "Multipart/form-data" method= "POST" >
<?php
for ($a =0; $a < $id; $a + +) {
echo "File: <input name=" userfile[] "type=" file "><br>";

}
?>
<input type= "Submit" value= "Upload" >
</form>

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.