Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

changing url.. imagepath/WIDTHxHEIGHT.jpg

  • Loading branch information...
commit 0188c602fdb536147d633ea5b08f97abd90b59ea 1 parent 7d0779a
@saml saml authored
Showing with 32 additions and 24 deletions.
  1. +2 −0  .gitignore
  2. +2 −2 README.markdown
  3. +28 −22 src/main.js
View
2  .gitignore
@@ -0,0 +1,2 @@
+tmp/
+img
View
4 README.markdown
@@ -1,10 +1,10 @@
# About
-For GET requests, dynamically resizes image using ImageMagick and serves the resized image.
+For GET requests, dynamically resizes image using ImageMagick `convert` command and serves the resized image.
For example,
- GET /some/image.c.10x20.jpg
+ GET /some/image.jpg/10x20.jpg
will execute
View
50 src/main.js
@@ -18,22 +18,27 @@ var Server = function(convert, srcDir, destDir) {
var me = {};
- var regex = /^\/(.+)\.c\.(\d+)x(\d+)(\.\w+)$/;
+ var regexForThumb = /^\/(.+)\/(\d+)x(\d+)(\.\w+)$/;
+
+ var getSourceImage = function(shortPath) {
+ return path.join(srcDir, shortPath);
+ };
var parseUrl = function(url) {
- var m = regex.exec(url);
+ var m = regexForThumb.exec(url);
if (!m) {
return null;
}
+
var ext = m[4];
- var src = path.join(srcDir, '%s%s'.f(m[1], ext));
+ var src = getSourceImage(m[1]);
var out = path.join(destDir, tempfile.getName("", ext));
- return {
- width: m[2] * 1
- , height: m[3] * 1
- , src: src
- , out: out
- };
+ return ({
+ width: m[2] * 1,
+ height: m[3] * 1,
+ src: src,
+ out: out
+ });
};
var getMimeType = function(name) {
@@ -166,21 +171,22 @@ var Server = function(convert, srcDir, destDir) {
}
var o = parseUrl(request.url);
- if (!o) {
- emitter.emit('error', me.do404, 'cannot parse: ' + request.url);
- return;
- }
-
- var size = '%sx%s'.f(o.width, o.height);
- resizeImage(o.src, o.out, size, function() {
- serveFile(o.out, undefined, function() {
- fs.unlink(o.out, function(err) {
- if (err) {
- sys.log('error while deleting: ' + o.out);
- }
+ if (o && o.src && o.out && o.width && o.height) {
+ //it is thumbnail url. resize it and serve.
+ var size = '%sx%s'.f(o.width, o.height);
+ resizeImage(o.src, o.out, size, function() {
+ serveFile(o.out, undefined, function() {
+ fs.unlink(o.out, function(err) {
+ if (err) {
+ sys.log('error while deleting: ' + o.out);
+ }
+ });
});
});
- });
+ } else {
+ //try to serve original without resizing.
+ serveFile(getSourceImage(request.url), undefined);
+ }
};
me.start = function() {
Please sign in to comment.
Something went wrong with that request. Please try again.