Skip to content

Commit

Permalink
adding conversion support + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bspink committed Jun 3, 2016
1 parent 0c098ce commit 75b914a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 29 deletions.
21 changes: 11 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var express = require('express'),
path = require('path'),
favicon = require('static-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser');

module.exports = function (config) {
var app = express();
Expand All @@ -25,7 +25,8 @@ module.exports = function (config) {
"/images/test.jpg",
"/images/portraitcat.jpg",
"/images/monster.png",
"/images/wat.jpg"
"/images/wat.jpg",
"/images/captainplanet.gif",
],
tests: [
"",
Expand All @@ -43,11 +44,11 @@ module.exports = function (config) {

try {
require('fs').mkdirSync('.tmp');
} catch(e) {};
} catch(e) {}

try {
require('fs').mkdirSync('.tmp/immp');
} catch(e) {};
} catch(e) {}

app.use('/im/*', require('./src')(config));

Expand Down Expand Up @@ -83,4 +84,4 @@ module.exports = function (config) {
});

return app;
}
};
8 changes: 7 additions & 1 deletion bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ var app = require('../app')({
graphicsMagick: true,
cacheFolder: process.cwd() + '/.tmp/immp',
// allowProxy: true,
imageDir: process.cwd() + '/public'
imageDir: process.cwd() + '/public',
convertTo: {
gif: {
fileType: 'png32',
mimeType: 'png'
}
}
});

app.set('port', process.env.PORT || 3000);
Expand Down
43 changes: 25 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function (_config) {
ttl: 1000 * 60 * 60 * 24 * 7, // 1 week
allowProxy: true,
imageDir: process.cwd(),
convertTo: null,
// convertTo: {},

}, _config);

Expand Down Expand Up @@ -130,27 +130,34 @@ module.exports = function (_config) {
*/
function (_imageSrc, _manipulationDoneCallback) {
var gmImage = gm(_imageSrc, image.hash).options(gmOptions);

async.waterfall([

//Get original content-type
// Get image format/content-type
function (_callback) {
gmImage.format({
bufferStream: true
}, function (_error, _format) {
if(_error) {
console.log(_error);
return _callback(_error);
}
image.format = _format;
if(!_res.headersSent) {
_res.header('Content-Type', 'image/' + _format.toLowerCase());
}

_callback(null);
});
}, _callback);
},
function (_format, _callback) {
// Check if we should convert.
var newFormat;
var mimeType;
_format = _format.toLowerCase();

if(config.convertTo && config.convertTo[_format] && config.convertTo[_format].fileType && config.convertTo[_format].fileType.toLowerCase() !== _format) {
newFormat = config.convertTo[_format].fileType.toLowerCase();
gmImage.setFormat(newFormat);
mimeType = config.convertTo[_format].mimeType;
_format = newFormat;
}
image.format = _format;
if(!mimeType) {
mimeType = _format.toLowerCase();
}
if(!_res.headersSent) {
_res.header('Content-Type', 'image/' + mimeType);
}
_callback(null);
},

// Custom crop as per request
function (_callback) {
// All params should exist and be numeric.
Expand All @@ -164,7 +171,7 @@ module.exports = function (_config) {
_callback(null);
},

//Get original size (after initial crop)
// Get size (after initial crop)
function (_callback) {
gmImage.size({
bufferStream: true
Expand Down
44 changes: 44 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,48 @@ describe('immp', function () {
});
});

describe('with convertTo set', function () {

before(function (_done) {
// Clear file cache.
var immpFiles = fs.readdirSync(immpPath);
immpFiles.forEach(function (_file) {
fs.unlinkSync(path.join(immpPath, '/', _file));
});

// Recreate server with new config option/s.
server.on('close', function () {
serverImmpConfig.convertTo = {
gif: {
fileType: 'png32',
mimeType: 'png'
}
};
// Set up the test server.
var debug = require('debug')('gm');
var app = require('../app')(serverImmpConfig);

app.set('port', serverPort);

server = app.listen(app.get('port'), function () {
debug('Express server listening on port ' + serverPort);
_done();
});
});
server.close();
});

it('should return in the appropriate format', function (_done) {
http.get(serverUrl + '/im/?image=/images/captainplanet.gif', function (_httpResponse) {
_httpResponse.statusCode.should.eql(200);
_httpResponse.headers['content-type'].should.eql('image/png');
gm(_httpResponse)
.options(gmOptions).format(function (error, format) {
format.should.eql('PNG');
_done();
});
});
});
});

});

0 comments on commit 75b914a

Please sign in to comment.