my.getimageinfo(object object)
基础库 或更高版本; 且 支付宝客户端 10.1.8 或更高版本; 若版本较低,建议采取 兼容处理 。
支小宝客户端 支持
安诊儿客户端 支持
主体: 企业支付宝小程序 、 个人支付宝小程序
获取图片信息。
object object
查看示例
属性 | 类型 | 默认值 | 必填 | 描述 |
---|---|---|---|---|
src | string | - | 是 | 图片路径,支持网络地址、本地临时文件、本地缓存文件、本地用户文件、代码包文件。
|
success | function | - | 否 | 调用成功的回调函数 |
fail | function | - | 否 | 调用失败的回调函数 |
complete | function | - | 否 | 调用结束的回调函数(调用成功、失败都会执行) |
success 回调函数
参数
object res
查看示例
属性 | 类型 | 兼容性 | 描述 | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
width | number | - | 图片宽度,单位为 px |
||||||||||||||||||||||||||||
height | number | - | 图片高度,单位为 px |
||||||||||||||||||||||||||||
path | string | - | 图片的 本地临时文件 路径 |
||||||||||||||||||||||||||||
orientation | string | 基础库: 1.10.0/2.3.1 |
返回图片的方向 |
||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
type | string | 基础库: 1.10.0/2.3.1 |
返回图片的格式 |
||||||||||||||||||||||||||||
|
fail 回调的参数为 object,error
属性为错误码,errormessage
属性为错误消息。
错误码 | 错误消息 | 凯发app官方网站的解决方案 |
---|---|---|
12(android)2(ios) | 接口参数无效。 |
检查 src 路径格式是否正确。 |
12(ios) | 文件不存在。 |
检查 src 路径是否有特殊字符或者缺少协议名称,例如有空格等等。 |
18(android) | 获取图片信息失败。 | 检查 src 链接是否真实有效存在。 |
"21 " | 下载文件失败。 | 检查 src 链接是否真实有效存在。 |
my.getimageinfo ({
src: 'https://img.alicdn.com/tps/tb1sxgyifxxxxc5xpxxxxxxxxxx.jpg',
success: function (res) {
console.log (res);
},
fail: function (err) {
console.log (err);
},
});
// mini.project.json 配置示例,下例中 source 为根目录(app.json 所在目录)下的文件夹,* 代表任意文件名。
{
"assetsinclude": ["image/*.jpg"]
}
my.getimageinfo ({
// 包文件路径与当前页面路径无关。/source/api.png 也可写作 source/api.png,含义相同,都是从项目根目录算起
src: '/source/api.png',
success: function (res) {
console.log (res);
},
fail: function (err) {
console.log (err);
},
});
my.getimageinfo ({
src: 'https://img.alicdn.com/tps/tb1sxgyifxxxxc5xpxxxxxxxxxx.jpg',
success: function (res) {
console.log ('getimageinfo: success', res);
my.detectfiletype ({
filepath: res.path,
success (res) {
console.log ('detectfiletype: success', res);
},
fail (err) {
console.log ('detectfiletype: fail', err);
},
});
},
fail: function (err) {
console.log ('getimageinfo: fail', err);
},
});
{
"height": 121,
"orientation": "up",
"path": "https://resource/67fb0515088f92ffba8e5a0df259b491.image",
"type": "png",
"width": 312
}
{
"error": 2,
"errormessage": "接口参数无效"
}
-
tip读取小程序 代码包文件 前需在
mini.project.json
中配置可读取的小程序文件。 -
tipios 端判断图片类型需选择原图,否则无法正确判断出 gif 格式的图片。
-
tip
最终得到的图片路径在 ide 上以实际后缀名为后缀(部分不常用的图片后缀名会以 .null 为后缀),在真机预览上以 .image 为后缀。
若需判断文件扩展真实后缀名,可使用 my.detectfiletype 进行查询,请以此接口返回结果为准,不过需注意此接口仅限真机调试,可参考上方示例代码。
-
bug支付宝客户端 10.5.36 版本之前,对于 jpg 图片,安卓端回调参数 type 的值为 jpg,ios 为 jpeg;10.5.36 以及之后版本,type 值均为 jpeg。