Home >Backend Development >PHP Tutorial >Add watermark to image with PHP imagecopy() and imagecopymerge()_PHP Tutorial
There are many ways to add watermarks to images in PHP. These functions are all based on the GD library in PHP. If you do not have an account with the GD library, you cannot use the watermark function.
The imagecopymerge() function is used to copy and merge part of an image. It returns TRUE if successful, otherwise it returns FALSE.
Enable PHP GD library support under Windows
Find php.ini, open the content and find:
;extension=php_gd2.dll
Remove the first semicolon ";" and save it. If there is no semicolon in the first place, it means it is already enabled
Basic syntax
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 )
Parameter Description: Parameter Description
dst_im target image
src_im copied source image
dst_x target image start x coordinate
dst_y is the starting y coordinate of the target image. If x and y are both 0, it starts from the upper left corner
src_x copy image start x coordinate
src_y starts copying the y coordinate of the image. If x and y are both 0, then copy starts from the upper left corner
src_w (starting from src_x) Width of copy
src_h (starting from src_y) height of copy
pct Image merging degree, value 0-100. When pct=0, nothing is actually done, otherwise it is completely merged.
When pct = 100, this function is exactly the same as imagecopy() for palette images
After knowing how to use it, it is easy to implement our function. You can easily implement it with the following code
The code is as follows | Copy code |
header("Content-type: image/jpeg"); //original image //Get original image information //Watermark image //Watermark transparency //Merge watermark images //Output the merged watermark image After the new version, the imagecopymerge function is almost no longer used. We can directly use imagecopy to generate watermarks. The functions of the two functions are exactly the same. //增加水印 |
A better function that can generate thumbnails and add watermarks to pictures
/***
Want to manipulate pictures
First, you must get the size and type information of the image
Watermark: Copy the specified watermark to the target and add a transparent effect
Thumbnail: Copy a large image to a small screen
***/
The code is as follows | Copy code |
class ImageTool { $dinfo['height']); |