-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vdemedes
committed
Jan 7, 2015
1 parent
4e45433
commit d721d6d
Showing
6 changed files
with
262 additions
and
7 deletions.
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
SRC = $(wildcard lib/*.js) | ||
DEST = $(SRC:lib/%.js=build/%.js) | ||
|
||
build: $(DEST) | ||
build/%.js: lib/%.js | ||
mkdir -p $(@D) | ||
./node_modules/.bin/6to5 -b generators $< -o $@ | ||
|
||
clean: | ||
rm -rf build |
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,47 @@ | ||
"use strict"; | ||
|
||
/** | ||
* Module dependencies | ||
*/ | ||
|
||
var system = require("system"); | ||
|
||
|
||
// arguments | ||
var params = JSON.parse(system.args[1]); | ||
|
||
var page = require("webpage").create(); | ||
|
||
// features | ||
var window = params.window; | ||
var disable = params.disable; | ||
var crop = params.crop; | ||
|
||
if (window) { | ||
page.viewportSize = params.window; | ||
} | ||
|
||
if (crop) { | ||
page.clipRect = { | ||
top: crop.y, | ||
left: crop.x, | ||
width: crop.width, | ||
height: crop.height | ||
}; | ||
} | ||
|
||
if (disable) { | ||
if (disable.javascript) { | ||
page.settings.javascriptEnabled = false; | ||
} | ||
|
||
if (disable.images) { | ||
page.settings.loadImages = false; | ||
} | ||
} | ||
|
||
var url = decodeURIComponent(params.url); | ||
page.open(url, function () { | ||
page.render(params.destPath); | ||
phantom.exit(); | ||
}); |
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,171 @@ | ||
"use strict"; | ||
|
||
/** | ||
* Module dependencies | ||
*/ | ||
|
||
var request = require("co-request"); | ||
var Image = require("magician"); | ||
var exec = require("co-exec"); | ||
|
||
var util = require("./util"); | ||
|
||
|
||
/** | ||
* Thumbbot | ||
*/ | ||
|
||
var Thumbbot = function Thumbbot(path) { | ||
this.path = path; | ||
this.options = {}; | ||
}; | ||
|
||
Thumbbot.prototype.format = function (format) { | ||
this.options.format = format; | ||
|
||
return this; | ||
}; | ||
|
||
Thumbbot.prototype.width = function (width) { | ||
if (!this.options.resize) this.options.resize = {}; | ||
|
||
this.options.resize.width = width; | ||
|
||
return this; | ||
}; | ||
|
||
Thumbbot.prototype.height = function (height) { | ||
if (!this.options.resize) this.options.resize = {}; | ||
|
||
this.options.resize.height = height; | ||
|
||
return this; | ||
}; | ||
|
||
Thumbbot.prototype.resize = function (width, height) { | ||
this.options.resize = { width: width, height: height }; | ||
|
||
return this; | ||
}; | ||
|
||
Thumbbot.prototype.window = function (width, height) { | ||
this.options.window = { width: width, height: height }; | ||
|
||
return this; | ||
}; | ||
|
||
Thumbbot.prototype.seek = function (position) { | ||
this.options.seek = position; | ||
|
||
return this; | ||
}; | ||
|
||
Thumbbot.prototype.crop = function (x, y, width, height) { | ||
this.options.crop = { x: x, y: y, width: width, height: height }; | ||
|
||
return this; | ||
}; | ||
|
||
Thumbbot.prototype.disable = function (feature) { | ||
if (!this.options.disable) this.options.disable = {}; | ||
|
||
this.options.disable[feature] = true; | ||
}; | ||
|
||
Thumbbot.prototype.type = function () { | ||
var extension = util.parseExtension(this.path); | ||
|
||
switch (extension) { | ||
case "mp4": | ||
case "3gp": | ||
case "mov": | ||
case "avi": | ||
return "video"; | ||
|
||
case "jpeg": | ||
case "jpg": | ||
case "png": | ||
case "gif": | ||
case "tiff": | ||
return "image"; | ||
|
||
default: | ||
return "page"; | ||
} | ||
}; | ||
|
||
Thumbbot.prototype.saveVideo = function (destPath) { | ||
var seek = this.options.seek || "00:00:01"; | ||
|
||
var command = "ffmpeg -i \"" + this.path + "\" -ss \"" + seek + "\" -vframes 1 \"" + destPath + "\""; | ||
|
||
return exec(command); | ||
}; | ||
|
||
Thumbbot.prototype.savePage = function (destPath) { | ||
var options = Object.assign({}, this.options); | ||
|
||
options.url = encodeURIComponent(this.path); | ||
options.destPath = destPath; | ||
options = JSON.stringify(options); | ||
|
||
var command = ["phantomjs", "--disk-cache=true", "--ignore-ssl-errors=true", "--web-security=false", "--ssl-protocol=TLSv1", __dirname + "/render.js", "'" + options + "'"].join(" "); | ||
|
||
return exec(command); | ||
}; | ||
|
||
Thumbbot.prototype.saveImage = function (destPath) { | ||
var image = new Image(this.path); | ||
|
||
var options = this.options; | ||
var _resize = options.resize; | ||
var _crop = options.crop; | ||
|
||
|
||
if (_resize) { | ||
image.resize(_resize.width, _resize.height); | ||
image.fit(options.fit || "clip"); | ||
} | ||
|
||
if (_crop) { | ||
image.crop(_crop.x, _crop.y, _crop.width, _crop.height); | ||
} | ||
|
||
return image.save(destPath); | ||
}; | ||
|
||
Thumbbot.prototype.save = function* (destPath) { | ||
var _type = this.type(); | ||
|
||
if (!destPath) { | ||
destPath = util.tmpPath(this.options.format); | ||
} | ||
|
||
switch (_type) { | ||
case "video": | ||
yield this.saveVideo(destPath); | ||
break; | ||
|
||
case "image": | ||
yield this.saveImage(destPath); | ||
break; | ||
|
||
case "page": | ||
yield this.savePage(destPath); | ||
break; | ||
|
||
default: | ||
throw new Error("Uknown file type given to Thumbbot"); | ||
} | ||
|
||
return new Image(destPath); | ||
}; | ||
|
||
|
||
|
||
|
||
/** | ||
* Module exports | ||
*/ | ||
|
||
module.exports = Thumbbot; |
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,31 @@ | ||
"use strict"; | ||
|
||
/** | ||
* Module exports | ||
*/ | ||
|
||
var exports = module.exports; | ||
|
||
exports.clone = clone; | ||
exports.tmpPath = tmpPath; | ||
exports.parseExtension = parseExtension; | ||
|
||
/** | ||
* Utilities | ||
*/ | ||
|
||
function clone(obj) { | ||
return JSON.parse(JSON.stringify(obj)); | ||
} | ||
|
||
function tmpPath(format) { | ||
if (!format) format = "jpg"; | ||
|
||
var filename = Math.random().toString(36).substring(7); | ||
|
||
return "/tmp/" + filename + "." + format; | ||
} | ||
|
||
function parseExtension(path) { | ||
return /\.([a-z0-9]{3,5})$/i.exec(path)[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 |
---|---|---|
@@ -1,5 +1 @@ | ||
require('6to5/register')({ | ||
blacklist: ['generators'] | ||
}); | ||
|
||
module.exports = require('./lib/thumbbot'); | ||
module.exports = require('./build/thumbbot'); |
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