PHP instance ———— making watermark image

Source: Internet
Author: User
Tags imagejpeg transparent color watermark images

At present, QQ space and Sina Weibo and other social software, shared images are mostly added watermark. After adding a watermark, the photos become unique, as if they were adding a personal signature. Can be very good to prevent people from stealing your photos, but also can let others look at the watermark on the above to get some information about the photos. Today, I wrote a function to add a watermark to the picture, and share it with you. With this function, it is much easier to add a watermark to a photo. In fact, the principle is very simple, the original image is a picture, the watermark is actually a picture, but some watermark is white or transparent color background, and then two pictures are synthesized, it becomes a watermark picture.

1. PHP functions required to create watermark images

(1) getimagesize ();

(2) Imagecreatefromgif (); Imagecreatefromjpeg (); imagecreatefrompng;

(3) imagecogy ();

(4) Imagegif (); imagejpeg (); Imagepng ();

How to use the specific function, I will not tell you, you can refer to the PHP reference manual

2. Code sharing

<?phpfunction Watermark ($maxfile, $minfile) {//Get size Chart information $maxarr=getimagesize ($maxfile); $minarr =getimagesize ($ minfile); $max _width= $maxarr [0]; $max _height= $maxarr [1]; $max _format= $maxarr [2]; $max _mime= $maxarr [' mime ']; $min _                Width= $minarr [0]; $min _height= $minarr [1]; $min _format= $minarr [2];//$min _mime= $minarr [' MIME ']; Determine the size chart format switch ($max _format) {Case 1: $max _createimg= "Imagecreatefromgif"; $max _outimg= "Imagegif"; Break;case 2: $max _createimg= "Imagecreatefromjpeg"; $max _outimg= "imagejpeg"; Break;case 3: $max _createimg= "Imagecreatefrompng"; $max _ Outimg= "imagepng"; break;} Switch ($min _format) {Case 1: $min _createimg= "Imagecreatefromgif"; Break;case 2: $min _createimg= "Imagecreatefromjpeg" ; Break;case 3: $min _createimg= "Imagecreatefrompng"; Create a size chart resource $maxim= $max _createimg ($maxfile), $minim = $min _createimg ($minfile);//two-picture size difference $d_value_x= $max _width-$min _ width; $D _value_y= $max _height-$min _height;//add watermark imagecopy ($maxim, $minim, $D _value_x, $D _value_y,0,0, $min _width,$ Min_height)///Save watermark Picture Header ("content-type:{$max _mime} "); $max _outimg ($maxim," watermark.jpg ");} Watermark ("Picture/apple.jpg", "picture/copypic.jpg");? >

3. Say the points in the code that may not be well understood

(1) Because the function suffix required for a picture in a different format is not the same, you must determine the image format before using the function to create the resource and save the watermark image.

(2) Calculate the size difference of two pictures, is to add a small map is good to the lower right corner of the large image (most of the watermark is added to the bottom right corner of it).

4. Where the whole can be optimized

(1) Two switch always feel a little bit more. If you put it in an if statement or a for loop, you can definitely integrate it. So hungry, so later in the optimization of the problem (obviously to make excuses for themselves).

(2) The watermark I used is directly another picture with a smaller size. However, the actual watermark is mostly a line of words, a URL, a time and so on. Can be used as a method of making verification code, making watermark. Then the watermark needs the text as the function parameters, you can add their own watermark in a picture, and the watermark generation does not need to look for pictures, fully automatic generation.


PHP instance ———— making watermark image

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.