PHP to add a watermark instance function to the picture

Source: Internet
Author: User
Tags file size imagecopy imagejpeg

The realization of watermark function is mainly depend on these function function operation

1.imagecreatefromjpeg//Open JPG picture 2.imagecreatefromgif//Open GIF picture
3.imagecreatefrompng//Open PNG picture
4.imagecreatefromwbmp//Open wbmp picture (less use)
5.getimagesize//Get picture size information
6.imagecopymerge//Integrate multiple images (main function to add watermarks)
7.imagejpeg//save JPG Picture
8.imagegif//Save GIF picture
9.imagepng//Save PNG picture

The code is as follows Copy Code

<?php

Echo Img_water_mark ("1.jpg", "walter.gif", NULL, "2.jpg", 5,80);

/**
* Image watermark (applicable to png/jpg/gif format)
*
* @author FLYNETCN
*
* @param $srcImg Original picture
* @param $waterImg watermark Picture
* @param $savepath Save path
* @param $savename Save name
* @param $positon Watermark Location
* 1: Top left, 2: Top right, 3: center, 4: Bottom Bureau left, 5: bottom of the right
* @param $alpha Transparency-0: Fully transparent, 100: completely opaque
*
* @return Success--New image address after watermark
* Failed---1: Original file does not exist,-2: Watermark picture does not exist,-3: Original file image Object build failed
*-4: Watermark file Image Object build failed-5: New picture after Watermark failed to save
*/
function Img_water_mark ($SRCIMG, $WATERIMG, $savepath =null, $savename =null, $positon =5, $alpha =30)
{
$temp = PathInfo ($SRCIMG);
$name = $temp [basename];
$path = $temp [dirname];
$exte = $temp [extension];
$savename = $savename? $savename: $name;
$savepath = $savepath? $savepath: $path;
$savefile = $savepath. " /". $savename;
$srcinfo = @getimagesize ($SRCIMG);
if (! $srcinfo) {
return-1; The original file does not exist
}
$waterinfo = @getimagesize ($WATERIMG);
if (! $waterinfo) {
Return-2; Watermark picture does not exist
}
$SRCIMGOBJ = Image_create_from_ext ($SRCIMG);
if (! $srcImgObj) {
return-3; Original file image object failed to build
}
$WATERIMGOBJ = Image_create_from_ext ($WATERIMG);
if (! $waterImgObj) {
return-4; Watermark file Image Object build failed
}
Switch ($positon) {
1 Top Left
Case 1: $x = $y = 0; Break
2 Top of Right
Case 2: $x = $srcinfo [0]-$waterinfo [0]; $y = 0; Break
3 Center
Case 3: $x = ($srcinfo [0]-$waterinfo [0])/2; $y = ($srcinfo [1]-$waterinfo [1])/2; Break
4 bottom Left
Case 4: $x = 0; $y = $srcinfo [1]-$waterinfo [1]; Break
5 bottom Right
Case 5: $x = $srcinfo [0]-$waterinfo [0]; $y = $srcinfo [1]-$waterinfo [1]; Break
Default: $x = $y = 0;
}
Imagecopymerge ($SRCIMGOBJ, $WATERIMGOBJ, $x, $y, 0, 0, $waterinfo [0], $waterinfo [1], $alpha);
Switch ($srcinfo [2]) {
Case 1:imagegif ($SRCIMGOBJ, $savefile); Break
Case 2:imagejpeg ($SRCIMGOBJ, $savefile); Break
Case 3:imagepng ($SRCIMGOBJ, $savefile); Break
default:return-5; Save failed
}
Imagedestroy ($SRCIMGOBJ);
Imagedestroy ($WATERIMGOBJ);
return $savefile;
}

function Image_create_from_ext ($imgfile)
{
$info = getimagesize ($imgfile);
$im = null;
Switch ($info [2]) {
Case 1: $im =imagecreatefromgif ($imgfile); Break
Case 2: $im =imagecreatefromjpeg ($imgfile); Break
Case 3: $im =imagecreatefrompng ($imgfile); Break
}
return $im;
}

Currently supports JPG, GIF, PNG and other picture formats.

Use examples:

The code is as follows Copy Code

