hapi-imagemin-proxy
hapi-imagemin-proxy copied to clipboard
Hapi proxy for serving optimized images
hapi-imagemin-proxy
Image optimization proxy written in Node.js using hapi.
Allows you to resize an image and change image formats. Output is always optimized for the smallest file size.
-
http://localhost:5678/cat.gif,w100
- Resize to a width of 100px -
http://localhost:5678/cat.gif,w100,h50
- Fit into 100px × 50px (keeping aspect ratio) -
http://localhost:5678/find-peace.png,jpg
- Convert PNG to JPG -
http://localhost:5678/find-peace.png,w100,h50,jpg
- All combined
Usage
Requires GraphicsMagick to be installed (e.g. on Mac OS X via Homebrew: brew install graphicsmagick
).
npm install hapi-imagemin-proxy
Afterwards, include hapi-imagemin-proxy
as plug-in into your existing Hapi project (a demo server can be found in /example
):
const Hapi = require('hapi');
const server = new Hapi.Server();
server.register({
plugin: require('hapi-imagemin-proxy'),
options: {
source: '/path/to/images',
cache: {},
imageCache: {
engine: require('catbox-memory'),
options: {
expiresIn: 3600000
}
},
plugins: [
imageminGm.resize(),
imageminGm.convert(),
imageminJpegoptim({ progressive: true, max: 75 }),
imageminPngquant(),
imageminGifsicle({ optimizationLevel: 3 }),
imageminSvgo()
]
// ...
}
}, function (err) {
if (err) {
return {
console.error(err);
}
}
});
Options
-
source
: Location of the images to be served. Can be either a local path or a URL (required). -
wreck
: Whensource
is an URL, these request options are passed to Wreck. -
cache
: Sets Hapi's route.cache options -
imagecache
:-
engine
: Catbox caching engine. Must support binary data, e.g.catbox-s3
. Default:require('catbox-memory')
-
options
: Engine specific options (optional)-
maxByteSize
: only forcatbox-memory
, default:104857600
(100MB) -
expiresIn
: Cache time-to-live in milliseconds, default:3600000
(one hour)
-
-
-
plugins
: Array of imagemin optimization plug-ins, defaults:imageminJpegoptim({ progressive: true, max: 75 }), imageminPngquant(), imageminGifsicle({ optimizationLevel: 3 }), imageminSvgo()
TODO
- Use cjpeg-dssim for JPG optimization
- Rewrite using async/await to support [email protected]
License
MIT