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