PHP image processing using the Imagecopy function to add a picture watermark instance _php tips

Source: Internet
Author: User
Tags imagecopy rand

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");
?>

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.