简介
本文档提供关于基础图片处理的相关的 API 概览以及 SDK 示例代码。
上传时使用图片处理
下面示例展示了如何在上传图片时自动实现图片处理。
图片上传完成后,COS 会存储原始图片和已处理过的图片。后续用户可以通过普通的下载请求获取处理结果。
示例代码
<view><button type="primary" bindtap="button">上传时使用图片处理</button></view>
Page({button: function () {wx.chooseMessageFile({count: 10,type: 'all',success: function (res) {var file = res.tempFiles[0];wxfs.readFile({filePath: file.path,success: function (res) {cos.putObject({Bucket: 'examplebucket-1250000000',Region: 'COS_REGION',Key: file.name,Body: res.data,Headers: {// 通过 imageMogr2 接口使用图片缩放功能:指定图片宽度为 200,宽度等比压缩'Pic-Operations':'{"is_pic_info": 1, "rules": [{"fileid": "desample_photo.jpg", "rule": "imageMogr2/thumbnail/200x/"}]}',},},(err, data) => {console.log(err || data);},);},fail: (err) => console.error(err),});},fail: (err) => console.error(err),});},});
对云上数据进行图片处理
下面示例展示了如何对已存储在 COS 的图片进行相应处理操作,并将结果存入到 COS。
示例代码
<view><button type="primary" bindtap="button">对云上数据进行图片处理</button></view>
Page({button: function () {cos.request({Bucket: 'examplebucket-1250000000',Region: 'COS_REGION',Key: 'exampleImage',Method: 'POST',Action: 'image_process',Headers: {// 通过 imageMogr2 接口使用图片缩放功能:指定图片宽度为 200,宽度等比压缩'Pic-Operations':'{"is_pic_info": 1, "rules": [{"fileid": "desample_photo.jpg", "rule": "imageMogr2/thumbnail/200x/"}]}',},},(err, data) => {console.log(err || data);},);},});
下载时使用图片处理
下面示例展示了如何在下载图片时实现图片处理。
示例代码
<view><button type="primary" bindtap="button">下载时使用图片处理</button></view>
Page({button: function () {cos.getObject({Bucket: 'examplebucket-1250000000',Region: 'COS_REGION',Key: 'exampleImage',QueryString: `imageMogr2/thumbnail/200x/`,},(err, data) => {console.log(err || data);},);},});
生成带图片处理参数的签名 URL
示例代码
<view><button type="primary" bindtap="sign">生成带图片处理参数的 URL,带签名</button><button type="primary" bindtap="unsign">生成带图片处理参数的 URL,不带签名</button></view>
Page({sign: function () {// 生成带图片处理参数的文件签名 URL,过期时间设置为 30 分钟。cos.getObjectUrl({Bucket: 'examplebucket-1250000000',Region: 'COS_REGION',Key: 'exampleobject',Query: {`imageMogr2/thumbnail/200x/`: ''},Expires: 1800,Sign: true,},(err, data) => {if (data) {console.log(data.URL);}},);},unsign: function () {// 生成带图片处理参数的文件 URL,不带签名。cos.getObjectUrl({Bucket: 'examplebucket-1250000000',Region: 'COS_REGION',Key: 'exampleobject',Query: {`imageMogr2/thumbnail/200x/`: ''},Sign: false,},(err, data) => {if (data) {console.log(data.URL);}},);},});