Home > Backend Development > PHP Tutorial > Method to generate image thumbnails using PHP and Qiniu cloud storage interface

Method to generate image thumbnails using PHP and Qiniu cloud storage interface

WBOY
Release: 2023-07-07 11:08:02
Original
1682 people have browsed it

Method of generating image thumbnails using PHP and Qiniu Cloud Storage interface

In the development process of modern web applications, image processing is a common requirement. For image operations, thumbnail generation is a particularly common operation. This article will introduce how to use PHP language and Qiniu cloud storage interface to generate image thumbnails.

1. Preparation

  1. Register a Qiniu Cloud account and log in (https://www.qiniu.com).
  2. Create a Bucket and enter the Bucket's console.

2. Install the necessary dependent libraries

This experiment will be developed using the PHP SDK officially provided by Qiniu Cloud, so the PHP SDK needs to be installed, and you can use Composer to install it.

Open the terminal, enter the project root directory, and execute the following command to install:

composer require qiniu/php-sdk
Copy after login

3. Write the code

The following is a simple example code for uploading a photo The pictures are stored in Qiniu Cloud and a thumbnail with specified width and height is generated.

<?php

require 'autoload.php';

use QiniuAuth;
use QiniuStorageUploadManager;

$accessKey = "YOUR_ACCESS_KEY";
$secretKey = "YOUR_SECRET_KEY";
$bucket = "YOUR_BUCKET_NAME";

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

// 构建 UploadManager 对象
$uploadMgr = new UploadManager();

// 要上传的图片本地路径
$filePath = './path/to/image.jpg';

// 生成缩略图的宽度和高度
$width = 200;
$height = 200;

// 生成缩略图的规格
$thumbnail = "!".$width."x".$height."r";

// 上传图片并生成缩略图
$key = 'your_key_name';
$token = $auth->uploadToken($bucket);
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath, null, 'image/jpeg', false, null, $thumbnail);

if ($err !== null) {
    echo '上传失败:' . $err->message();
} else {
    echo '上传成功,缩略图地址为:'.$ret['key'];
}
Copy after login

Notes on the code:

  1. Replace YOUR_ACCESS_KEY, YOUR_SECRET_KEY and YOUR_BUCKET_NAME respectively with your seven Access Key, Secret Key and Bucket name of Niu Cloud Storage.
  2. Replace $filePath with the local path of the image you want to upload.
  3. $width and $height can be modified according to needs.
  4. $key is the storage path and file name of the uploaded image, which can also be modified according to needs.

4. Run the code

Save and close the code editor, use the command line to switch to the project root directory, and execute the following command:

php your_php_file.php
Copy after login

If everything is normal, You will see a successful upload message and the generated thumbnail address.

5. Summary

Through the cooperation of PHP and Qiniu Cloud Storage interface, we can easily generate image thumbnails. Not only that, Qiniu Cloud also provides a rich API interface to facilitate developers to upload, manage and process images. I hope this article was helpful and I wish you success in your development process!

The above is the detailed content of Method to generate image thumbnails 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