Adding a watermark to a picture is also a common feature in image processing. Because as long as the pictures you see on the page can easily get, you work hard to edit the pictures do not want to be taken away by others to use, so add a watermark for the picture to determine the copyright, to prevent the image was stolen. Make watermark can use text (company name plus URL), also can use the picture (company logo), the image watermark effect is better, because some do picture software to beautify. Use text to do the watermark, just draw some text on the picture. If you make a picture watermark, you need to first understand the imagecopy () function in the GD library, you can copy part of the picture. The prototype of the function looks like this:
Copy Code code 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)
The function is to start the coordinates of the src_im image from Src_x,src_y, with a width of src_w, and a height of src_h as part of the dst_im image where coordinates are dst_x and dst_y. As an example of a picture in JPEG format, write a function watermark () that adds a watermark to the picture, as shown in the following code:
Copy Code code as follows:
<?php
Add a picture watermark to the background image (random location), background image format jpeg, watermark picture format gif
function watermark ($filename, $water) {
Get the width and height of the background picture
List ($b _w, $b _h) = getimagesize ($filename);
Get the width and height of a watermark picture
List ($w _w, $w _h) = getimagesize ($water);
A random starting position of a picture in a background picture
$posX = rand (0, $b _w-$w _w));
$posY = rand (0, $b _h-$w _h));
Create a resource for a background picture
$back = Imagecreatefromjpeg ($filename);
Create a resource for a watermark picture
$water = Imagecreatefromgif ($water);
Use the Imagecopy () function to copy the watermark picture to the location specified in the background picture
Imagecopy ($back, $water, $posX, $posY, 0, 0, $w _w, $w _h);
Save a background picture with a watermark picture
Imagejpeg ($back, $filename);
Imagedestroy ($back);
Imagedestroy ($water);
}
Watermark ("Brophp.jpg", "logo.gif");
?>