Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add image watermark in PHP

Shulou Source: shulou.com Published: 2022-06-03 03:05:11 11月12日 Update

This article mainly explains "how to add image watermark in PHP", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "how to add image watermark in PHP"!

In our daily life, in order not to want their own pictures stolen, often can see in some pictures add watermark, such as our website, input image files will automatically add a watermark in the lower right corner, then this watermark effect is how to achieve it? Through PHP you can achieve the effect of adding watermarks to images.

In the previous article, the operation of cropping an image onto the target image is somewhat similar to adding a watermark. In order to add a watermark, you need to use the imagecopymerge function.

imagecopymerge function

The syntax for the imagecopymerge function is as follows:

imagecopymerge($target image,$source image,$target start x ,$target start y,$source x ,$source y ,$source width,$source height,$transparency)

The transparency value is an integer from 0 to 100, and the larger the transparency value is, the more opaque the watermark is.

Next, let's take a look at how to add a watermark to an image through an example:

First, we prepared an image named test.png and a watermark image named logo.png and put them in our root directory.

Note that the above is an example image, the watermark below is not added by me, it is automatically added by the website, in order to distinguish, the watermark of the example is as follows

Then, the operation is as follows:

Output:

As can be seen from the above results, a watermark with transparency of 50 is added by imagecopymerge function.

Extension of knowledge imagecopy function

Watermarks can be added not only by imagecopy function, but also by imagecopy() function, which can copy part of the function. Its syntax format is as follows:

imagecopy($target image, $source image,$target image x,$target image y,$source image x,$source image y,$source image width,$source image height)

It means that a rectangular area of width w and height h is selected at (x, y) of the source image onto (x,y) of the target image.

Use the imagesx() and imagesy() functions in PHP to get the width and height of the image, respectively. Their syntax is as follows:

imagesx(resource $image)imagesy(resource $image)

Examples are as follows:

Output:

The effects are as follows:

At this point, I believe that we have a deeper understanding of "how to add image watermark in PHP," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Picture watermark source function target transparency instance effect format result website syntax next content address method learning output practical deeper Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux Shulou Tech Info OPPO Reno Docker