Home > Backend Development > PHP Tutorial > How to add watermark when saving remote pictures using PHP?

How to add watermark when saving remote pictures using PHP?

王林
Release: 2023-07-11 22:26:01
Original
986 people have browsed it

How to add a watermark when saving remote images using PHP?

Watermark is to add some logos or text to the picture to protect the copyright of the picture or play a promotional role. When using PHP to save remote pictures, you can protect or personalize the pictures by adding watermarks. This article will introduce how to use PHP to add watermarks to remote images, with corresponding code examples.

The principle of adding watermark using PHP is to superimpose a watermark image or add watermark text on the image, and save the result as a new image file. The following is an implementation step:

  1. Get remote images
    Use PHP's file_get_contents function to obtain the contents of remote images. First, you need to determine the URL address of the remote image, and then use the file_get_contents function to read the image content. The following is a sample code to obtain a remote image:
$remoteImageURL = 'https://example.com/remote-image.jpg';
$imageContent = file_get_contents($remoteImageURL);
Copy after login
  1. Create image resource
    Create a new image resource using the image content, and use the imagecreatefromstring function to copy the image content Decoded into image resources. The following is a sample code for creating image resources:
$image = imagecreatefromstring($imageContent);
Copy after login
  1. Add watermark image or text
    Choose to add watermark image or text according to your needs. The following is a code example that demonstrates how to add watermark images and text.

Add watermark image:

$watermarkImage = imagecreatefrompng('watermark.png');
$watermarkWidth = imagesx($watermarkImage);
$watermarkHeight = imagesy($watermarkImage);

// 将水印图片叠加到原图片上
imagecopy($image, $watermarkImage, 0, 0, 0, 0, $watermarkWidth, $watermarkHeight);
Copy after login

Add watermark text:

$watermarkText = 'Watermark Text';
$fontSize = 24;
$fontColor = imagecolorallocate($image, 255, 255, 255); // 设置文字颜色为白色

// 将水印文字写入图片
imagettftext($image, $fontSize, 0, 10, 10, $fontColor, 'font.ttf', $watermarkText);
Copy after login
  1. Save image
    Finally, use imagepng, The imagejpeg or imagegif function saves the image with watermark as a new file. The following is a sample code for saving pictures:
$outputFilePath = 'output.jpg';
imagejpeg($image, $outputFilePath);
Copy after login

The complete sample code is as follows:

$remoteImageURL = 'https://example.com/remote-image.jpg';
$imageContent = file_get_contents($remoteImageURL);
$image = imagecreatefromstring($imageContent);

// 添加水印图片
$watermarkImage = imagecreatefrompng('watermark.png');
$watermarkWidth = imagesx($watermarkImage);
$watermarkHeight = imagesy($watermarkImage);
imagecopy($image, $watermarkImage, 0, 0, 0, 0, $watermarkWidth, $watermarkHeight);

// 添加水印文字
$watermarkText = 'Watermark Text';
$fontSize = 24;
$fontColor = imagecolorallocate($image, 255, 255, 255);
imagettftext($image, $fontSize, 0, 10, 10, $fontColor, 'font.ttf', $watermarkText);

$outputFilePath = 'output.jpg';
imagejpeg($image, $outputFilePath);
Copy after login

Using the above code, you can add a watermark while saving remote pictures. Note that watermark.png should be replaced with your own watermark image, and font.ttf should be replaced with your own font file.

Summary:
This article introduces how to use PHP to add watermarks when saving remote images. First get the remote image, then create the image resource, then you can choose to add a watermark image or text, and finally save the image with the watermark. Code examples are used to demonstrate the specific implementation of adding watermark images and text. By understanding the above methods, you can realize the functions of saving remote pictures and adding watermarks in PHP.

The above is the detailed content of How to add watermark when saving remote pictures using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template