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. React Native之ES5/ES6语法差异对照表

    很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...

  2. Delphi 窗体失踪在最上面的代码

    unit ufrmSysPubMessage; interface uses  Windows, Forms, Messages, Classes, ExtCtrls, Controls, StdCt ...

  3. 【MyEcplise】导入项目报错:Errors running builder 'JavaScript Validator' on project '项目名'. java.lang.ClassCastException

    导入项目报错:Errors running builder 'JavaScript Validator' on project '项目名'. java.lang.ClassCastException ...

  4. Spring的学习(IoC,AOP)等

    下面这个系列是非常好的例子: http://www.yiibai.com/spring/spring-3-hello-world-example.html 正在看,把一些基础夯实. IoC可以从下面一 ...

  5. Python数据整合与数据准备-BigGorilla应用

    一.前言 要应用BigGorilla框架对应数据进行数据的处理与匹配,那么首先要下载Anaconda安装,下载地址:https://www.continuum.io/downloads Anacond ...

  6. Yii2.0源码分析之——控制器文件分析(Controller.php)创建动作、执行动作

    在Yii中,当请求一个Url的时候,首先在application中获取request信息,然后由request通过urlManager解析出route,再在Module中根据route来创建contr ...

  7. 转:JavaWeb学习总结(一) 写得相当不错

    这个系列写的相当不错,浅显易懂,图文并茂 http://www.cnblogs.com/xdp-gacl/tag/JavaWeb%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93 ...

  8. Required MultipartFile parameter 'file' is not present error

    <input type=“file”>  中的name 与id 属性 与  addbanner(@RequestParam("file") MultipartFile ...

  9. Managed Media Aggregation using Rtsp and Rtp

    his article was written almost 2 years ago, it's content may not reflect the latest state of the cod ...

  10. [Functional Programming] Draw Items from One JavaScript Array to Another using a Pair ADT

    We want to be able to pick nine random cards from an array of twelve cards, but can run into problem ...