Use the imagecopy function to add an image watermark instance for PHP image processing

Source: Internet
Author: User
Tags imagecopy
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, if you need a watermark, you can refer to the following section to add a watermark to an image. this is also a common function 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 ");
?>

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.