Permalink
Browse files

Cleanup, refactoring, use sharp for for grayscale

  • Loading branch information...
1 parent 6a686a7 commit 8d4dc0ab748aebe88d09cf599e0a2786a07a3419 @DMarby committed Nov 9, 2014
Showing with 236 additions and 222 deletions.
  1. +1 −0 .jshintignore
  2. +3 −0 .jshintrc
  3. +14 −19 buildcache.js
  4. +48 −54 imageProcessor.js
  5. +94 −81 index.js
  6. +2 −1 package.json
  7. +74 −67 server.js
View
@@ -0,0 +1 @@
+node_modules
View
@@ -0,0 +1,3 @@
+{
+ "asi": true
+}
View
@@ -1,39 +1,34 @@
var sharp = require('sharp')
var path = require('path')
+var async = require('async')
var config = require('./config')()
var fs = require('fs')
var imageProcessor = require('./imageProcessor')(sharp, path, config, fs)
var images = require(config.image_store_path)
+fs.mkdir(config.cache_folder_path, function (error) {})
+
var index = process.argv[2] || 0
console.log('Start: %s', index)
-sharp.cache(0)
+if (index > 0) {
+ images.splice(0, index)
+}
-var nextImage = function (the_index, callback) {
+async.eachLimit(images, 5, function (image, next) {
var width = 458
var height = 354
- var filePath = images[the_index].filename
var blur = false
- imageProcessor.getProcessedImage(width, height, null, false, false, filePath, false, function (error, imagePath) {
+ imageProcessor.getProcessedImage(width, height, null, false, false, image.filename, false, function (error, imagePath) {
if (error) {
- console.log('filePath: ' + filePath)
+ console.log('filePath: ' + image.filename)
console.log('imagePath: ' + imagePath)
console.log('error: ' + err)
}
- console.log('%s done', the_index)
- callback()
+ console.log('%s done', image.id)
+ next()
})
-}
-
-var imageLinksCallback = function () {
- index++
- if (index >= images.length) {
- console.log('Done!')
- return
- }
- nextImage(index, imageLinksCallback)
-}
-
-nextImage(index, imageLinksCallback)
+}, function (error) {
+ console.log('Done')
+})
View
@@ -1,13 +1,48 @@
-module.exports = exports = function (sharp, path, config, fs) {
- var gm = require('gm')
+var gm = require('gm')
+module.exports = exports = function (sharp, path, config, fs) {
var ImageProcessor = {
+ 'getProcessedImage': function (width, height, gravity, gray, blur, filePath, shortName, callback) {
+ gravity = ImageProcessor.getGravity(gravity)
+ ImageProcessor.getAndCheckDestination(width, height, gravity, blur, filePath, gray ? 'gray-' : '', shortName, function (exists, destination) {
+ if (exists) {
+ return callback(null, destination)
+ }
+
+ ImageProcessor.imageResize(width, height, gravity, filePath, destination, gray, function (error, destination) {
+ if (error) {
+ ImageProcessor.deleteFile(destination)
+ return callback(error)
+ }
+
+ if (blur) {
+ gm(destination).blur(0, 5).write(destination, function (error) {
+ if (error) {
+ ImageProcessor.deleteFile(destination)
+ return callback(error)
+ }
+ callback(null, destination)
+ })
+ } else {
+ callback(null, destination)
+ }
+ })
+ })
+ },
+
'getGravity': function(gravity) {
gravity = gravity ? gravity : 'center'
gravity = gravity == 'centre' ? 'center' : gravity
return gravity
},
+ 'getAndCheckDestination': function (width, height, gravity, blur, filePath, prefix, shortName, callback) {
+ var destination = shortName ? ImageProcessor.getShortDestination(width, height, gravity, blur, filePath, prefix) : ImageProcessor.getDestination(width, height, gravity, blur, filePath, prefix)
+ fs.exists(destination, function (exists) {
+ callback(exists, destination)
+ })
+ },
+
'getDestination': function (width, height, gravity, blur, filePath, prefix) {
return config.cache_folder_path + '/' + prefix + path.basename(filePath, path.extname(filePath)) + '-' + width + 'x' + height + '-' + gravity + (blur ? '-blur' : '') + '.jpeg'
},
@@ -16,66 +51,25 @@ module.exports = exports = function (sharp, path, config, fs) {
return config.cache_folder_path + '/' + prefix + width + '^' + height + '-' + gravity + (blur ? '-blurred' : '') + '.jpeg'
},
- 'getAndCheckDestination': function (width, height, gravity, blur, filePath, prefix, shortName, callback) {
- var destination = shortName ? ImageProcessor.getShortDestination(width, height, gravity, blur, filePath, prefix) : ImageProcessor.getDestination(width, height, gravity, blur, filePath, prefix)
- fs.exists(destination, function (exists) {
- callback(exists, destination)
- })
- },
-
- 'imageResize': function (width, height, gravity, filePath, destination, callback) {
+ 'imageResize': function (width, height, gravity, filePath, destination, gray, callback) {
try {
- sharp(filePath).rotate().resize(width, height).crop(sharp.gravity[gravity]).jpeg().progressive().toFile(destination, function (error) {
+ var image = sharp(filePath).rotate().resize(width, height).crop(sharp.gravity[gravity]);
+
+ if (gray) {
+ image.grayscale()
+ }
+
+ image.jpeg().progressive().toFile(destination, function (error) {
callback(error, destination)
})
} catch (error) {
callback(error, null)
}
},
- 'getProcessedImage': function (width, height, gravity, gray, blur, filePath, shortName, callback) {
- gravity = ImageProcessor.getGravity(gravity)
- ImageProcessor.getAndCheckDestination(width, height, gravity, blur, filePath, gray ? 'gray-' : '', shortName, function (exists, destination) {
- if (exists) {
- return callback(null, destination)
- }
- ImageProcessor.imageResize(width, height, gravity, filePath, destination, function (error, destination) {
- if (error) {
- fs.unlink(destination, function (error) {
- console.log('Error, deleted file')
- })
- return callback(error)
- }
- if (gray) {
- var modifyImage = gm(destination).colorspace('GRAY')
- if (blur) {
- modifyImage.blur(0, 5)
- }
- modifyImage.write(destination, function (error) {
- if (error) {
- fs.unlink(destination, function (error) {
- console.log('Error, deleted file')
- })
- return callback(error)
- }
- callback(null, destination)
- })
- } else {
- if (blur) {
- gm(destination).blur(0, 5).write(destination, function (error) {
- if (error) {
- fs.unlink(destination, function (error) {
- console.log('Error, deleted file')
- })
- return callback(error)
- }
- callback(null, destination)
- })
- } else {
- callback(null, destination)
- }
- }
- })
+ 'deleteFile': function (destination) {
+ fs.unlink(destination, function (error) {
+ console.log('Error, deleted file')
})
},
Oops, something went wrong.

0 comments on commit 8d4dc0a

Please sign in to comment.