How to add image watermarks using PHP and Qiniu Cloud Storage interface

王林
Release: 2023-07-05 17:20:01
Original
1565 people have browsed it

How to add image watermarks using PHP and Qiniu Cloud Storage interface

Introduction:
With the development of the Internet, pictures have played a very important role in web design and application. In order to protect the copyright of an individual or business, it is sometimes necessary to add a watermark to the image. This article will introduce how to use PHP and Qiniu cloud storage interface to add image watermarks.

1. Preparation
Before you start, you need to ensure that you have the following environment and resources:

  1. PHP has been installed and configured correctly.
  2. Already have a Qiniu Cloud Storage account and understand the basic concepts and operations of Qiniu Cloud Storage.

2. Principle of adding watermark
To add a watermark, we need to first upload the image to be watermarked to Qiniu Cloud Storage and obtain the image URL with the watermark. Then, we can display watermarked images on the web page by adding image tags to HTML and specifying the URL of the image.

3. Detailed explanation of steps
Next, we will follow the following steps.

  1. Install the PHP SDK of Qiniu Cloud Storage
    It can be installed through composer. Enter the project folder and run the following command:

    composer require qiniu/php-sdk
    Copy after login
  2. Configure Qiniu cloud storage parameters
    Create a new config.php file in the project and add the following content:

    <?php
    // 七牛云存储的秘钥
    define('QINIU_ACCESS_KEY', 'your_access_key');
    define('QINIU_SECRET_KEY', 'your_secret_key');
    Copy after login

    Here you need to replace your_access_key and your_secret_key with your own Qiniu Cloud Storage key.

  3. Upload pictures to Qiniu Cloud Storage
    Create a new upload.php file in the project and add the following content:

    <?php
    require_once('config.php');
    require_once('vendor/autoload.php');
    
    use QiniuAuth;
    use QiniuStorageUploadManager;
    
    $bucket = 'your_bucket_name';
    $auth = new Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY);
    $token = $auth->uploadToken($bucket);
    
    if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
     $file_path = $_FILES['file']['tmp_name'];
     $key = uniqid();
     $upload_manager = new UploadManager();
     list($ret, $err) = $upload_manager->putFile($token, $key, $file_path);
     if ($err !== null) {
         echo json_encode(['status' => 'error', 'message' => '上传图片失败']);
     } else {
         $image_url = 'http://your_bucket_domain/' . $key;
         echo json_encode(['status' => 'success', 'image_url' => $image_url]);
     }
    } else {
     echo json_encode(['status' => 'error', 'message' => '上传图片失败']);
    }
    Copy after login

    Here you need to replace your_bucket_name with The storage space name of your own Qiniu Cloud Storage, replace your_bucket_domain with the domain name of your own Qiniu Cloud Storage.

  4. Add image watermark
    Create a new watermark.php file in the project and add the following content:

    <?php
    require_once('config.php');
    require_once('vendor/autoload.php');
    
    use QiniuAuth;
    
    function addWatermark($image_url) {
     // 水印图片的URL
     $watermark_url = 'http://your_bucket_domain/watermark.png';
    
     // 水印位置和透明度
     $position = 'NorthEast';
     $opacity = 80;
    
     // 生成带有水印的图片URL
     $auth = new Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY);
     $signed_url = $auth->privateDownloadUrl($image_url);
     $signed_watermark_url = "{$signed_url}?watermark/1/image/{$watermark_url}/dissolve/{$opacity}/gravity/{$position}";
    
     return $signed_watermark_url;
    }
    
    // 从upload.php返回的image_url获取待添加水印的图片URL
    $image_url = $_GET['image_url'];
    
    // 调用addWatermark函数,获取带有水印的图片URL
    $signed_watermark_url = addWatermark($image_url);
    
    ?>
    <!DOCTYPE html>
    <html>
    <head>
     <title>图片水印示例</title>
    </head>
    <body>
     <h1>添加水印后的图片:</h1>
     <img src="<?php echo $signed_watermark_url; ?>" alt="带有水印的图片">
    </body>
    </html>
    Copy after login

    Here you need to replace your_bucket_domain with your own seven The domain name of Niu Cloud Storage, replace watermark.png with your own watermark image.

4. Summary
This article introduces the method of adding image watermarks using PHP and Qiniu cloud storage interface. By uploading pictures to Qiniu Cloud Storage, and using the image processing function of Qiniu Cloud Storage, the pictures with watermarks are displayed in HTML. I hope this article is helpful to you, thanks for reading!

The above is the detailed content of How to add image watermarks using PHP and Qiniu Cloud Storage interface. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!