Created
July 14, 2012 12:05
-
-
Save gigablah/3110899 to your computer and use it in GitHub Desktop.
nodejs + gearman + gm + pngquant + pngcrush thumbnail generator
This file contains 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
#!/usr/bin/env node | |
var path = require('path'), | |
exec = require('child_process').exec, | |
when = require('when'), | |
gm = require('gm'), | |
Gearman = require('node-gearman'), | |
gearman = new Gearman(), | |
options = { | |
thumbDir: path.join(__dirname, 'thumb'), | |
uploadDir: path.join(__dirname, 'files'), | |
outputFormat: 'png' | |
}, | |
thumbnail = function(inputFile, outputFile) { | |
var deferred = when.defer(); | |
gm(inputFile).setFormat(options.outputFormat) | |
.quality(90) | |
.filter('Lanczos') | |
.gravity('Center') | |
.resize(100, 100, '>') | |
.sharpen(0, 0.3) | |
.background('transparent') | |
.extent(100, 100) | |
.write(outputFile, | |
function(err) { | |
if (err) { | |
deferred.reject(err); | |
} else { | |
console.log('thumbnail: done!'); | |
deferred.resolve(outputFile); | |
} | |
}); | |
return deferred.promise; | |
}, | |
pngquant = function(inputFilePromise) { | |
var deferred = when.defer(); | |
when(inputFilePromise, function(inputFile) { | |
exec('pngquant -f --ext .png ' + inputFile, {timeout: 60000}, function(err, stdout, stderr) { | |
if (err) { | |
deferred.reject(err); | |
} else { | |
console.log('pngquant: done!'); | |
deferred.resolve(inputFile); | |
} | |
}); | |
}); | |
return deferred.promise; | |
}, | |
pngcrush = function(inputFilePromise) { | |
var deferred = when.defer(); | |
when(inputFilePromise, function(inputFile) { | |
exec('pngcrush -ow ' + inputFile, {timeout: 60000}, function(err, stdout, stderr) { | |
if (err) { | |
deferred.reject(err); | |
} else { | |
console.log('pngcrush: done!'); | |
deferred.resolve(inputFile); | |
} | |
}); | |
}); | |
return deferred.promise; | |
}; | |
gearman.registerWorker('resize', function(payload, worker) { | |
if (!payload) { | |
console.log('Invalid job'); | |
worker.error(); | |
return; | |
} | |
var filename = payload.toString('utf-8'), | |
inputfile = path.join(options.uploadDir, filename), | |
outputfile = path.join( | |
options.thumbDir, | |
path.basename(filename, path.extname(filename)) + '.' + options.outputFormat | |
), | |
errorHandler = function(err) { | |
console.log('Error: ' + err); | |
worker.error(); | |
}; | |
console.log('thumbnail: ' + inputfile); | |
worker.write('thumbnail'); | |
try { | |
when(thumbnail(inputfile, outputfile), | |
function(file) { | |
console.log('pngquant: ' + file); | |
worker.write('pngquant'); | |
return pngquant(file); | |
}, errorHandler | |
).then( | |
function(file) { | |
console.log('pngcrush: ' + file); | |
worker.write('pngcrush'); | |
return pngcrush(file); | |
}, errorHandler | |
).then( | |
function(file) { | |
console.log('success: ' + file); | |
worker.end(file); | |
}, errorHandler | |
); | |
} | |
catch(err) { | |
errorHandler(err); | |
return; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment