zoukankan      html  css  js  c++  java
  • node-images 进行图片压缩

    前置条件:先安装images

     npm install images

    编写代码

    思路: 从指定文件夹遍历图片,执行压缩,压缩完成后放到指定文件夹中,并保持图片名无变化。

    var images = require("images");
    var fs = require("fs");
    
    var path = "1012";
    var outpath = "compress/";  
    
    
    function compress(path){
        fs.readdir(path, function(err, files){     
            if(err){
                console.log('error:
    ' + err);
                return;
            }
    
            files.forEach(function(file){
    
                fs.stat(path + '/' + file, function(err, stat){
                    if(err){console.log(err); return;}
                    if(stat.isDirectory()){                 
                        // 如果是文件夹遍历
                        compress(path + '/' + file);
                    }else{
                       
                         //遍历图片
                        console.log('文件名:' + path + '/' + file);
                        var name = path + '/' + file;
                        var outName =  outpath+file
    
                   images(name) .save(outName, {             
                                    quality : 82                    //保存图片到文件,图片质量为50
                                });
    
                    }               
                });
    
            });
    
        });
    }
    
    compress(path)
    

      

    测试执行: node img.js

    结果 

    经测试,在不改变图片宽高情况下,手机拍照原图进行压缩,压缩图片大小是原图的4分之1。

    附注:api接口

    images(file)

    Load and decode image from file
    从指定文件加载并解码图像

    images(width, height)

    Create a new transparent image
    创建一个指定宽高的透明图像

    images(buffer[, start[, end]])

    Load and decode image from a buffer
    从Buffer数据中解码图像

    images(image[, x, y, width, height])

    Copy from another image
    从另一个图像中复制区域来创建图像

    .fill(red, green, blue[, alpha])

    eg:images(200, 100).fill(0xff, 0x00, 0x00, 0.5) Fill image with color
    以指定颜色填充图像

    .draw(image, x, y)

    Draw image on the current image position( x , y )
    在当前图像( x , y )上绘制 image 图像

    .encode(type[, config])

    eg:images("input.png").encode("jpg", {operation:50}) Encode image to buffer, config is image setting.
    以指定格式编码当前图像到Buffer,config为图片设置,目前支持设置JPG图像质量
    Return buffer
    返回填充好的Buffer
    Note:The operation will cut off the chain
    注意:该操作将会切断调用链
    See:.save(file[, type[, config]]) 参考:.save(file[, type[, config]])

    .save(file[, type[, config]])

    eg:images("input.png").encode("output.jpg", {operation:50}) Encoding and save the current image to a file, if the type is not specified, type well be automatically determined according to the fileconfig is image setting. eg: { operation:50 }
    编码并保存当前图像到 file ,如果type未指定,则根据 file 自动判断文件类型,config为图片设置,目前支持设置JPG图像质量

    .size([width[, height]])

    Get size of the image or set the size of the image,if the height is not specified, then scaling based on the current width and height
    获取或者设置图像宽高,如果height未指定,则根据当前宽高等比缩放

    .resize(width[, height])

    Set the size of the image,if the height is not specified, then scaling based on the current width and height
    设置图像宽高,如果height未指定,则根据当前宽高等比缩放, 默认采用 bicubic 算法。

    .width([width])

    Get width for the image or set width of the image
    获取或设置图像宽度

    .height([height])

    Get height for the image or set height of the image
    获取或设置图像高度

    images.setLimit(width, height)

    Set the limit size of each image
    设置库处理图片的大小限制,设置后对所有新的操作生效(如果超限则抛出异常)

    images.setGCThreshold(value)

    Set the garbage collection threshold
    设置图像处理库自动gc的阈值(当新增内存使用超过该阈值时,执行垃圾回收)

    images.getUsedMemory()

    Get used memory (in bytes)
    得到图像处理库占用的内存大小(单位为字节)

    images.gc()

    Forced garbage collection
    强制调用V8的垃圾回收机制




  • 相关阅读:
    在Eclipse/STS里添加代码反编译器(.class)步骤
    关于页面添加字段
    关于jeesite Date和时间戳问题
    关于前端ajax通过实体类向后端传参报不存在问题
    引入字典
    关于添加主键
    简单的前后端分离项目 部署到 centos7
    Cenost7 Mysql5.7 安装 并打开远程访问
    npm install vue-cli -g 报错
    docker 笔记 docker 基础 docker 常用命令
  • 原文地址:https://www.cnblogs.com/fslnet/p/11769436.html
Copyright © 2011-2022 走看看