简体   繁体   English

nodejs 在 JIMP 和 MOZJPEG 之间进行选择

[英]nodejs choosing between JIMP and MOZJPEG

I was wondering if there is a blaring reason to use jimp vs. imagemin-mozjpeg for compressing jpegs (I am already using both imagemin and jimp in my project already, imagemin-webp to serve next gen images, and jimp to convert pngs to jpegs in rare cases) So I am more looking for reasoning that is based on the following:我想知道是否有明显的理由使用jimpimagemin-mozjpeg来压缩 jpeg(我已经在我的项目中同时使用 imagemin 和 jimp,imagemin-webp 用于提供下一代图像,而 jimp 将 png 转换为 jpeg在极少数情况下)所以我更多地寻找基于以下内容的推理:

  1. Performance表现
  2. Reliability (I have noticed that there are some JPEGs mozjpeg has trouble with and fails on. specifically ones that I have used GNU Image Manipulation Program [GIMP] with.)可靠性(我注意到有一些 JPEG 文件 mozjpeg 有问题并且失败了。特别是我使用 GNU Image Manipulation Program [GIMP] 的那些。)

However, if someone has good reasons that don't align with the two aforementioned I would still like to hear them.但是,如果有人有充分的理由与上述两个不一致,我仍然想听听他们的意见。

Heres some quick links to the NPM packages mentioned if anyone needs them:如果有人需要,这里有一些 NPM 软件包的快速链接:
imagemin-mozjpeg imagemin-mozjpeg
jimp吉普

Performance表现

imagemin-mozjpeg uses mozjpeg to process images. imagemin-mozjpeg使用mozjpeg处理图像。 And mozjpeg itself is made using C language.mozjpeg本身是使用C语言制作的。 While jimp uses javascript to process it.jimp使用 javascript 来处理它。

As mention in the main repository jimp :如主存储库jimp中所述:

An image processing library for Node written entirely in JavaScript, with zero native dependencies.一个完全用 JavaScript 编写的 Node 图像处理库,本地依赖项为零。

We know the difference in performance between Javascript and C.我们知道 Javascript 和 C 之间的性能差异。

Reliability可靠性

I do not want much opinion in this section.我不想在这部分发表太多意见。 but we can see directly how the statistics of each repository.但是我们可以直接看到每个存储库的统计情况。

mozjpeg :莫兹佩格

  • Star: 4.1k星级: 4.1k
  • Open Issues: 76未解决的问题: 76
  • Closed Issues: 186已关闭的问题: 186

jimp : 吉普

  • Star: 10.3k星级: 10.3k
  • Open Issues: 157未解决问题: 157
  • Closed Issues: 430已关闭的问题: 430

I do not side with either.我也不赞成。 They all have worked well.他们都运作良好。 I really appreciate the work of the maintainers and contributors of the library library.我非常感谢库的维护者和贡献者所做的工作。

Yes, and it goes far beyond the performance of the compression process (ie how long it takes to compress an image, which is also important) or the relative activity of development of the library (which is arguably less important).是的,它远远超出了压缩过程的性能(即压缩图像需要多长时间,这也很重要)或库开发的相对活动(可以说不那么重要)。

I highly recommend reading Is WebP really better than JPEG?我强烈推荐阅读WebP 真的比 JPEG 更好吗? (and this discussion ), which shows that even among JPEG compression libraries, the implementation can have a significant impact on compression ratio. (和这个讨论),这表明即使在 JPEG 压缩库中,实现也会对压缩率产生重大影响。

In short, MozJPEG produces jpeg files that are 10% smaller than jpeg files produced by the reference JPEG implementation (libjpeg).简而言之,MozJPEG 生成的 jpeg 文件比参考 JPEG 实现 (libjpeg) 生成的 jpeg 文件小 10%。 Even more interesting, for images larger than 500px, MozJPEG actually produces jpeg files that are smaller than WebP.更有趣的是,对于大于 500px 的图像,MozJPEG 实际上会生成比 WebP更小的 jpeg 文件。

This leads to an interesting question.这就引出了一个有趣的问题。 It will depend on exactly your use case and priorities, but it might actually make sense to simplify and use MozJPEG for everything, and ditch WebP entirely.这将完全取决于您的用例和优先级,但实际上简化和使用 MozJPEG 并完全放弃 WebP 可能是有意义的。

Looking forward, AVIF might make sense as a true next-gen format (delivering 30% smaller images), but browser support is "coming soon".展望未来,AVIF 可能会成为真正的下一代格式(提供缩小 30% 的图像),但浏览器支持“即将推出”。 Alternatively, JPEG XL also looks promising, but the standard hasn't been finalized yet.或者,JPEG XL 看起来也很有希望,但标准尚未最终确定。 HEIC is problematic and I wouldn't count on wide support. HEIC 是有问题的,我不会指望广泛的支持。


Warning regarding jimp:关于 jimp 的警告:

As jimp is implemented in pure JavaScript, all image operations end up blocking the JS thread.由于 jimp 是在纯 JavaScript 中实现的,因此所有图像操作最终都会阻塞 JS 线程。 This is catastrophic in node.js.这在 node.js 中是灾难性的。

You must use the new Worker Threads API manually to run jimp on a thread.您必须手动使用新的工作线程 API在线程上运行 jimp。


Finally, a warning regarding selecting image manipulation libraries generally in the node.js world:最后,关于在 node.js 世界中选择图像处理库的警告:

From what I've seen, a majority of them end up writing temp files to disk and then invoking a child process to do the actual work, and then read the result back in. (eg something like child_process.exec('imageresizer -in temp/file.jpg -out temp/resized.jpg') ).据我所见,他们中的大多数最终将临时文件写入磁盘,然后调用子进程来完成实际工作,然后将结果读回。(例如child_process.exec('imageresizer -in temp/file.jpg -out temp/resized.jpg') )。

This is not an ideal way to do this, and it may be especially surprising when the API looks something like var img = await resizeImg(buffer) , which does not look like it writes to disk.这不是一个理想的方法,当 API 看起来像var img = await resizeImg(buffer)时,这可能会特别令人惊讶,它看起来不像它写入磁盘。

imagemin is one such library; imagemin 就是这样一个库; I would avoid it where performance matters.我会在性能很重要的地方避免它。

Instead, search for modules that implement bindings to native code on the libuv thread pool.相反,在 libuv 线程池上搜索实现与本机代码绑定的模块。 This will usually be the most performant way to work with images, since the operations happen on a thread in your node process and with minimal memory copying — and no disk I/O at all.这通常是处理图像的最高效方式,因为这些操作发生在节点进程中的线程上,并且只需最少的 memory 复制——而且根本没有磁盘 I/O。

I've never used it, but node-mozjpeg looks like a good candidate.我从未使用过它,但node-mozjpeg看起来是一个不错的候选者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM