From a45155c76a4d1b6c9b3db78e55abcc8d2f1cb60c Mon Sep 17 00:00:00 2001 From: mpd Date: Wed, 25 Sep 2013 15:14:49 -0700 Subject: [PATCH] add watermarking functionality --- lib/image-magick.js | 47 ++++++++++++++++++++++++++++++++++++++++++ lib/request-handler.js | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/image-magick.js b/lib/image-magick.js index 6654e11..a98e4d0 100644 --- a/lib/image-magick.js +++ b/lib/image-magick.js @@ -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)) @@ -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 x!') + + this._convert(ImageMagick.Templates.get('watermarkCmd', {}), params, callback) +} + // private ImageMagick.prototype.execute = function(cmd, options, callback) { @@ -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) + }) + } + }) + }) + }) +} diff --git a/lib/request-handler.js b/lib/request-handler.js index 25544d3..3d54fb9 100644 --- a/lib/request-handler.js +++ b/lib/request-handler.js @@ -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)