Method of using PHP and Qiniu cloud storage interface to achieve image quality compression and format conversion

王林
Release: 2023-07-09 10:18:02
Original
1165 people have browsed it

Method of using PHP and Qiniu cloud storage interface to achieve quality compression and format conversion of images

Introduction:
In web development, it is often necessary to perform quality compression and format conversion of images in order to improve Page loading speed and bandwidth savings. This article will introduce how to use PHP and Qiniu cloud storage interface to implement this function. At the same time, we will give detailed code examples to make it easier for readers to understand and apply.

Prerequisite preparation:
Before starting, we need to prepare the following:

  1. A Qiniu cloud storage account, and the API configuration has been completed.
  2. PHP development environment, such as Apache, Nginx, etc.
  3. Install the PHP SDK of Qiniu Cloud Storage.

Step 1: Install the PHP SDK of Qiniu Cloud Storage

  1. Open the terminal and switch to the directory where the project is located.
  2. Run the following command to install the PHP SDK of Qiniu Cloud Storage:

    composer require "qiniu/php-sdk"
    Copy after login
  3. After the installation is completed, you can start writing code.

Step 2: Code implementation of quality compression and format conversion
The following is a simple PHP function for quality compression and format conversion of images:

<?php
require 'vendor/autoload.php'; // 引入七牛云存储的PHP SDK

// 设置七牛云存储的Access Key和Secret Key
$accessKey = 'YOUR_ACCESS_KEY';
$secretKey = 'YOUR_SECRET_KEY';

// 设置要上传的空间
$bucket = 'YOUR_BUCKET';

// 设置图片的样式,包括质量压缩和格式转换
$style = 'imageView2/2/q/75|sys_png';

// 压缩并转换图片的函数
function compressAndConvertImage($path, $key)
{
    global $accessKey, $secretKey, $bucket, $style;

    // 构建Auth对象
    $auth = new QiniuAuth($accessKey, $secretKey);

    // 构建图片对象
    $image = new QiniuStorageUploadManager();

    // 上传图片并压缩、转换格式
    list($ret, $err) = $image->putFile(
        $auth->uploadToken($bucket), 
        $key, 
        $path . $key + '?'.$style
    );

    if ($err !== null) {
        // 上传失败,打印错误信息
        echo $err;
        return false;
    } else {
        // 上传成功,返回七牛云存储中的图片URL
        return $ret['key'];
    }
}

// 测试函数
$imagePath = '/path/to/image/'; // 图片所在的路径
$imageKey = 'example.jpg'; // 图片的文件名

$imageURL = compressAndConvertImage($imagePath, $imageKey);
echo '压缩并转换后的图片URL:' . $imageURL;
?>
Copy after login

Above In the code, we first introduced the PHP SDK of Qiniu Cloud Storage, and set the Access Key, Secret Key and space to be uploaded of Qiniu Cloud Storage. Then, a function named compressAndConvertImage is defined, which is used to compress and convert images. This function accepts two parameters: the path where the image is located and the file name of the image. In the function, the uploading, compression and format conversion of images are implemented through the Auth object and UploadManager object. Finally, test by calling the compressAndConvertImage function to print out the compressed and converted image URL.

Conclusion:
Through the above steps, we can easily use PHP and Qiniu cloud storage interface to achieve image quality compression and format conversion. In this way, we can not only improve the loading speed and user experience of the website, but also save bandwidth and storage space. Hope this article helps you!

The above is the detailed content of Method of using PHP and Qiniu cloud storage interface to achieve image quality compression and format conversion. 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