Home  >  Article  >  Backend Development  >  PHP image watermark adding tutorial

PHP image watermark adding tutorial

王林
王林Original
2023-09-13 08:46:411097browse

PHP image watermark adding tutorial

Tutorial on adding watermark to PHP images, specific code examples are required

In many website developments, we often need to add watermarks to images to display copyright information, prevent theft, etc. . You can easily watermark images using PHP language. This article will introduce how to use PHP to add watermarks to images and give specific code examples.

First of all, we need an image to be used as a watermark, which can be a logo with copyright information or a piece of text. Suppose we have a watermark image named watermark.png.

Then, we need to have a picture to be added with a watermark, assuming that our picture is test.jpg.

Next, we can write PHP code to implement the function of adding image watermarks. First, use the imagecreatefromjpeg() function to create the resource of the image to be watermarked. The code is as follows:

$sourceImg = imagecreatefromjpeg('test.jpg');

Then, use the imagecreatefrompng() function to create the resource of the watermark image. The code is as follows:

$watermarkImg = imagecreatefrompng('watermark.png');

We We also need to obtain the image to be watermarked and the width and height of the watermark image. The code is as follows:

$sourceWidth = imagesx($sourceImg);
$sourceHeight = imagesy($sourceImg);

$watermarkWidth = imagesx($watermarkImg);
$watermarkHeight = imagesy($watermarkImg);

Next, we need to superimpose the watermark image on the image to be added. We can use the imagecopy() function to implement this function. The code is as follows:

$posX = $sourceWidth - $watermarkWidth;  //水印位置X轴坐标
$posY = $sourceHeight - $watermarkHeight; //水印位置Y轴坐标
imagecopy($sourceImg, $watermarkImg, $posX, $posY, 0, 0, $watermarkWidth, $watermarkHeight);

Finally, we need to save the watermarked image to a file. We can use the imagejpeg() function to save the image in JPEG format. The code is as follows:

imagejpeg($sourceImg, 'result.jpg');

So far, we have completed the function of adding image watermarks. The complete code is as follows:

$sourceImg = imagecreatefromjpeg('test.jpg');
$watermarkImg = imagecreatefrompng('watermark.png');

$sourceWidth = imagesx($sourceImg);
$sourceHeight = imagesy($sourceImg);

$watermarkWidth = imagesx($watermarkImg);
$watermarkHeight = imagesy($watermarkImg);

$posX = $sourceWidth - $watermarkWidth;  //水印位置X轴坐标
$posY = $sourceHeight - $watermarkHeight; //水印位置Y轴坐标
imagecopy($sourceImg, $watermarkImg, $posX, $posY, 0, 0, $watermarkWidth, $watermarkHeight);

imagejpeg($sourceImg, 'result.jpg');

Using the above code, we can add a watermark named watermark.png to the picture named test.jpg and save it as a file named result.jpg.

It should be noted that the above code is just a simple example. In actual application, the image size, watermark position, etc. may need to be adjusted. In addition, you can achieve more effects by adjusting the transparency and using other shapes of watermarks.

To summarize, this article introduces how to use PHP to add watermarks to images, and gives specific code examples. Through simple code, we can easily implement the function of adding image watermarks for use in website development. Hope this article is helpful to you.

The above is the detailed content of PHP image watermark adding tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more