Use ThinkPHP to watermark and set the watermark position.

Source: Internet
Author: User

Use ThinkPHP to watermark and set the watermark position.

This document describes how to watermark ThinkPHP and set the watermark position. We will share this with you for your reference. The details are as follows:

I recently used Thinkphp's watermarking function and found that it can only be played in the lower left corner. PHP watermarking is still very easy to use.
Copy codeThe Code is as follows: 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)

Copy the coordinates of the src_im image from src_x, src_y, src_w in width, and src_h in height to the position where the coordinates of dst_im are dst_x and dst_y. The degree of merging is determined based on pct. The value range is from 0 to 100. When pct = 0, nothing is actually done. When it is 100, this function is identical to imagecopy () for the color palette image, and it achieves alpha transparency for the real color image.

Watermark demo diagram:

I need to place the watermark in the middle of the image and view the Thinkphp code. I found that the author is writing dead, and I can only make one modification.

/*** Add a watermark to the image * @ static public * @ param string $ source original file name * @ param string $ water watermark image * @ param string $ savename watermark image name after watermark * @ param string $ position of the postion watermark leftbottom rightbottom lefttop righttop center <New> * @ param string $ transparency of the alpha watermark * @ return void */static public function water ($ source, $ water, $ savename = null, $ postion = "center", $ alpha = 80) {// check whether the object exists if (! File_exists ($ source) |! File_exists ($ water) return false; // Image Information $ sInfo = self: getImageInfo ($ source); $ wInfo = self: getImageInfo ($ water ); // if the image is smaller than the watermark image, 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 image color mixing. Mode imagealphablending ($ wImage, true); // image position. The default value is the right-aligned position in the lower right corner. $ posArr = $ this-> WaterPostion ($ postion, $ sInfo, $ wInfo ); // Add // generate a hybrid 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 provided, the default value is the original image name if (! $ Savename) {$ savename = $ source; @ unlink ($ source) ;}// Save the 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 ;}}

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.