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. fiddler进行弱网测试

    fiddler模拟限速的原理 弱网测试原帖连接:http://blog.csdn.net/eleven521/article/details/19089671 我们可以通过fiddler来模拟限速,因 ...

  2. [BZOJ5020][THUWC2017]在美妙的数学王国中畅游(LCT)

    5020: [THUWC 2017]在美妙的数学王国中畅游 Time Limit: 80 Sec  Memory Limit: 512 MBSec  Special JudgeSubmit: 323  ...

  3. [Atcoder Regular Contest 063] Tutorial

    Link: ARC063 传送门 C: 将每种颜色的连续出现称为一段,寻找总段数即可 #include <bits/stdc++.h> using namespace std; ,len; ...

  4. CodeForces - 44A Indian Summer

    Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and ...

  5. Problem R: 零起点学算法13——求2个时间之间的分钟数

    #include<stdio.h> int main() { int a1,b1,a2,b2,s; scanf("%d:%d",&a1,&b1); sc ...

  6. 【Python笔记】Python语言基础

    Python是一种解释性(没有编译).交互式.面向对象的语言 1.安装python编译器 版本:Python2.7比较普遍,Python不是向下兼容的软件,因此Python3.x有些东西不好找资料 2 ...

  7. 踩的ssh坑

    先说下事情的起因,手贱删掉了~/.ssh下的所有文件,包括authorized_keys,id_rsa,id_rsa.pub,以及known_hosts.其实,这四个文件本来就是没有的,id_rsa和 ...

  8. 使用jQuery操作DOM(ppt练习)

    <!DOCTYPE html> <html> <head> <title>test3.html</title> <meta http- ...

  9. 【C语言】 Linux下编译提示pow未定义引用

    如下代码: #include <stdio.h> // 调用基本输入输出函数库 #include <math.h> #define PI 3.14 // 定义常量 float ...

  10. Scut游戏服务器免费开源框架--快速开发(3)

    Scut快速开发(3) 1     开发环境 Scut Lib版本:5.2.3.2 需要安装的软件 a)        IIS和消息队列(MSMQ) b)        数据库,Sql2005以上版本 ...