Thinkphp water Seal, example of setting watermark position

Source: Internet
Author: User
Tags save file

Recently in the use of thinkphp water printing function, found only in the lower left corner. PHP Water printing work is still very easy, most of the use of

BOOL Imagecopymerge (Resource $DST _im, resource $src _im, int $dst _x, int $dst _y, int $src _x, int $src _y, int $src _w , int $src _h, int $pct)

The coordinates of the src_im image are started from the src_x,src_y, the width is src_w, and the height is part of the src_h copy to the Dst_im image in the location of Dst_x and dst_y. The two images will be based on the PCT to determine the degree of consolidation, the value range from 0 to 100. When pct = 0 o'clock, in fact nothing is done, when 100 for the palette image this function and imagecopy () exactly the same, it is true color image to achieve alpha transparency.

I need to get the watermark to the real center of the picture and see the thinkphp code. Found that the author is incredibly written dead, and I can only make a modification


/**
* Add a watermark to a picture
* @static Public
* @param string $source The original file name
* @param string $water watermark Picture
* @param string $ $savename The name of the picture after the watermark is added
* @param the exact location of the string $postion watermark leftbottom rightbottom lefttop righttop Center < add >
* @param string $alpha The transparency of the watermark
* @return void
*/
static public Function water ($source, $water, $savename =null, $postion = "center", $alpha =80) {
Check to see if a file exists
if (!file_exists ($source) | | |!file_exists ($water))
return false;

Picture information
$sInfo = Self::getimageinfo ($source);
$wInfo = Self::getimageinfo ($water);

If the picture is smaller than the watermark picture, no picture is generated
if ($sInfo ["width"] < $wInfo ["width"] | | $sInfo [' height '] < $wInfo [' height ']] return false; Create an image $sCreateFun = "Imagecreatefrom". $sInfo [' type ']; $sImage = $sCreateFun ($source); $wCreateFun = "Imagecreatefrom". $wInfo [' type ']; $wImage = $wCreateFun ($water); Sets the blending mode imagealphablending ($wImage, true) of the image; Image position, default to right $POSARR = lower right corner = $this->waterpostion ($postion, $sInfo, $wInfo); New

To generate a mixed image
Imagecopymerge ($sImage, $wImage, $POSARR [0], $POSARR [1], 0, 0, $wInfo [' width '], $wInfo [' height '], $alpha);

Output image
$ImageFun = ' Image '. $sInfo [' type '];
If no save file name is given, the default is the original image name
if (! $savename) {
$savename = $source;
@unlink ($source);
}
Save Image
$ImageFun ($sImage, $savename);
Imagedestroy ($sImage);
}

Private Function Waterpostion ($postion, $sInfo, $wInfo)
{
$posY = $sInfo ["height"]-$wInfo ["height"];
$posX = $sInfo ["width"]-$wInfo ["width"];

Switch ($postion)
{
Case "Rightbottom":
Return Array ($posX, $posY);
Break

Case "Leftbottom":
return Array ($wInfo ["width"], $posY);
Break

Case "Lefttop":
return Array ($wInfo ["width"], $wInfo ["height"]);
Break

Case "Righttop":
Return Array ($posX, $wInfo ["height"]);
Break

Case "Center":
Return Array ($posX/2, $posY/2);
Break
}
}
It seems to be on the ~

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.