1.0.1 • Published 6 years ago

imageresizetool v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

加载压缩文件依赖

npm install

压缩文件

gulp

七个方法

Alt text

使用示例

var file = document.getElementById('demo').files[0];

imageResizeTool.fileResizetoFile(file,0.6,function(res){
    //回调中的res是一个压缩后的Blob类型(可以当做File类型看待)文件;
    console.log(res);
    //做出你要上传的操作;
})

使用Promise

使用过多的会调函数代码看上去会不优雅,如下:

var canvas = imageResizeTool.imagetoCanvas(img);
imageResizeTool.canvasResizetoFile(canvas,quality,file=>{
    imageResizeTool.filetoDataURL(file,data=>{
        imageResizeTool.dataURLtoImage(data,img=>{
            //做相应的操作
        })
    })
})

这时候可以引入 resize.promise.js 文件,以便使用 Promise 进行链式调用,如下:

imageResizeTool.imagetoCanvas(img)
    .then(canvas => imageResizeTool.canvasResizetoFile(canvas,quality))
    .then(file => imageResizeTool.filetoDataURL(file))
    .then(data => imageResizeTool.dataURLtoImage(data))
    .then(img => {
        //做相应的操作
    })
})

说明文档

王玉略的个人网站