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
2. Implementation method
<?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 '裁剪成功'; } ?>
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.
<?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 '缩放成功'; } ?>
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:
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!