Skip to content
Browse files

add npm cmd module;

add gitignore
  • Loading branch information...
1 parent 24448c9 commit b8c24238b43705f5f021896e67c6af4541eb7c94 @switer committed Oct 14, 2013
Showing with 79 additions and 10 deletions.
  1. +1 −0 .gitignore
  2. +3 −5 bin/imaging
  3. +62 −0 imaging.js
  4. +6 −5 run.js
  5. +7 −0 test/test2.js
View
1 .gitignore
@@ -0,0 +1 @@
+/node_modules
View
8 bin/imaging
@@ -27,26 +27,24 @@ function fixedImageUrl(url) {
}
if (!imgUri) {
- console.log('Please get a correct image url !'.red)
+ console.log('Please give a correct image url !'.red)
return;
} else {
imgUri = fixedImageUrl(imgUri);
}
var childArgs = [
__dirname + '/../run.js',
- encodeURIComponent(imgUri),
+ encodeURIComponent(imgUri)
]
if (program.width) {
childArgs.push(program.width);
} else {
childArgs.push(defaultWidth);
}
childArgs.push(encodeURIComponent(__dirname));
-
+childArgs.push('../');
childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
-// childProcess.exec('phantomjs ' + childArgs.join(' '), function(err, stdout, stderr) {
if (err) console.log(err);
else console.log(stdout);
- console.log(stderr);
})
View
62 imaging.js
@@ -0,0 +1,62 @@
+#!/usr/bin/env node
+var childProcess = require('child_process'),
+ phantomjs = require('phantomjs'),
+ program = require('commander'),
+ colors = require('colors'),
+ binPath = phantomjs.path,
+ config = {
+ defaultWidth : 40
+ };
+
+function _fixedImageUrl(url) {
+ if (url.match(/http[s]*\:\/\//)) return url;
+ else if (url.match(/[a-zA-Z]\:/) || url.match(/^\//)) return url;
+ else return process.cwd().replace('/$','') + '/' + url;
+}
+
+function _run (imgUri, options, callback) {
+ var childArgs = [
+ __dirname + '/run.js',
+ encodeURIComponent(imgUri)
+ ]
+
+ if (options.width) {
+ childArgs.push(options.width);
+ } else {
+ childArgs.push(config.defaultWidth);
+ }
+ childArgs.push(encodeURIComponent(__dirname));
+ childArgs.push('./');
+ childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
+ if (err) console.log(err);
+ // else console.log(stdout);
+ callback && callback(stdout);
+ })
+}
+
+exports.draw = function (/*src, [options], callback*/) {
+
+ var args = arguments,
+ src = args[0],
+ options = {},
+ callback = null;
+
+ var imgUri = src;
+ if (!imgUri) {
+ console.log('Please give a correct image url !'.red)
+ return;
+ } else {
+ imgUri = _fixedImageUrl(imgUri);
+ }
+ var param2 = args[1];
+
+ if (typeof(param2) == 'function') {
+ callback = param2;
+ } else if (typeof(param2) == 'object'){
+ options = param2;
+ }
+ if (!callback) callback = args[2];
+
+ _run(imgUri, options, callback);
+}
+
View
11 run.js
@@ -7,11 +7,13 @@ var args = require('system').args,
fs = require('fs'),
cwd = fs.workingDirectory;
-var dirname = decodeURIComponent(args[3]);
+var dirname = decodeURIComponent(args[3]),
+ relativePath = args[4];
+
fs.changeWorkingDirectory(dirname);
var page = require('webpage').create(),
- color = require(dirname.replace(/\/$/) + '/../node_modules/colors/colors.js'),
+ color = require(dirname.replace(/\/$/) + '/' + relativePath + '/node_modules/colors/colors.js'),
pix = '',
// pix = '●',
// pix = 'o',
@@ -52,7 +54,6 @@ var colorMap = {
}
var params = argsParse(args);
-
function argsParse (args) {
width = args[2];
return {
@@ -87,7 +88,7 @@ function canvasProcess (height) {
draw(colors, function () {
setTimeout(function () {
phantom.exit();
- }, 750);
+ }, 500);
});
return pixData;
}
@@ -130,6 +131,6 @@ page.onConsoleMessage = function(msg) {
}
};
page.settings["localToRemoteUrlAccessEnabled"] = true;
-page.open('../res/index.html?width=' + params.width + '&src=' + params.img, function (status) {
+page.open(relativePath + 'res/index.html?width=' + params.width + '&src=' + params.img, function (status) {
if (status === 'fail') console.log('Runing index.html error !');
});
View
7 test/test2.js
@@ -0,0 +1,7 @@
+var imaging = require('imaging');
+imaging.draw('images/1.jpg', function (resp) {
+ console.log(resp);
+});
+imaging.draw('images/1.jpg', {width:50}, function (resp) {
+ console.log(resp);
+});

0 comments on commit b8c2423

Please sign in to comment.
Something went wrong with that request. Please try again.