Home  >  Article  >  Backend Development  >  How to add watermark and save remote images after saving them locally in PHP?

How to add watermark and save remote images after saving them locally in PHP?

PHPz
PHPzOriginal
2023-07-11 23:48:051602browse

How to add a watermark and save the remote image after saving it locally in PHP?

In PHP development, we often encounter the need to save remote images locally. Sometimes, we may also need to add a watermark to the saved image to protect copyright or add additional information. This article will introduce how to use PHP to save remote pictures to local and add watermarks to the saved pictures.

1. Save remote pictures to local

First, we need to use PHP's file operation function to save remote pictures to local. The following is a simple sample code:

<?php
function saveImage($url, $path)
{
    $ch = curl_init($url); // 初始化curl
    $fp = fopen($path, 'wb'); // 打开文件资源
    curl_setopt($ch, CURLOPT_FILE, $fp); // 设置curl参数,将文件写入fp
    curl_setopt($ch, CURLOPT_HEADER, 0); // 不包含header
    curl_exec($ch); // 执行curl请求
    curl_close($ch); // 关闭curl
    fclose($fp); // 关闭文件资源
}

$url = 'https://example.com/image.jpg'; // 远程图片链接
$path = 'path/to/save/local/image.jpg'; // 保存到本地的路径和文件名
saveImage($url, $path); // 调用函数保存远程图片到本地
?>

In the above code, we define a saveImage function that accepts the URL and save path of the remote image as parameters. The curl library is used internally to write the remote image content to the local file.

2. Add a watermark to the saved image

Next, we need to add a watermark to the saved image. The following is a sample code using the GD library:

<?php
function addWatermark($sourcePath, $watermarkPath, $outputPath)
{
    $source = imagecreatefromjpeg($sourcePath); // 打开原始图片
    $watermark = imagecreatefrompng($watermarkPath); // 打开水印图片

    $sourceWidth = imagesx($source); // 原始图片宽度
    $sourceHeight = imagesy($source); // 原始图片高度
    $watermarkWidth = imagesx($watermark); // 水印图片宽度
    $watermarkHeight = imagesy($watermark); // 水印图片高度

    $x = $sourceWidth - $watermarkWidth - 10; // 水印左上角的x坐标
    $y = $sourceHeight - $watermarkHeight - 10; // 水印左上角的y坐标

    imagecopy($source, $watermark, $x, $y, 0, 0, $watermarkWidth, $watermarkHeight); // 将水印图片拷贝到原始图片上

    imagejpeg($source, $outputPath); // 保存带水印的图片

    imagedestroy($source); // 销毁原始图片资源
    imagedestroy($watermark); // 销毁水印图片资源
}

$sourcePath = 'path/to/save/local/image.jpg'; // 保存的图片路径
$watermarkPath = 'path/to/watermark.png'; // 水印图片路径
$outputPath = 'path/to/output/image.jpg'; // 输出带水印图片的路径和文件名
addWatermark($sourcePath, $watermarkPath, $outputPath); // 调用函数添加水印并保存图片
?>

In the above code, we define a addWatermark function that accepts the original image path, the watermark image path, and the output path and file name as parameter. The GD library is used inside the function to operate pictures. First, the original picture and the watermarked picture are opened as resources, then the watermarked picture is copied to the original picture according to the location information, and finally the watermarked picture is saved.

Summary:

Through the above steps, we can use PHP to save remote images to the local and add watermarks to the saved images. This meets our need to protect copyright and add additional information. Hope this article is helpful to you!

The above is the detailed content of How to add watermark and save remote images after saving them locally in PHP?. 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