-
Notifications
You must be signed in to change notification settings - Fork 1
/
image-processor.js
70 lines (62 loc) · 2.58 KB
/
image-processor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var fs = require('fs-extra')
var target = function(file){return file.path}
var warnAboutGraphicMagicOnce = _.once(function(){
app.warn("[core] no GraphicsMagick installed, images won't be resized")
})
// Parsing string expression `(Width)x(Height)(Format)` -> `{width, height, format}`.
var parseImageFormatCache = {}
exports.parseImageFormat = function(expression){
if(!(expression in parseImageFormatCache)){
var result = null
var match = /(\d+)x?(\d*)([^\d]*)/.exec(expression)
if(match){
result = {}
if(match[1]) result.width = parseInt(match[1])
if(match[2]) result.height = parseInt(match[2])
if(match[3]) result.format = _s.strip(match[3])
}else app.warn('[image-processor] invalid image format expression ' + expression)
parseImageFormatCache[expression] = result
}
return parseImageFormatCache[expression]
}
var originalTarget = function(file){return file.lowerCasedPath}
var resizedTarget = function(file, sizeAlias){
return file.basePath + '.' + sizeAlias + '.' + file.extension
}
exports.process = function(srcDir, buildDir, file, config, Application, ecb, cb){
// Parsing image formats.
var sizes = []
if(config.imageFormats){
_(config.imageFormats).each(function(sizeFormat, sizeAlias){
var sizeFormat = exports.parseImageFormat(sizeFormat)
if(sizeFormat) sizes.push([sizeAlias, sizeFormat])
})
}
var originalPath = app.pathUtil.join(srcDir, file.path)
// Creating directory because GraphicMagic can't create parent paths.
var directoryPath = app.pathUtil.join(buildDir, file.parent.basePath)
app.ensurePathInBuildDirectory(directoryPath)
fs.ensureDir(directoryPath, _.fork(ecb, function(){
// Converting images to different sizes.
_(sizes).asyncEach(function(sizeAliasAndFormat, i, ecb, next){
var sizeAlias = sizeAliasAndFormat[0]
var sizeFormat = sizeAliasAndFormat[1]
var targetPath = app.pathUtil.join(buildDir, resizedTarget(file, sizeAlias))
app.ensurePathInBuildDirectory(targetPath)
app
require('gm')(originalPath)
.resize(sizeFormat.width, sizeFormat.height, sizeFormat.format)
.autoOrient()
.noProfile()
.write(targetPath, _.fork(function(err){
// If no GraphicsMagick installed, just copying the same image.
if(err.syscall == 'spawn' && err.code == 'ENOENT'){
warnAboutGraphicMagicOnce()
app.copyFile(originalPath, targetPath, ecb, next)
}else ecb(err)
}, next))
}, ecb, function(){
app.copyFile(originalPath, app.pathUtil.join(buildDir, originalTarget(file)), ecb, cb)
})
}))
}