What is jpegtran-bin?
jpegtran-bin is an npm package that provides a binary wrapper for jpegtran, a command-line utility for optimizing JPEG images. It allows you to perform various operations on JPEG files such as lossless compression, cropping, and rotation.
What are jpegtran-bin's main functionalities?
Lossless Compression
This feature allows you to optimize JPEG images without losing quality. The code sample demonstrates how to use jpegtran-bin to perform lossless compression on an input JPEG file and save the optimized image as output.jpg.
const execFile = require('child_process').execFile;
const jpegtran = require('jpegtran-bin');
execFile(jpegtran, ['-optimize', '-outfile', 'output.jpg', 'input.jpg'], err => {
if (err) {
throw err;
}
console.log('Image optimized');
});
Cropping
This feature allows you to crop a JPEG image. The code sample demonstrates how to use jpegtran-bin to crop a 100x100 pixel area from the input image starting at coordinates (10, 10) and save the cropped image as output.jpg.
const execFile = require('child_process').execFile;
const jpegtran = require('jpegtran-bin');
execFile(jpegtran, ['-crop', '100x100+10+10', '-outfile', 'output.jpg', 'input.jpg'], err => {
if (err) {
throw err;
}
console.log('Image cropped');
});
Rotation
This feature allows you to rotate a JPEG image. The code sample demonstrates how to use jpegtran-bin to rotate the input image by 90 degrees and save the rotated image as output.jpg.
const execFile = require('child_process').execFile;
const jpegtran = require('jpegtran-bin');
execFile(jpegtran, ['-rotate', '90', '-outfile', 'output.jpg', 'input.jpg'], err => {
if (err) {
throw err;
}
console.log('Image rotated');
});
Other packages similar to jpegtran-bin
imagemin-jpegtran
imagemin-jpegtran is a plugin for imagemin that uses jpegtran to optimize JPEG images. It offers similar functionalities to jpegtran-bin but is designed to be used within the imagemin ecosystem, which provides a broader range of image optimization tools.
jpegoptim-bin
jpegoptim-bin is an npm package that provides a binary wrapper for jpegoptim, another command-line utility for optimizing JPEG images. It offers similar functionalities to jpegtran-bin, such as lossless compression and optimization, but uses a different underlying tool.
mozjpeg
mozjpeg is an npm package that provides a binary wrapper for MozJPEG, a JPEG encoder that aims to improve JPEG compression while maintaining compatibility with the JPEG standard. It offers advanced compression techniques and can achieve smaller file sizes compared to jpegtran-bin.
node-jpegtran-bin
jpegtran 1.2.90 (part of libjpeg-turbo) Node.js wrapper that makes it seamlessly available as a local dependency on OS X, Linux and Windows. Most commonly used to losslessly minify JPEG images.
libjpeg-turbo is a derivative of libjpeg that uses SIMD instructions (MMX, SSE2, NEON) to accelerate baseline JPEG compression and decompression on x86, x86-64, and ARM systems. On such systems, libjpeg-turbo is generally 2-4x as fast as the unmodified version of libjpeg, all else being equal.
Example usage
var execFile = require('child_process').execFile;
var jpegtranPath = require('jpegtran-bin').path;
execFile(jpegtranPath, ['-outfile', 'output.jpg', 'input.jpg'], function(err, stdout, stderr) {
console.log('Image minified');
});
You can also run directly from ./node_modules/.bin/jpegtran-bin
Dev
Note to self on how to update the binaries.
OS X and Linux
Run npm install
to build the binary.
Windows
- Download the Windows files 32/64-bit (GCC compiled) on a Windows machine
- Run the downloaded file to extract
- Go to the
bin
folder at the destination and copy jpegtran.exe
and libjpeg-62.dll
License
Everything excluding the binaries licensed under the BSD license and copyright Google.
libjpeg-turbo licensed under the BSD license and copyright dcommander.