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. hdu2825(AC 自动机)

    hdu2825 题意 给出一些字符串,要求构造一个长度为 \(n\) 的字符串至少包括其中的 \(k\) 个,问有多少种字符串满足条件. 分析 AC自动机 构造状态转移,然后 状态压缩DP 即可. \ ...

  2. 网页截图工具CutyCapt

    网页截图工具CutyCapt   CuteCapt是Kali Linux提供的一款网页截图工具.该工具运行在命令行中,可以将WebKit引擎解析的网页保存为图片.它保存的文件支持矢量图和位图两大类型, ...

  3. 设置参数进行java的jvm监控

    1.设置jconsole监控服务器的tomcat参数及java jvm大小,执行命令vi ../tomcat/bin/catalina.sh  “在服务器安装的tomcat目录下” JAVA_OPTS ...

  4. [BZOJ5006][LOJ#2290][THUWC2017]随机二分图(概率+状压DP)

    https://loj.ac/problem/2290 题解:https://blog.csdn.net/Vectorxj/article/details/78905660 不是很好理解,对于边(x1 ...

  5. UML及其StarUML介绍

    http://blog.csdn.net/monkey_d_meng/article/details/6005764 http://www.uml.org.cn/oobject/200901203.a ...

  6. http://blog.csdn.net/tkwxty/article/details/34474501

    http://blog.csdn.net/tkwxty/article/details/34474501

  7. Java笔记9:Spring简单Demo

      1 下载spring-framework-3.0.5.RELEASE-with-docs.zip和spring-framework-3.0.5.RELEASE-dependencies.zip,放 ...

  8. Hadoop+Spark+Hbase部署整合篇

    之前的几篇博客中记录的Hadoop.Spark和Hbase部署过程虽然看起来是没多大问题,但是之后在上面跑任务的时候出现了各种各样的配置问题.庆幸有将问题记录下来,可以整理出这篇部署整合篇. 确保集群 ...

  9. easyUI表头样式

    easyUI表头样式 学习了:https://blog.csdn.net/lucasli2016/article/details/53606609 easyUI的样式定义在easyui.css中 表头 ...

  10. WRTNode(MT7620n)USB启动总结

    一.改动mt7620.dtsi,去掉默认的bootargs,kernel_menuconfig取消buildin的command line 二.kernel_menuconfig增加scsi驱动.US ...