UNPKG

1.93 kBJavaScriptView Raw
1/**
2 * 获取图片信息,支持png、jpg、jpeg、gif
3 *
4 * @param {any} imgPath
5 * @returns
6 *
7 * @memberOf Watch
8 */
9module.exports = (imgPath)=>{
10 const path = require('path'),
11 fs = require('fs-extra'),
12 // Pixelsmith = require('pixelsmith'),
13 sizeOf = require('image-size'),
14 pathInfo = require('./getPathInfo');
15
16 // let pix = new Pixelsmith(),
17 // imgInfo = {};
18 let imgInfo = {};
19
20 return new Promise((resolve,reject)=>{
21 let dimensions = sizeOf(imgPath),
22 info = pathInfo(imgPath);
23
24
25
26 imgInfo.width = dimensions.width;
27 imgInfo.height = dimensions.height;
28 imgInfo.name = info.name;
29 // imgInfo.type = info.extension.replace(/\./g,'');
30 imgInfo.type = dimensions.type;
31 imgInfo.path = imgPath;
32
33 resolve({
34 'status':'success',
35 'msg':`${imgPath} 信息获取成功`,
36 'data':imgInfo
37 });
38
39
40 // pix.createImages([imgPath],(err,imgs)=>{
41 // if(err){
42 // reject({
43 // 'status':'error',
44 // 'msg':`${imgPath} 信息获取错误`,
45 // 'data':err
46 // });
47 // return;
48 // };
49 // let img = imgs[0],
50 // info = pathInfo(imgPath);
51 // imgInfo.width = img.width;
52 // imgInfo.height = img.height;
53 // // 转为base64会导致内存占用过大
54 // // imgInfo.base64 = fs.readFileSync(imgPath).toString('base64');
55 // imgInfo.name = info.name;
56 // imgInfo.type = info.extension.replace(/\./g,'');
57 // imgInfo.path = imgPath;
58
59 // resolve({
60 // 'status':'success',
61 // 'msg':`${imgPath} 信息获取成功`,
62 // 'data':imgInfo
63 // });
64 // });
65 });
66};
\No newline at end of file