Commit 1d7b9b8d authored by rockyl's avatar rockyl

增加compressFile方法

parent e1843170
# Tiny-image
图片压缩
可以压缩png和jpg格式的图片,对png的压缩效果比较显著,压缩比高达15%。
# Install
yarn add http://gitlab2.dui88.com/laoqifeng/tiny-image.git
#Usage
```javascript
import {compressBuffer, compressFile} from 'tiny-image';
let tinyBuffer = compressBuffer('your buffer here');
or
let tinyBuffer = compressFile('you png or jpg file path here');
```
......@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var FormData = _interopDefault(require('form-data'));
require('fs');
var fs = _interopDefault(require('fs-extra'));
/**
* Created by rockyl on 2020-02-13.
......@@ -51,5 +51,11 @@ async function compressBuffer(buffer) {
})
}
async function compressFile(file) {
let buffer = await fs.readFile(file);
return compressBuffer(buffer);
}
exports.compressBuffer = compressBuffer;
exports.compressFile = compressFile;
//# sourceMappingURL=index.js.map
{"version":3,"file":"index.js","sources":["../src/index.js"],"sourcesContent":["/**\n * Created by rockyl on 2020-02-13.\n */\n\nimport FormData from 'form-data';\nimport fs from 'fs';\n\nconst tinifyUrl = 'http://tinify.duiba.com.cn/tinify';\n\nexport async function compressBuffer(buffer) {\n\treturn new Promise((resolve, reject) => {\n\t\tif (buffer && buffer.length > 0) {\n\t\t\tlet form = new FormData();\n\t\t\tform.append('file', buffer, {\n\t\t\t\tfilename: 'image.png',\n\t\t\t\tcontentType: 'image/png',\n\t\t\t});\n\n\t\t\tform.submit(tinifyUrl, function (err, res) {\n\t\t\t\tif (err) {\n\t\t\t\t\treject(err);\n\t\t\t\t} else {\n\t\t\t\t\tres.resume();\n\t\t\t\t\tlet resBuffer = Buffer.alloc(0);\n\t\t\t\t\tres.on('data', (d) => {\n\t\t\t\t\t\tresBuffer = Buffer.concat([resBuffer, d], resBuffer.length + d.length);\n\t\t\t\t\t});\n\t\t\t\t\tres.on('end', () => {\n\t\t\t\t\t\tif (resBuffer.length > 50) {\n\t\t\t\t\t\t\tresolve(resBuffer);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tlet str = resBuffer.toString();\n\t\t\t\t\t\t\tlet json = JSON.parse(str);\n\t\t\t\t\t\t\treject(json.msg);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t\tres.on('abort', () => {\n\t\t\t\t\t\treject('abort')\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t});\n\t\t} else {\n\t\t\treject('empty buffer')\n\t\t}\n\t})\n}\n"],"names":[],"mappings":";;;;;;;;;AAAA;;;AAGA,AAGA;AACA,MAAM,SAAS,GAAG,mCAAmC,CAAC;;AAEtD,AAAO,eAAe,cAAc,CAAC,MAAM,EAAE;CAC5C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK;EACvC,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;GAChC,IAAI,IAAI,GAAG,IAAI,QAAQ,EAAE,CAAC;GAC1B,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE;IAC3B,QAAQ,EAAE,WAAW;IACrB,WAAW,EAAE,WAAW;IACxB,CAAC,CAAC;;GAEH,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,GAAG,EAAE,GAAG,EAAE;IAC1C,IAAI,GAAG,EAAE;KACR,MAAM,CAAC,GAAG,CAAC,CAAC;KACZ,MAAM;KACN,GAAG,CAAC,MAAM,EAAE,CAAC;KACb,IAAI,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAChC,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,KAAK;MACrB,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;MACvE,CAAC,CAAC;KACH,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM;MACnB,IAAI,SAAS,CAAC,MAAM,GAAG,EAAE,EAAE;OAC1B,OAAO,CAAC,SAAS,CAAC,CAAC;OACnB,MAAM;OACN,IAAI,GAAG,GAAG,SAAS,CAAC,QAAQ,EAAE,CAAC;OAC/B,IAAI,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;OAC3B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;OACjB;MACD,CAAC,CAAC;KACH,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM;MACrB,MAAM,CAAC,OAAO,EAAC;MACf,CAAC,CAAC;KACH;IACD,CAAC,CAAC;GACH,MAAM;GACN,MAAM,CAAC,cAAc,EAAC;GACtB;EACD,CAAC;CACF;;;;"}
\ No newline at end of file
{"version":3,"file":"index.js","sources":["../src/index.js"],"sourcesContent":["/**\n * Created by rockyl on 2020-02-13.\n */\n\nimport FormData from 'form-data';\nimport fs from 'fs-extra';\n\nconst tinifyUrl = 'http://tinify.duiba.com.cn/tinify';\n\nexport async function compressBuffer(buffer) {\n\treturn new Promise((resolve, reject) => {\n\t\tif (buffer && buffer.length > 0) {\n\t\t\tlet form = new FormData();\n\t\t\tform.append('file', buffer, {\n\t\t\t\tfilename: 'image.png',\n\t\t\t\tcontentType: 'image/png',\n\t\t\t});\n\n\t\t\tform.submit(tinifyUrl, function (err, res) {\n\t\t\t\tif (err) {\n\t\t\t\t\treject(err);\n\t\t\t\t} else {\n\t\t\t\t\tres.resume();\n\t\t\t\t\tlet resBuffer = Buffer.alloc(0);\n\t\t\t\t\tres.on('data', (d) => {\n\t\t\t\t\t\tresBuffer = Buffer.concat([resBuffer, d], resBuffer.length + d.length);\n\t\t\t\t\t});\n\t\t\t\t\tres.on('end', () => {\n\t\t\t\t\t\tif (resBuffer.length > 50) {\n\t\t\t\t\t\t\tresolve(resBuffer);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tlet str = resBuffer.toString();\n\t\t\t\t\t\t\tlet json = JSON.parse(str);\n\t\t\t\t\t\t\treject(json.msg);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t\tres.on('abort', () => {\n\t\t\t\t\t\treject('abort')\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t});\n\t\t} else {\n\t\t\treject('empty buffer')\n\t\t}\n\t})\n}\n\nexport async function compressFile(file) {\n\tlet buffer = await fs.readFile(file);\n\treturn compressBuffer(buffer);\n}\n"],"names":[],"mappings":";;;;;;;;;AAAA;;;AAGA,AAGA;AACA,MAAM,SAAS,GAAG,mCAAmC,CAAC;;AAEtD,AAAO,eAAe,cAAc,CAAC,MAAM,EAAE;CAC5C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK;EACvC,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;GAChC,IAAI,IAAI,GAAG,IAAI,QAAQ,EAAE,CAAC;GAC1B,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE;IAC3B,QAAQ,EAAE,WAAW;IACrB,WAAW,EAAE,WAAW;IACxB,CAAC,CAAC;;GAEH,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,GAAG,EAAE,GAAG,EAAE;IAC1C,IAAI,GAAG,EAAE;KACR,MAAM,CAAC,GAAG,CAAC,CAAC;KACZ,MAAM;KACN,GAAG,CAAC,MAAM,EAAE,CAAC;KACb,IAAI,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAChC,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,KAAK;MACrB,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;MACvE,CAAC,CAAC;KACH,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM;MACnB,IAAI,SAAS,CAAC,MAAM,GAAG,EAAE,EAAE;OAC1B,OAAO,CAAC,SAAS,CAAC,CAAC;OACnB,MAAM;OACN,IAAI,GAAG,GAAG,SAAS,CAAC,QAAQ,EAAE,CAAC;OAC/B,IAAI,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;OAC3B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;OACjB;MACD,CAAC,CAAC;KACH,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM;MACrB,MAAM,CAAC,OAAO,EAAC;MACf,CAAC,CAAC;KACH;IACD,CAAC,CAAC;GACH,MAAM;GACN,MAAM,CAAC,cAAc,EAAC;GACtB;EACD,CAAC;CACF;;AAED,AAAO,eAAe,YAAY,CAAC,IAAI,EAAE;CACxC,IAAI,MAAM,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;CACrC,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;CAC9B;;;;;"}
\ No newline at end of file
......@@ -6,12 +6,12 @@
"license": "MIT",
"dependencies": {
"form-data": "^3.0.0",
"fs-extra": "^8.1.0",
"node-fetch": "^2.6.0"
},
"scripts": {
"dev": "rollup -c -w",
"build": "rollup -c",
"start": "node dist/index.js"
"build": "rollup -c"
},
"devDependencies": {
"rollup-plugin-progress": "^1.1.1"
......
......@@ -3,7 +3,7 @@
*/
import FormData from 'form-data';
import fs from 'fs';
import fs from 'fs-extra';
const tinifyUrl = 'http://tinify.duiba.com.cn/tinify';
......@@ -44,3 +44,8 @@ export async function compressBuffer(buffer) {
}
})
}
export async function compressFile(file) {
let buffer = await fs.readFile(file);
return compressBuffer(buffer);
}
/**
* Created by rockyl on 2020-02-15.
*/
const {compressFile} = require('.');
(async function(){
console.time('cost');
let buffer = await compressFile('/Users/rockyl/Pictures/一个亿.png');
console.timeEnd('cost');
console.log(buffer.length);
})();
......@@ -61,11 +61,32 @@ form-data@^3.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"
fs-extra@^8.1.0:
version "8.1.0"
resolved "https://registry.npm.taobao.org/fs-extra/download/fs-extra-8.1.0.tgz#49d43c45a88cd9677668cb7be1b46efdb8d2e1c0"
integrity sha1-SdQ8RaiM2Wd2aMt74bRu/bjS4cA=
dependencies:
graceful-fs "^4.2.0"
jsonfile "^4.0.0"
universalify "^0.1.0"
graceful-fs@^4.1.6, graceful-fs@^4.2.0:
version "4.2.3"
resolved "https://registry.npm.taobao.org/graceful-fs/download/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423"
integrity sha1-ShL/G2A3bvCYYsIJPt2Qgyi+hCM=
has-flag@^3.0.0:
version "3.0.0"
resolved "https://registry.npm.taobao.org/has-flag/download/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0=
jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.npm.taobao.org/jsonfile/download/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
integrity sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=
optionalDependencies:
graceful-fs "^4.1.6"
mime-db@1.43.0:
version "1.43.0"
resolved "https://registry.npm.taobao.org/mime-db/download/mime-db-1.43.0.tgz?cache=0&sync_timestamp=1578281193492&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fmime-db%2Fdownload%2Fmime-db-1.43.0.tgz#0a12e0502650e473d735535050e7c8f4eb4fae58"
......@@ -96,3 +117,8 @@ supports-color@^5.3.0:
integrity sha1-4uaaRKyHcveKHsCzW2id9lMO/I8=
dependencies:
has-flag "^3.0.0"
universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.npm.taobao.org/universalify/download/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
integrity sha1-tkb2m+OULavOzJ1mOcgNwQXvqmY=
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment