console.log("ok")

/*
var images = require("images");
var fs = require("fs");
var path = require("path");

var images = require("images");

images(path.join(__dirname, './image/A1.jpg')) //Load image from file
//加载图像文件
.size(400) //Geometric scaling the image to 400 pixels width
//等比缩放图像到400像素宽
//.draw(images("logo.png"), 10, 10) //Drawn logo at coordinates (10,10)
//在(10,10)处绘制Logo
.save("output.jpg", { //Save the image to a file,whih quality 50
quality : 50 //保存图片到文件,图片质量为50
});

*/
//编码图片依赖模块https://github.com/zhangyuanwei/node-images
var chokidar = require('chokidar');
var images = require("images");
var fs = require("fs");
var path = require("path");
var gm = require('gm');
function readFile(src,dst){
console.log(src);
console.log(dst);

if(fs.existsSync(src)){

//读取文件夹
fs.readdir(src,function(err,files){
if(err){
throw err;
}

files.forEach(function(filePath){
console.log(filePath);

var url = path.join(__dirname,src+"\\"+filePath),
dest = path.join(__dirname,dst+"\\"+filePath);
// console.log(url);
//console.log(dest);
fs.stat(url,function(err,stats){
if(err)throw err;

//是文件
if(stats.isFile()){
//正则判定是图片
//if(/.*\.(jpg)$/i.test(url)){
encoderjpg(url,dest);
/*}else if(/.*\.(png|gif)$/i.test(url)){
encoderjpg(url,dest);

}*/
}else if(stats.isDirectory()){
exists(url,dest,readFile);
}

})
});

});

}else{
throw "no files,no such!"
}
}

//这里处理文件跟复制有点相关,输出要检测文件是否存在,不存在要新建文件
function exists(url,dest,callback){

fs.exists(dest,function(exists){
if(exists){
callback && callback(url,dest);
}else{
//第二个参数目录权限 ,默认0777(读写权限)
fs.mkdir(dest,0777,function(err){
if (err) throw err;
callback && callback(url,dest);
});
}
});
}

function encoderjpg(url,destImg){
console.log(sourceImg);
console.log(destImg);
var sourceImg = images(url);
/* console.log(sourceImg.width());
console.log(sourceImg.height());*/
console.log(sourceImg)
console.log(/.*\.(jpg)$/i.test(url))
if(/.*\.(jpg)$/i.test(url)){
if(sourceImg.width()>600){
sourceImg //加载图像文件
.size(700) //等比缩放图像到1000像素宽
// .draw(images("pficon.jpg"),10,10) //在(10,10)处绘制Logo
.save(destImg,{
quality :80 //保存图片到文件,图片质量为50
})
}else{
sourceImg.save(destImg);
}
}else if(/.*\.(png|gif)$/i.test(url)){

if(sourceImg.width()>250){
gm(url).quality(80)
.resize(250)
.write(destImg, function (err) {
if (!err) console.log('done');
console.log(err)
})
}else{
sourceImg.save(destImg);
}

}

/*gm(sourceImg).quality(40)
.resize(700)
.write(destImg, function (err) {
if (!err) console.log('done');
console.log(err)
});*/
/* (new images(sourceImg)).encode(sourceImg, 100, 100,
function (data, error) {
console.log("xx");
// fs.writeFile("out.png", destImg, "binary", function(err) {
// console.log(err); // writes out file without error, but it's not a valid image
//});
}
);*/
}

var watcher = chokidar.watch("image", {
ignored: /(^|[\/\\])\../,//ignored: /node_modules|\.git/,
ignoreInitial: false,
persistent: true,
followSymlinks: true,
useFsEvents: true,
usePolling: true
});
var watcherFlag=false;
/*watcher
.on('add', function(path) {*/
/* console.log(path);
console.log(path);
encoderjpg(path,path.replace("image\\","out\\")); })
/* .on('addDir', function(path) { console.log('Directory', path, 'has been added'); })
.on('change', function(path) { console.log('File', path, 'has been changed'); })
.on('unlink', function(path) { console.log('File', path, 'has been removed'); })
.on('unlinkDir', function(path) { console.log('Directory', path, 'has been removed'); })
.on('error', function(error) { console.log('Error happened', error); })
.on('ready', function() { console.log('Initial scan complete. Ready for changes.');watcherFlag=true; })
.on('raw', function(event, path, details) { encoderjpg(path,path.replace("image","out"))})*/
// 监听增加,修改,删除文件的事件
var watcherFlag=false;
watcher.on('all', (event, path) => {

switch (event) {

// break;
case 'add':
if(watcherFlag)encoderjpg(path,path.replace("image\\","out\\"));
// if(!!watcherFlag){
//// console.log("YES"+watcherFlag);
// encoderjpg(path,path.replace("image\\","out\\"));
// }else{
// console.log("NO"+watcherFlag);
//encoderjpg(path,path.replace("image\\","out\\"));
// }

// }

console.log('Add.');
break;

case 'change':
console.log("change");
break;
case 'unlink':

break;
default:
break;
}
}).on('ready', function() {
watcherFlag=true;
console.log('Ready');
})
;
//readFile('image','out');

