Home >Backend Development >PHP Tutorial >How to add watermark when saving remote pictures using PHP?
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:
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);
imagecreatefromstring
function to copy the image content Decoded into image resources. The following is a sample code for creating image resources: $image = imagecreatefromstring($imageContent);
Add watermark image:
$watermarkImage = imagecreatefrompng('watermark.png'); $watermarkWidth = imagesx($watermarkImage); $watermarkHeight = imagesy($watermarkImage); // 将水印图片叠加到原图片上 imagecopy($image, $watermarkImage, 0, 0, 0, 0, $watermarkWidth, $watermarkHeight);
Add watermark text:
$watermarkText = 'Watermark Text'; $fontSize = 24; $fontColor = imagecolorallocate($image, 255, 255, 255); // 设置文字颜色为白色 // 将水印文字写入图片 imagettftext($image, $fontSize, 0, 10, 10, $fontColor, 'font.ttf', $watermarkText);
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);
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);
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!