Home > Article > Backend Development > How to add image watermarks using PHP and Qiniu Cloud Storage interface
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:
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.
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
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');
Here you need to replace your_access_key and your_secret_key with your own Qiniu Cloud Storage key.
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' => '上传图片失败']); }
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.
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>
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!