Home > Article > Backend Development > How to use PHP and Qiniu cloud storage interface to implement special filters and style migration of pictures
How to use PHP and Qiniu cloud storage interface to implement special filters and style migration of pictures
Abstract: This article introduces how to use PHP and Qiniu cloud storage interface to implement special filters and styles of pictures Migration method. By using the image processing interface of Qiniu Cloud Storage, we can process various filter effects on images and transfer the style of one image to another image. In order to implement these functions, we will use Qiniu Cloud Storage’s PHP SDK.
Keywords: PHP, Qiniu Cloud Storage, image processing, filters, style migration, SDK
First, we need to create an account on Qiniu Cloud Storage and obtain the API key. Then, we need to install the PHP SDK of Qiniu Cloud Storage, which can be installed through Composer. Open a command line terminal, enter the project directory, and execute the following command:
composer require qiniu/php-sdk
After the installation is complete, we can start writing code.
<?php require 'vendor/autoload.php'; // 引入七牛云存储的PHP SDK use QiniuAuth; // 引入Auth类 use QiniuStorageBucketManager; // 引入BucketManager类 $accessKey = 'YOUR_ACCESS_KEY'; $secretKey = 'YOUR_SECRET_KEY'; $bucket = 'YOUR_BUCKET'; // 实例化Auth类 $auth = new Auth($accessKey, $secretKey); // 实例化BucketManager类 $bucketManager = new BucketManager($auth); // 图片链接 $url = 'http://example.com/image.jpg'; // 滤镜效果参数 $filter = 'filtername/arg1/arg2'; // 生成新的图片链接 $newUrl = $bucketManager->buildUrl($url, $filter); // 输出新的图片链接 echo $newUrl; ?>
In the code, we first introduced the PHP SDK of Qiniu Cloud Storage and used the Require statement to introduce it as a dependent library. Then, we introduced the Auth class and BucketManager class using the Use statement. Next, we need to replace the access key and bucket name with our own information.
The core part of the code is the buildUrl() method, which accepts two parameters: image link and filter effect parameters. We can set different values for the $filter variable to achieve different filter effects. After calling the buildUrl() method, we can get the processed image link.
You can change the code according to your own needs and add other filter effect parameters. After executing the code, a new image link will be output, that is, the processed image.
<?php require 'vendor/autoload.php'; // 引入七牛云存储的PHP SDK use QiniuAuth; // 引入Auth类 use QiniuStorageBucketManager; // 引入BucketManager类 $accessKey = 'YOUR_ACCESS_KEY'; $secretKey = 'YOUR_SECRET_KEY'; $bucket = 'YOUR_BUCKET'; // 实例化Auth类 $auth = new Auth($accessKey, $secretKey); // 实例化BucketManager类 $bucketManager = new BucketManager($auth); // 原始图片链接 $fromUrl = 'http://example.com/image1.jpg'; // 目标图片链接 $toUrl = 'http://example.com/image2.jpg'; // 风格迁移参数 $style = 'yourStyle'; // 生成新的图片链接 $newUrl = $bucketManager->buildUrl($fromUrl, 'style/' . $style . '|saveas/' . Qiniuase64_urlSafeEncode($bucket . ':' . $toUrl)); // 输出新的图片链接 echo $newUrl; ?>
The code is similar to the previous filter effect processing example, except that the parameters for saving the target image are added to the buildUrl() method. We pass the link of the original image and the link of the target image to the buildUrl() method, and set the corresponding style migration parameters.
According to your own needs, you can change the variable values in the code and add other style migration parameters. After executing the code, the processed image link will be output, that is, the image after style migration.
Reference materials:
The above code examples It is implemented through the PHP SDK of Qiniu Cloud Storage. Qiniu Cloud Storage has powerful image processing functions. By calling the corresponding methods and setting parameters, we can achieve various filter effects and style migration. Readers can modify and expand according to their own needs to achieve more creative and unique image processing effects.
The above is the detailed content of How to use PHP and Qiniu cloud storage interface to implement special filters and style migration of pictures. For more information, please follow other related articles on the PHP Chinese website!