zoukankan      html  css  js  c++  java
  • 下图片异步变同步

    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);
    });
  • 相关阅读:
    实例事件
    实例方法
    Extends Option 扩展选项
    Watch 选项 监控数据
    methods方法选项
    computed Option 计算选项
    propsData Option 全局扩展的数据传递
    component 父子组件
    component-04 动态绑定组件
    component 全局局部组件
  • 原文地址:https://www.cnblogs.com/cnchengv/p/10849084.html
Copyright © 2011-2022 走看看