Skip to content

Commit

Permalink
add imagesStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
caozhihui4399 committed Apr 8, 2017
1 parent ebe91e8 commit 1a122ae
Show file tree
Hide file tree
Showing 85 changed files with 30,061 additions and 234 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.idea
node_modules
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2,220 changes: 1,996 additions & 224 deletions .idea/workspace.xml

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions imageInfo/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

var gulp = require('gulp');
var spriter = require('gulp-css-spriter');

gulp.task('css', function() {
return gulp.src('./css/recharge.css')//比如recharge.css这个样式里面什么都不用改,是你想要合并的图就要引用这个样式。 很重要 注意(recharge.css)这个是我的项目。别傻到家抄我一样的。
.pipe(spriter({
// The path and file name of where we will save the sprite sheet
'spriteSheet': './dist/images/spritesheet.png', //这是雪碧图自动合成的图。 很重要
// Because we don't know where you will end up saving the CSS file at this point in the pipe,
// we need a litle help identifying where it will be.
'pathToSpriteSheetFromCSS': '../images/spritesheet.png' //这是在css引用的图片路径,很重要
}))
.pipe(gulp.dest('./dist/css')); //最后生成出来
});
122 changes: 122 additions & 0 deletions imageInfo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
let fs = require('fs');

let images = require('images');


let option = {
dirPath : '/Users/caozhihui/code/huodong/4399cn/y2017/timeMachine/dev/images/',
outputPath:'/Users/caozhihui/code/huodong/4399cn/y2017/timeMachine/dev/scss/'
};

function DealImages(option){
this.dirPath = option.dirPath;
this.outputPath = option.outputPath;
}

DealImages.prototype.init = function(){
this.readAllImages();
fs.watch(this.dirPath,(eventType,filename)=>{
if(eventType){
this.readAllImages();
}
});
};

DealImages.prototype.readAllImages = function(){
fs.readdir(this.dirPath,(err,data)=>{
if(err){
console.log(err);
return false;
}

this.callback('writeStylesheet',data);
});
};

DealImages.prototype.filterImages = function(data){

let formatAry = ['bmp','png','jpeg','gif']

let newAry = [];

data.forEach((item,index)=>{

let suffix = item.split('.')[1];

if(formatAry.indexOf(suffix) != -1){ // 如果 judge 里面有 说明是图片
newAry.push(item);
}
});

return newAry;

};

DealImages.prototype.getSize = function(filePath){

let size = images(filePath);

return {
width : size.width(),
height : size.height()
}
};

DealImages.prototype.template = function(w,h,fileName,fileNameAll){
return `@mixin ${fileName}(){
width:${w}rem/$base;
height:${h}rem/$base;
background:url(../images/${fileNameAll}) no-repeat;
background-size:100% auto;
}
`;
}

DealImages.prototype.writeStylesheet = function(data){

let imagesAry = this.filterImages(data);

let template = ``;

imagesAry.map((item,index)=>{

let fileName = item.split('.')[0];

let filePath = this.dirPath + item;

let size = this.getSize(filePath);

template += this.template(size.width,size.height,fileName,item);

});

let outputPath = this.outputPath + 'imagesStylesheet.scss';

fs.writeFile(outputPath,template,(err)=>{
if(err){
console.log(err);
return err;
}
});
};


DealImages.prototype.callback = function(strategy,data){
this.strategies(strategy,data) || console.log(strategy + '您回调的策略对象不存在~');
};

DealImages.prototype.strategies = function (strategy,data){

if(this[strategy]){

this[strategy](data);

return true;
}

return false;
};

let deal = new DealImages(option);
deal.init();

15 changes: 15 additions & 0 deletions imageInfo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "imageinfo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "caozhihui",
"license": "ISC",
"devDependencies": {
"gulp-css-spriter": "^0.4.0",
"images": "^3.0.0"
}
}
Loading

0 comments on commit 1a122ae

Please sign in to comment.