if ($pic = Watermark ('./image.jpg ', './watermark.png '))
{
Echo ' ';
}
Else
{
Echo ' }

The following shows a full watermark addition function

The code is as follows Copy Code

<?php
/**************************************************************

Parameter description:
$max _file_size: Upload file size limit, unit byte
$destination _folder: Upload file path
$watermark: Whether additional watermark (1 for Watermark, other for no watermark);

Instructions for use:
1. Remove the "Extension=php_gd2.dll" line in front of the php.ini file, because we need to use the GD library;
2. Change Extension_dir = to the directory where your Php_gd2.dll is located;
**************************************************************/

Upload file Type list
$uptypes =array (
' Image/jpg ',
' Image/jpeg ',
' Image/png ',
' Image/pjpeg ',
' Image/gif ',
' Image/bmp ',
' Image/x-png '
);

$max _file_size=2000000; Upload file size limit, unit byte
$destination _folder= "uploadimg/"; Upload file path
$watermark = 1; Whether to add watermark (1 for Watermark, other for no watermark);
$watertype = 1; Watermark Type (1 for text, 2 for picture)
$waterposition = 1; Watermark position (1 is lower left corner, 2 is lower right corner, 3 is upper left corner, 4 is upper right corner, 5 is centered);
$waterstring = "http://www.111cn.net/"; Watermark String
$waterimg = "Xplore.gif"; Watermark Picture
$imgpreview = 1; Whether to generate a preview diagram (1 is a build, others are not generated);
$imgpreviewsize =1/1; Thumbnail proportions
?>
<title> Picture Water Printing program Demo!www.mop8.com</title>
<style type= "Text/css" >
<!--
Body
{
font-size:9pt;
}
Input
{
Background-color: #66CCFF;
border:1px inset #CCCCCC;
}
-->
</style>

<body>
<center>
<form enctype= "Multipart/form-data" method= "post" name= "Upform" >
Upload file:
<input name= "upfile" type= "File" >
<input type= "Submit" value= "Upload" ><P>
The types of files allowed to upload are: <?=implode (', ', $uptypes)?>
</form>
<font color= "#FF0000" > This demo space is provided by Tuwoo, this procedure uses the way of text watermark .</font></center>
<?php
if ($_server[' request_method '] = = ' POST ')
{
if (!is_uploaded_file ($_files["Upfile"][tmp_name))
Is there a file
{
echo "Picture does not exist!";
Exit
}

$file = $_files["Upfile"];
if ($max _file_size < $file ["size"])
Check File size
{
echo "file is too big!";
Exit
}

if (!in_array ($file ["type"], $uptypes))
Check file type
{
echo "file type does not match!". $file [' type '];
Exit
}

if (!file_exists ($destination _folder))
{
mkdir ($destination _folder);
}

$filename = $file ["Tmp_name"];
$image _size = getimagesize ($filename);
$pinfo =pathinfo ($file ["name"]);
$ftype = $pinfo [' extension '];
$destination = $destination _folder.time (). "." $ftype;
if (file_exists ($destination) && $overwrite!= True)
{
echo "file with the same name already exists";
Exit
}

if (!move_uploaded_file ($filename, $destination))
{
echo "Move file Error";
Exit
}

$pinfo =pathinfo ($destination);
$fname = $pinfo [basename];
echo "<font color=red> has been successfully uploaded </font><br> FileName: <font color=blue>". $destination _folder. $fname. " </font><br> ";
echo "width:". $image _size[0];
echo "Length:". $image _size[1];
echo "<br> size:". $file ["Size"]. "bytes";

if ($watermark ==1)
{
$iinfo =getimagesize ($destination, $iinfo);
$nimage =imagecreatetruecolor ($image _size[0], $image _size[1]);
$white =imagecolorallocate ($nimage, 255,255,255);
$black =imagecolorallocate ($nimage, 0,0,0);
$red =imagecolorallocate ($nimage, 255,0,0);
Imagefill ($nimage, 0,0, $white);
Switch ($iinfo [2])
{
Case 1:
$simage =imagecreatefromgif ($destination);
Break
Case 2:
$simage =imagecreatefromjpeg ($destination);
Break
Case 3:
$simage =imagecreatefrompng ($destination);
Break
Case 6:
$simage =imagecreatefromwbmp ($destination);
Break
Default
Die ("Unsupported file type");
Exit
}

Imagecopy ($nimage, $simage, 0,0,0,0, $image _size[0], $image _size[1]);
Imagefilledrectangle ($nimage, 1, $image _size[1]-15,80, $image _size[1], $white);

Switch ($watertype)
{
Case 1://Add watermark String
Imagestring ($nimage, 2,3, $image _size[1]-15, $waterstring, $black);
Break
Case 2://Add watermark Picture
$simage 1 =imagecreatefromgif ("Xplore.gif");
Imagecopy ($nimage, $simage 1,0,0,0,0,85,15);
Imagedestroy ($simage 1);
Break
}

Switch ($iinfo [2])
{
Case 1:
Imagegif ($nimage, $destination);
Imagejpeg ($nimage, $destination);
Break
Case 2:
Imagejpeg ($nimage, $destination);
Break
Case 3:
Imagepng ($nimage, $destination);
Break
Case 6:
Imagewbmp ($nimage, $destination);
Imagejpeg ($nimage, $destination);
Break
}

Overwrite original upload file
Imagedestroy ($nimage);
Imagedestroy ($simage);
}

   if ($imgpreview ==1)   
   {  
   echo <br> Image preview: <br> ";  
   echo"    echo "alt=" Picture preview: R file name: ". $destination." R upload time: ">";  
  }  
}  
?>  
</body>& nbsp; 

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.