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.3 (part of libjpeg-turbo) Node.js wrapper that makes it seamlessly available as a local dependency on OS X, Linux, FreeBSD, Solaris 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.
Install
- Install with npm:
npm install --save jpegtran-bin
Example usage
var execFile = require('child_process').execFile;
var jpegtranPath = require('jpegtran-bin').path;
execFile(jpegtranPath, ['-outfile', 'output.jpg', 'input.jpg'], function() {
console.log('Image minified');
});
Can also be run directly from ./node_modules/.bin/jpegtran
.
Dev
Note to self on how to update the binaries.
OS X and Linux
- Run
npm install
to build the binary.
The nasm
(Netwide Assember) package is required to build the binary on Ubuntu.
Windows
-
Download the Windows files 32/64-bit (GCC compiled) on a Windows machine
(current version 1.3.0, x64 libjpeg-turbo-1.3.0-gcc64.exe
and for x86 libjpeg-turbo-1.3.0-gcc.exe
)
-
Run the downloaded file to extract
-
In the extracted folder go to the bin
folder and copy jpegtran.exe
and libjpeg-62.dll
to jpegtran-bin\vendor\
folder
(for grunt-contrib-imagemin
the folder is grunt-contrib-imagemin\node_modules\jpegtran-bin\vendor\
)
License
Everything excluding the binaries licensed under the BSD license and copyright Google.
libjpeg-turbo licensed under the BSD license and copyright dcommander.