What is imagemin-jpegtran?
The imagemin-jpegtran npm package is a plugin for the imagemin image optimization tool that specifically optimizes JPEG images using jpegtran. It is used to compress JPEG images without losing quality, making it ideal for web developers looking to reduce image file sizes for faster loading times.
What are imagemin-jpegtran's main functionalities?
Basic JPEG Optimization
This feature allows you to optimize JPEG images in a specified directory and output the optimized images to another directory. The code sample demonstrates how to use imagemin-jpegtran to compress JPEG images in the 'images' folder and save the optimized images in the 'output' folder.
const imagemin = require('imagemin');
const imageminJpegtran = require('imagemin-jpegtran');
(async () => {
await imagemin(['images/*.jpg'], {
destination: 'output',
plugins: [
imageminJpegtran()
]
});
console.log('Images optimized');
})();
Progressive JPEG Optimization
This feature allows you to create progressive JPEGs, which are images that load in successive waves of detail, improving the user experience on slow connections. The code sample shows how to enable the progressive option in imagemin-jpegtran.
const imagemin = require('imagemin');
const imageminJpegtran = require('imagemin-jpegtran');
(async () => {
await imagemin(['images/*.jpg'], {
destination: 'output',
plugins: [
imageminJpegtran({ progressive: true })
]
});
console.log('Images optimized');
})();
JPEG Optimization with Arithmetic Coding
This feature allows you to use arithmetic coding for JPEG compression, which can result in smaller file sizes compared to Huffman coding. The code sample demonstrates how to enable arithmetic coding in imagemin-jpegtran.
const imagemin = require('imagemin');
const imageminJpegtran = require('imagemin-jpegtran');
(async () => {
await imagemin(['images/*.jpg'], {
destination: 'output',
plugins: [
imageminJpegtran({ arithmetic: true })
]
});
console.log('Images optimized');
})();
Other packages similar to imagemin-jpegtran
imagemin-mozjpeg
imagemin-mozjpeg is another plugin for imagemin that optimizes JPEG images using the mozjpeg encoder. It often achieves better compression rates than jpegtran, but it may be slower. It is suitable for scenarios where achieving the smallest possible file size is more important than the speed of compression.
jpegoptim-bin
jpegoptim-bin is a Node.js wrapper for jpegoptim, a command-line tool for optimizing JPEG files. It provides similar functionality to imagemin-jpegtran but can be used independently of imagemin. It offers various optimization options, including lossless and lossy compression.
jpeg-recompress-bin
jpeg-recompress-bin is a Node.js wrapper for jpeg-recompress, a tool that optimizes JPEGs using various compression algorithms. It aims to provide the best possible quality-to-size ratio and offers more advanced options for fine-tuning the compression process compared to imagemin-jpegtran.
imagemin-jpegtran
jpegtran imagemin plugin
Install
$ npm install --save imagemin-jpegtran
Usage
const imagemin = require('imagemin');
const imageminJpegtran = require('imagemin-jpegtran');
(async () => {
await imagemin(['images/*.jpg'], {
destination: 'build/images',
plugins: [
imageminJpegtran()
]
});
console.log('Images optimized');
})();
API
imageminJpegtran(options?)(buffer)
Returns a promise for a buffer.
options
Type: object
progressive
Type: boolean
Default: false
Lossless conversion to progressive.
arithmetic
Type: boolean
Default: false
Use arithmetic coding.
buffer
Type: buffer
Buffer to optimize.