Skip to content

Commit

Permalink
add watermarking functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mpd committed Sep 25, 2013
1 parent bdbef91 commit a45155c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
47 changes: 47 additions & 0 deletions lib/image-magick.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ImageMagick.Templates = {
resizeCmd: "-resize '%{size}'",
fitCmd: "-gravity center -resize '%{size}'^ -extent %{size}",
cropCmd: "-crop %{crop} +repage -thumbnail '%{size}'",
watermarkCmd: "-gravity northeast",

checkValue: function(value) {
if(!value.toString().match(/^[^;"']+$/i))
Expand Down Expand Up @@ -88,6 +89,13 @@ ImageMagick.prototype.crop = function(params, callback) {
}
}

ImageMagick.prototype.watermark = function(params, callback) {
// if(!params.hasOwnProperty('size'))
// throw new Error('Resize needs size-param with format <width>x<height>!')

this._convert(ImageMagick.Templates.get('watermarkCmd', {}), params, callback)
}

// private

ImageMagick.prototype.execute = function(cmd, options, callback) {
Expand Down Expand Up @@ -275,3 +283,42 @@ ImageMagick.prototype._convert = function(_command, params, callback) {
})
})
}

ImageMagick.prototype._composite = function(_command, params, callback) {
var source = params.url
, self = this
, size = params.size || params.crop.split('+')[0]
, pathHashString
, srcTmpPath
, destTmpPath

srcTmpPath = this._createTmpPath(source)

pathHashString = source + ':' + _command

destTmpPath = this._createTmpPath(pathHashString)

this._localizeSource(source, srcTmpPath, function(err, exists, localizedSource) {
if(err && !exists)
return callback && callback(err, null, null)

self._getTempfile(localizedSource, destTmpPath, function(tmpfile) {
fs.exists(tmpfile, function(tmpFileExists) {
if(tmpFileExists) {
self.logger.log('Converted file already exists. Skipping composite')
callback(null, tmpfile)
} else {
self.logger.log('Converted file does not exist. Converting...')
var command = "convert -composite " + _command + " '" + localizedSource + "' /data/watermark/shared/watermark.png '" + tmpfile + "'"

if(!exists)
command = self._getFallbackImageCommand(size, tmpfile)

self.execute(command, { timeout: self.config.timeouts.convert }, function (err, stdout, stderr) {
callback && callback(err, tmpfile)
})
}
})
})
})
}
2 changes: 1 addition & 1 deletion lib/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ RequestHandler.prototype.isImageProcessUrl = function(req) {
}

RequestHandler.prototype.getImageProcessUrlRegExp = function() {
var template = "^\/(identify|resize|crop|fit)(\/([^\/\?]+))?"
var template = "^\/(watermark|identify|resize|crop|fit)(\/([^\/\?]+))?"

if (this.config.hasOwnProperty('namespace') && (this.config.namespace != ''))
template = template.replace("^", "^\/" + this.config.namespace)
Expand Down

0 comments on commit a45155c

Please sign in to comment.