var fs = require('fs'); var request = require("request"); var src = "https://...../activity/img/hb11.jpg"; const last = src.lastIndexOf('/') down2img(); async function down2img(){ let ret = await downImg(src,1); console.log('down 1 over' + ret); ret = await downImg(src,2); console.log('down 1 over' + ret); } function downImg(url,para) { return new Promise((resolve, reject) => { // 块方式写入文件 var writeStream = fs.createWriteStream(para+'hb11.jpg'); var readStream = request(url); readStream.pipe(writeStream); writeStream.on('error', (err) => { reject(err); }); writeStream.on('finish', () => { console.log("文件写入成功"); writeStream.end(); resolve(true); // 括号里面的是返回值 }); }); } /* var writeStream = fs.createWriteStream('hb11.jpg'); var readStream = request(src) readStream.pipe(writeStream); readStream.on('end', function() { console.log('文件下载成功'); }); readStream.on('error', function() { console.log("错误信息:" + err) }) writeStream.on("finish", function() { console.log("文件写入成功"); writeStream.end(); }); */
图片缩放
var fs = require('fs');
var gm = require('gm');
gm('./1hb11.jpg')
.resize(50, 50,"!")
.write('./50.jpg', function (err) {
if (!err)
console.log('done');
else
console.log(err);
});