Created
July 15, 2015 16:10
-
-
Save bevacqua/b5718070018ca0593863 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var fs = require('fs'); | |
var gm = require('gm'); | |
var but = require('but'); | |
var path = require('path'); | |
var contra = require('contra'); | |
var Imagemin = require('imagemin'); | |
var pngquant = require('imagemin-pngquant'); | |
var mozjpeg = require('imagemin-mozjpeg'); | |
var gifsicle = require('imagemin-gifsicle'); | |
var winston = require('winston'); | |
var prettyBytes = require('pretty-bytes'); | |
var level = process.env.LOGGING_LEVEL; | |
var limits = { | |
width: 900, | |
height: 550 | |
}; | |
function findPlugin (file) { | |
var ext = path.extname(file).toLowerCase(); | |
if (ext === '.png') { | |
return pngquant(); | |
} else if (ext === '.jpg' || ext === '.jpeg') { | |
return mozjpeg(); | |
} else if (ext === '.gif') { | |
return gifsicle({ interlaced: true }); | |
} | |
} | |
function getMinifier (options) { | |
var imagemin = new Imagemin().src(options.file).dest(path.dirname(options.file)); | |
var plugin = findPlugin(options.file); | |
if (plugin) { | |
imagemin.use(plugin); | |
} | |
return imagemin; | |
} | |
function log (options) { | |
if (level !== 'debug') { | |
return; | |
} | |
fs.stat(options.file, function gotStats (err, stats) { | |
if (err) { | |
return; | |
} | |
var was = prettyBytes(options.size); | |
var is = prettyBytes(stats.size); | |
var difference = options.size - stats.size; | |
var diff = prettyBytes(-difference); | |
var percentage = -(100 - stats.size * 100 / options.size).toFixed(2); | |
winston.debug('%s was %s, is %s, diff %s [%s%]', options.name, was, is, diff, percentage, winston.ext()); | |
}); | |
} | |
function shrink (options, done) { | |
gm(options.file).autoOrient().resize(limits.width, limits.height).write(options.file, done); | |
} | |
function optimize (options, done) { | |
contra.series([ | |
function shrinkStep (next) { | |
shrink(options, next); | |
}, | |
function minifyStep (next) { | |
getMinifier(options).run(next); | |
}, | |
function logStep (next) { | |
process.nextTick(logLater); | |
next(); | |
} | |
], but(done)); | |
function logLater () { | |
log(options); | |
} | |
} | |
module.exports = { | |
optimize: optimize | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage (assumes
file
pulled from Express)Usage (raw)