jimp 图片裁减库

jimp 图片裁减库

jimp

jimp

使用

var Jimp = require('jimp');
 
// open a file called "lenna.png"
Jimp.read('lenna.png', (err, lenna) => {
  if (err) throw err;
  lenna
    .resize(256, 256) // resize
    .quality(60) // set JPEG quality
    .greyscale() // set greyscale
    .write('lena-small-bw.jpg'); // save
});

函数封装

//注意引入方法
const Jimp = require('jimp');
// 图片裁减保存
    jimpImg(target,width,height){
        try {
            if (target) {
                // 服务器保存图片地址
                let uploadJimpDir = target+`_${width}x${height}`+extname(target);
                // 前端访问图片地址
                let saveJimpDir = uploadJimpDir.slice(uploadJimpDir.indexOf('/upload'))
                Jimp.read(target, (err, lenna) => {
                    if (err) throw err;
                    lenna
                    .resize(width, height) // resize
                    .quality(60) // set JPEG quality
                    // .greyscale() // set greyscale
                    // .write(target+"_200x200"+extname(target)); // save
                    .write(uploadJimpDir); // save
                });
                return {
                    saveJimpDir,
                    uploadJimpDir
                } 
            } else {
                return {
                    saveJimpDir:'',
                    uploadJimpDir:""
                }
            }
        } catch (error) {
            return {
                saveJimpDir:'',
                uploadJimpDir:""
            }
        }
    }

你可能感兴趣的:(jimp 图片裁减库)