wasm-vips libvips webassembly 实现

发布时间 2023-11-05 22:42:42作者: 荣锋亮

wasm-vips 是利用了emscripten将libvips 编译为webassembly 可以实现在node 以及浏览器中使用libvips 强大的图片处理处理
以下是一个简单的试用

参考试用

  • app.js
 
const Vips = require('wasm-vips');
 
async function init() {
    const vips = await Vips();
    vips.Image.newFromFile('test.png')
    .smartcrop(320, 320,{
        interesting: vips.Interesting.attention
    }).writeToFile('test2.png');
 
    const thumbnail = vips.Image.thumbnail('test.png', 320, {
        height: 320,
        no_rotate: true,
        crop: vips.Interesting.attention // 'attention'
      });
      thumbnail.writeToFile('test3.png');
}
init();

效果

原始图

生成图片

 

说明

以上是一个简单的使用,实际上也有基于node 的包装sharp 是一个很不错的选择,但是基于webwaasmbly 也是一个不错的选择,至少不需要对于
libvips 的依赖了,sharp 是使用的预编译好的依赖

参考资料

https://github.com/kleisauke/wasm-vips
https://www.libvips.org/
https://github.com/libvips/libvips
https://emscripten.org/
https://github.com/lovell/sharp
https://github.com/emscripten-core/emscripten