-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from chetan1/master
Merge in Chetan's code
- Loading branch information
Showing
7 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,23 @@ | ||
## Coming soon! | ||
# Middleware for proxying and manipulating images | ||
|
||
This module allows you to manipulate and convert images on the fly. | ||
|
||
It provides: | ||
|
||
* Resizing of images | ||
* Conversion of images to grayscale | ||
* Conversion of images into different formats | ||
|
||
|
||
## Installation | ||
|
||
npm install connect-image-proxy | ||
|
||
|
||
## Usage | ||
|
||
* Resize: hostname/route/?url=[url]&resize=1&height=[height]&width=[width] | ||
* Grayscale: hostname/route/?url=[url]&grayscale=1 | ||
* Conversion [JPG, PNG, GIF]: hostname/route/?url=[url]&format=[format] | ||
* Conversion + Grayscale + Resize: hostname/route/?url=[url]&format=png&grayscale=1&resize=1&height=[height]&width=[width] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var image_proxy = require('./../image-proxy'); | ||
|
||
var express = require('express'); | ||
var app = express.createServer(); | ||
|
||
app.configure(function(){ | ||
app.set('view engine', 'jade'); | ||
app.set('views', __dirname + '/views'); | ||
app.set('view options', { layout: false, pretty: true}); | ||
app.use("/images", express.static(__dirname + '/public')); | ||
}); | ||
|
||
|
||
app.get('/image', image_proxy.run); | ||
|
||
app.get('/test', function(req, res){ | ||
res.render('test.jade'); | ||
}); | ||
|
||
app.listen(3000); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<h2>Original Image<h2> | ||
<img src="../image/?url=http%3A%2F%2Flocalhost%3A3000%2Fimages%2Ftest.jpg" /> | ||
|
||
<h2>Resize<h2> | ||
<img src="../image/?url=http%3A%2F%2Flocalhost%3A3000%2Fimages%2Ftest.jpg&resize=1&height=120" /> | ||
|
||
<h2>Grayscale<h2> | ||
<img src="../image/?url=http%3A%2F%2Flocalhost%3A3000%2Fimages%2Ftest.jpg&grayscale=1" /> | ||
|
||
<h2>PNG Conversion<h2> | ||
<img src="../image/?url=http%3A%2F%2Flocalhost%3A3000%2Fimages%2Ftest.jpg&format=png" /> | ||
|
||
<h2>Resize + Grayscale + GIF Conversion<h2> | ||
<img src="../image/?url=http%3A%2F%2Flocalhost%3A3000%2Fimages%2Ftest.jpg&format=gif&resize=1&height=210&grayscale=1" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
var fs = require('fs'), | ||
path = require('path'), | ||
mime = require('mime'), | ||
url = require('url'), | ||
http = require('http'), | ||
gm = require('gm'); | ||
|
||
var run = function(req, res){ | ||
|
||
var remoteUrl = decodeURI(req.param('url')); | ||
var urlObject = url.parse(remoteUrl); | ||
var options = urlObject; | ||
var fileExt = path.extname(remoteUrl); | ||
var urlHost = urlObject.hostname; | ||
|
||
if(urlObject.port != undefined) | ||
urlHost += ":"+urlObject.port; | ||
|
||
if(urlHost != req.headers.host) | ||
throw('Error: Only local proxy allowed.' + urlHost + " " + req.headers.host); | ||
|
||
http.get(options, function(res) { | ||
|
||
var localFile = getRandomFileName(fileExt); | ||
var file = fs.createWriteStream(localFile); | ||
|
||
res.on('data', function(data) { | ||
file.write(data); | ||
}).on('end', function() { | ||
|
||
file.end(); | ||
|
||
if(!validateMime(localFile)) | ||
throw "Error: Unsupported file type."; | ||
|
||
var outputImage = gm(localFile); | ||
|
||
if(req.param('resize') == 1) | ||
{ | ||
var newWidth = req.param('width'); | ||
var newHeight = req.param('height'); | ||
|
||
if(newHeight == null && newWidth == null) | ||
throw("Atleast one parameter from height or width should be defined."); | ||
else | ||
{ | ||
outputImage = outputImage.resize(newWidth, newHeight); | ||
} | ||
} | ||
|
||
if(req.param('grayscale') == 1) | ||
{ | ||
outputImage = outputImage.type('grayscale'); | ||
} | ||
|
||
if(req.param('format') != undefined) | ||
{ | ||
outputImage = outputImage.setFormat(req.param('format')); | ||
imageType = req.param('format'); | ||
} | ||
|
||
fs.unlink(localFile, function (err) { | ||
if (err) throw err; | ||
}); | ||
|
||
outputImage.stream(renderImage); | ||
}); | ||
|
||
}).on('error', function(e) { | ||
throw('File cannot be downloaded: ' + e.message) | ||
}); | ||
|
||
function renderImage(err, stdout, stderr) { | ||
|
||
res.setHeader('Content-type', 'image/' + fileExt.substring(1)); | ||
|
||
stdout.on('data', function(chunk) { | ||
res.write(chunk); | ||
}); | ||
stdout.on('end', function() { | ||
res.end(); | ||
}); | ||
} | ||
|
||
}; | ||
|
||
function validateMime(name) | ||
{ | ||
var type = mime.lookup(name); | ||
var validTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg']; | ||
|
||
if(validTypes.indexOf(type) == -1) | ||
return false; | ||
else | ||
return true; | ||
} | ||
|
||
function getRandomFileName(ext) | ||
{ | ||
return __dirname + "/temp/" + Math.floor(Math.random()*100000000) + ext; | ||
} | ||
|
||
exports.run = run; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "connect-image-proxy", | ||
"version": "0.0.1", | ||
|
||
"description": "Middleware for proxying and manipulating images.", | ||
"keywords": ["images", "proxy", "graphicsmagick", "express", "middleware", "connect", "server-side"], | ||
|
||
"homepage": "https://github.com/mysociety/node-connect-image-proxy", | ||
"bugs": { | ||
"url" : "https://github.com/mysociety/node-connect-image-proxy/issues" | ||
}, | ||
|
||
"contributors": [ | ||
"Edmund von der Burg <evdb@mysociety.org> (http://www.mysociety.org/)", | ||
"Chetan Bansal <chetan1@gmail.com>" | ||
], | ||
|
||
"main": "connect-image-proxy", | ||
|
||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/mysociety/node-connect-image-proxy.git" | ||
}, | ||
|
||
"dependencies": { | ||
"gm": ">= 1.3.0", | ||
"mime": "1.2.4" | ||
}, | ||
|
||
"devDependencies": { | ||
"express": ">=2 <3", | ||
"jade": ">= 0.25.0" | ||
}, | ||
|
||
"engines": { | ||
"node": "*" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.