/*module.exports = readFile;*/

gm picture的更多相关文章

  1. How to solve GM MDI cannot complete the installation

    Dear Joy, I have a problem using GM MDI diagnostic tool. When I installed it on my laptop, the tool ...

  2. How to use GM MDI interface for programming

    GM has had its newest programming/J2534 Pass Thru device on the market for some years now. A lot has ...

  3. 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?

    复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...

  4. MFC Picture控件加载图片

    CStatic *pPic = (CStatic*)GetDlgItem(IDC_PICTURE); CBitmap bitmap; bitmap.LoadBitmapW(IDB_BITMAP2); ...

  5. [POJ1177]Picture

    [POJ1177]Picture 试题描述 A number of rectangular posters, photographs and other pictures of the same sh ...

  6. USACO 5.5 Picture(周长并)

    POJ最近做过的原题. /* ID: cuizhe LANG: C++ TASK: picture */ #include <cstdio> #include <cstring> ...

  7. 彩色照片转换为黑白照片(Color image converted to black and white picture)

    This blog will be talking about the color image converted to black and white picture. The project st ...

  8. HDU 1828 Picture(线段树扫描线求周长)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  9. MFC 配合 protobuff libevent 实现的Socket 的GM工具 框架

    MFC 配合 protobuff libevent 实现的Socket 的GM工具 框架

随机推荐

  1. 洛谷——P2421 A-B数对(增强版)

    题目背景 woshiren在洛谷刷题,感觉第一题:求两数的和(A+B Problem)太无聊了,于是增加了一题:A-B Problem,难倒了一群小朋友,哈哈. 题目描述 给出N 个从小到大排好序的整 ...

  2. 超级素数(sprime) (BFS)

    问题 G: 超级素数(sprime) 时间限制: 1 Sec  内存限制: 64 MB提交: 47  解决: 11[提交][状态][讨论版] 题目描述 超级素数是指一个素数,每去掉后面一个数字,总能保 ...

  3. Eclipse Build all and build project not working - jar missing

    Eclipse Build all and build project not working - jar missing

  4. oracle enable / disable all constraint

    beginfor i in (select constraint_name, table_name from user_constraints where table_name='') LOOPexe ...

  5. 安全管道工具SSF

    安全管道工具SSF   SSF(Secure Socket Funneling)是一款安全网络通信工具.它可以将多个管道的数据通过一个TLS连接转发给远程计算机,从而实现数据的安全传输.它不仅支持TC ...

  6. 【bitset】poj2443 Set Operation

    模板题.S[i][j]表示i是否存在于第j个集合里.妈蛋poj差点打成poi(波兰无关)是不是没救了. #include<cstdio> #include<bitset> us ...

  7. 【字符串哈希】【BKDRhash】【Rabin-Karp算法】模板

    #include<cstdio> #include<iostream> #include<cstring> #include<string> #incl ...

  8. winform如何保持TreeView节点展开和折叠的状态

    转载:http://blog.sina.com.cn/s/blog_6abcacf5010138q5.html private Hashtable NodesStatus = new Hashtabl ...

  9. codeforces round #264(div2)

    A题   我仅仅想说题意非常坑,一不小心就会wa,哎,不机智的我居然在最后判题的过程中错了,少加一个推断语句. 错的值了,你说呢? #include<map> #include<cm ...

  10. Centos7.4 建站系统和软件版本搭配

    一.系统和软件版本搭配 版本: 1.1.2 类型: 建站系统 适用于: Centos7.4 64bit 集成软件版本: nginx_versi=1.12.2 PHP=7.1.13 (已提供提供Zend ...