PHP Transparent Background Watermark program Code _php Tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg unsupported
Add watermark to the picture is a common and very useful function of PHP program, let me give you a detailed introduction to the PHP PNG transparent background Watermark program code bar.
The code is as follows Copy Code

Original image

$DST = "/upload/20120914/20120914040740-0.jpg"; Note that the picture path should be correct

Get the original picture information

$DST _info = getimagesize ($DST);

Switch ($DST _info[2])

{

Case 1:

$DST _im =imagecreatefromgif ($DST);

Case 2:

$DST _im =imagecreatefromjpeg ($DST);

Case 3:

$DST _im =imagecreatefrompng ($DST);

Case 6:

$DST _im =imagecreatefromwbmp ($DST);

Default

Die ("Unsupported file type 1"); exit;

}

Watermark Image

$SRC = "/images/shuiyin.png"; Note the path to write to

$src _info = getimagesize ($SRC);

Switch ($src _info[2])

{

Case 1:

$SRC _im =imagecreatefromgif ($SRC);

Case 2:

$SRC _im =imagecreatefromjpeg ($SRC);

Case 3:

$SRC _im =imagecreatefrompng ($SRC);

Case 6:

$SRC _im =imagecreatefromwbmp ($SRC);

Default

Die ("Unsupported file type 1"); exit;

}

Translucent format watermark

$alpha = 50;//Watermark Transparency

Imagecopymerge ($dst _im, $src _im, $dst _info[0]-$src _info[0]-10, $dst _info[1]-$src _info[1]-

10,0,0, $src _info[0], $src _info[1], $alpha);

Ways to support PNG's own transparency

Imagecopy ($dst _im, $src _im, $dst _info[0]-$src _info[0]-10, $dst _info[1]-$src _info[1]-10,0,0, $src _info

[0], $src _info[1]);

Save picture

Switch ($DST _info[2]) {

Case 1:

Imagegif ($dst _im, $DST);

Case 2:

Imagejpeg ($dst _im, $DST);

Case 3:

Imagepng ($dst _im, $DST);

Case 6:

Imagewbmp ($dst _im, $DST);

Default

Die ("Unsupported file type 2"); exit;

}

Imagedestroy ($dst _im);

Imagedestroy ($src _im);


Give a standard picture watermark class

The code is as follows Copy Code

Find the Watermark code on the Internet:
/*
* Function: PHP image watermark (watermark supports picture or text)
Parameters
* $product _img 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 $product _img.
* Author: longware @ 2004-11-3 14:15:13
*/
function Imagewatermark

($product _img, $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 (!empty ($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 1water_im = imagecreatefromgif ($waterImage);
Case 2water_im = Imagecreatefromjpeg ($waterImage);
Case 3water_im = imagecreatefrompng ($waterImage);
Default:die ($FORMATMSG);
}
}
Read background picture
if (!empty ($product _img) && file_exists ($product _img))
{
$ground _info = getimagesize ($product _img);
$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 ($product _img);
Case 2: $ground _im = imagecreatefromjpeg ($product _img);
Case 3: $ground _im = imagecreatefrompng ($product _img);
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 *2.5), 0, "Arial.ttf", $waterText);//Get text using TrueType fonts

The scope of this
$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 (!empty ($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 ($product _img);
Switch ($ground _info[2])//Get the format of the background picture
{
Case 1:imagegif ($ground _im, $product _img);
Case 2:imagejpeg ($ground _im, $product _img);
Case 3:imagepng ($ground _im, $product _img);
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);
}

//---------------------------------------------------------------------------------------
if (Isset ($_files) &&!empty ($_files[' userfile ']) && $_files[' userfile ' [' Size ']>0]
{
$uploadfile = "./". Time (). " _ ". $_files[' userfile ' [' name '];
if (copy ($_files[' userfile ' [' tmp_name '], $uploadfile))
{
echo "OK
";
Text watermark
Imagewatermark ($uploadfile, 0, "", http://www.bkjia.com,5, "#FF0000");
Image watermark
$waterImage = "images/bz.gif";//Watermark Picture Path
Imagewatermark ($uploadfile, 0, $waterImage);
echo "";
}
Else
{
echo "Fail
";
}
}


http://www.bkjia.com/PHPjc/444633.html www.bkjia.com true http://www.bkjia.com/PHPjc/444633.html techarticle add watermark to the picture is a common and very useful function of PHP program, let me give you a detailed introduction to the PHP PNG transparent background Watermark program code bar. The code below copy code/...

  • 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.