This article describes how to use the imagecopy function to add an image watermark for PHP image processing. This article provides a simple entry-level User-Defined Function example. For more information, see
This article describes how to use the imagecopy function to add an image watermark for PHP image processing. This article provides a simple entry-level User-Defined Function example. For more information, see
Adding watermarks to images is also a common feature in image processing. As long as the images you see on the page can be easily obtained, the images you have worked so hard to edit don't want to be taken away by others, so you can add watermarks to the images to determine the copyright, prevents image theft. You can use text (company name and website) or image (company LOGO) to make a watermark. The watermark effect is better, because some image software can be used for beautification. To use text as a watermark, you only need to draw some text on the image. If you want to make an image watermark, You need to first understand the imagecopy () function in the GD library and copy part of the image. The function is prototype as follows:
The Code is as follows:
Bool imagecopy (resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h)
This function is used to copy the coordinates of the src_im image from the src_x, src_y, src_w in width, and src_h to the dst_im image's dst_x and dst_y positions. Take an image in JPEG format as an example to compile a function watermark () for adding a watermark to the image. The Code is as follows:
The Code is as follows:
<? Php
// Add an image watermark (random position) to the background image. The background image format is jpeg and the watermark image format is gif.
Function watermark ($ filename, $ water ){
// Obtain the width and height of the background image.
List ($ B _w, $ B _h) = getimagesize ($ filename );
// Obtain the width and height of the watermark image
List ($ w_w, $ w_h) = getimagesize ($ water );
// Place the watermark position in the background image at random start position
$ PosX = rand (0, ($ B _w-$ w_w ));
$ PosY = rand (0, ($ B _h-$ w_h ));
// Create background image resources
$ Back = imagecreatefromjpeg ($ filename );
// Create watermark image resources
$ Water = imagecreatefromgif ($ water );
// Use the imagecopy () function to copy the watermark image to the position specified in the background image.
Imagecopy ($ back, $ water, $ posX, $ posY, 0, 0, $ w_w, $ w_h );
// Save the background image with the watermark
Imagejpeg ($ back, $ filename );
Imagedestroy ($ back );
Imagedestroy ($ water );
}
Watermark ("brophp.jpg", "logo.gif ");
?>