Home > Backend Development > PHP Tutorial > Method of cropping and zooming images using PHP and Qiniu cloud storage interface

Method of cropping and zooming images using PHP and Qiniu cloud storage interface

WBOY
Release: 2023-07-05 19:44:01
Original
1123 people have browsed it

How to use PHP and Qiniu cloud storage interface to realize image cropping and scaling

Introduction:
With the rapid development of the Internet, image processing has become a part that cannot be ignored in our daily development. In the process of image processing, we often encounter the need to perform operations such as cropping and scaling of images. This article will introduce how to use PHP combined with Qiniu cloud storage interface to realize image cropping and zooming, and give code examples.

1. Background introduction

  1. Qiniu Cloud Storage
    Qiniu Cloud Storage is a highly available, high-performance, low-cost, and easy-to-expand cloud storage service. It provides a rich API interface to facilitate developers to manage and process resources.
  2. PHP
    PHP is a widely used scripting language, especially suitable for web development. It is easy to learn, powerful, and has a rich extension library, making it an ideal choice for image processing.

2. Implementation method

  1. Preparation work
    First, we need to create a storage space on Qiniu Cloud and obtain the corresponding Access Key and Secret Key. In the PHP code, we use the SDK provided by Qiniu Cloud to operate, so we also need to install the SDK package of Qiniu Cloud.
  2. Picture Cropping
    Picture cropping refers to cropping a certain part of the picture while maintaining the original picture aspect ratio. The following is an example of PHP code that uses the Qiniu Cloud storage interface to implement image cropping:
<?php
require 'vendor/autoload.php'; // 引入七牛云SDK

use QiniuAuth;
use QiniuStorageBucketManager;

$accessKey = 'YOUR_ACCESS_KEY';
$secretKey = 'YOUR_SECRET_KEY';
$bucket = 'YOUR_BUCKET';
$auth = new Auth($accessKey, $secretKey);
$bucketManager = new BucketManager($auth);

$key = 'image.jpg'; // 待裁剪的图片key
$newKey = 'new_image.jpg'; // 裁剪后保存的图片key
$width = 200; // 裁剪后的宽度
$height = 150; // 裁剪后的高度
$x = 100; // 裁剪起始点的横坐标
$y = 100; // 裁剪起始点的纵坐标

$ops = 'imageView2/1/w/' . $width . '/h/' . $height . '/x/' . $x . '/y/' . $y;

list($ret, $err) = $bucketManager->fetch($bucket . ':' . $key, $bucket, $newKey, $ops);
if ($err !== null) {
    echo '裁剪失败:' . $err->message();
} else {
    echo '裁剪成功';
}
?>
Copy after login

Analysis:
In the code, we first introduce the Qiniu Cloud SDK package and create an Auth and BucketManager object.
Next, we define the image key to be cropped, the image key saved after cropping, and the width, height, and horizontal and vertical coordinates of the starting point after cropping.
We set the parameters for cropping by splicing an operation string ops.
Finally, we call the fetch method of BucketManager and pass in the cropping parameters and storage space related information to crop the image.

  1. Picture Zoom
    Picture zoom refers to the operation of enlarging or reducing the picture according to a certain ratio. The following is an example of PHP code that uses the Qiniu Cloud storage interface to implement image scaling:
<?php
require 'vendor/autoload.php'; // 引入七牛云SDK

use QiniuAuth;
use QiniuStorageBucketManager;

$accessKey = 'YOUR_ACCESS_KEY';
$secretKey = 'YOUR_SECRET_KEY';
$bucket = 'YOUR_BUCKET';
$auth = new Auth($accessKey, $secretKey);
$bucketManager = new BucketManager($auth);

$key = 'image.jpg'; // 待缩放的图片key
$newKey = 'new_image.jpg'; // 缩放后保存的图片key
$scale = 0.5; // 缩放比例

$ops = 'imageView2/0/q/75|imageslim|slim';

list($ret, $err) = $bucketManager->fetch($bucket . ':' . $key, $bucket, $newKey, $ops);
if ($err !== null) {
    echo '缩放失败:' . $err->message();
} else {
    echo '缩放成功';
}
?>
Copy after login

Analysis:
In the code, we also introduced the Qiniu Cloud SDK package and created the Auth and BucketManager object.
We define the image key to be scaled, the image key to be saved after scaling, and the scaling ratio.
By splicing an operation string ops, the scaling parameters are set.
Finally, we call the fetch method of BucketManager, pass in the scaling parameters and storage space related information, and perform image scaling.

3. Summary
This article introduces the method of using PHP combined with Qiniu cloud storage interface to realize image cropping and scaling, and gives corresponding code examples. By using the powerful storage and processing capabilities of Qiniu Cloud, developers can easily perform various operations such as cropping and zooming of images to present users with better image display effects. I hope this article can be helpful to the image processing problems developers encounter in daily development.

Reference materials:

  • Qiniu Cloud Storage official website: https://www.qiniu.com/
  • Qiniu Cloud Storage PHP SDK: https:/ /github.com/qiniu/php-sdk

The above is the detailed content of Method of cropping and zooming images using PHP and Qiniu cloud storage interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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