Skip to content

Commit

Permalink
图片压缩
Browse files Browse the repository at this point in the history
  • Loading branch information
caozhihui4399 committed Apr 15, 2017
1 parent 1a122ae commit bb6b8ac
Show file tree
Hide file tree
Showing 42 changed files with 409 additions and 1,932 deletions.
2,205 changes: 274 additions & 1,931 deletions .idea/workspace.xml

Large diffs are not rendered by default.

117 changes: 117 additions & 0 deletions imageInfo/Sprite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const fs = require('fs');
const images = require('images');


/*
* 原理 将两张图片合成一张
*
* */
let option = {
dirPath : './images/',
outputPath:'./images/',
outputName : 'sprites.png',
spacing : 10,
};

function Sprite(option) {

this.option = option;

this.init();
}


Sprite.prototype.init = function(){

this.imagesAry = this.filterImages(this.getAllImagesAry(this.option.dirPath)); // 获取图片并过滤不是图片的资源

this.createSprites();

};

Sprite.prototype.createSprites = function(){

this.createFirstSprites(); // 创建第一张雪碧图


};

Sprite.prototype.synthetic = function(){ //将两张图片合成一张

};

Sprite.prototype.createFirstSprites = function(){ //创建第一张雪碧图

let firstImage = this.imagesAry.pop();

let imagePath = this.getImagePath(firstImage);

let {w,h} = this.getImageSize(imagePath);

let outputPath = this.option.outputPath + this.option.outputName;
console.log(imagePath);
images(w + this.option.spacing,h + this.option.spacing)
.draw(images(imagePath),0,0)
.save(outputPath)


};

Sprite.prototype.createEmptyImage = function(w,h){ //创建空白图层

let path = this.option.outputPath + this.option.outputName;

images(w,h).save(path);

};

Sprite.prototype.getImagePath = function(imageName){
/* 这个 要加路径判断 判断最后路径 有没有 / */

return this.option.dirPath + imageName;
};

Sprite.prototype.getImageSize = function(dirPath){

let size = images(dirPath);

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

};

Sprite.prototype.getAllImagesAry = function(dirPath){

let result = [];

try {
result = fs.readdirSync(dirPath);
}catch (e){
console.log(e);
result = [];
}

return result;
};

Sprite.prototype.filterImages = function(imagesAry){

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

let newAry = [];

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

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

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

return newAry;

};



var sprite = new Sprite(option);
6 changes: 6 additions & 0 deletions imageInfo/imageOrganic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const fs = require('fs');
const images = require('images');

images(1000,1000)
.draw(images("./images/logo.png"), 80, 80)
.save('outer.png');
Binary file added imageInfo/images/baaner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/btn-download.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/btn-feelbad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/btn-go.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/btn-lottery.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/button-拷贝.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/close.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/done.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/footer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/notify.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/step_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/step_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/step_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/title.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/vedio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageInfo/images/xx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion imageInfo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ DealImages.prototype.readAllImages = function(){

DealImages.prototype.filterImages = function(data){

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

let newAry = [];

Expand Down
Binary file added imageOrganic/done.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageOrganic/images/baaner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageOrganic/images/btn-download.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageOrganic/images/btn-feelbad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageOrganic/images/btn-go.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageOrganic/images/btn-lottery.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageOrganic/images/button-拷贝.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imageOrganic/images/close.png
Binary file added imageOrganic/images/done.png
Binary file added imageOrganic/images/footer.png
Binary file added imageOrganic/images/logo.png
Binary file added imageOrganic/images/notify.png
Binary file added imageOrganic/images/step_1.png
Binary file added imageOrganic/images/step_2.png
Binary file added imageOrganic/images/step_3.png
Binary file added imageOrganic/images/test.png
Binary file added imageOrganic/images/title.png
Binary file added imageOrganic/images/vedio.png
Binary file added imageOrganic/images/xx.png
11 changes: 11 additions & 0 deletions imageOrganic/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs');
const gm = require('gm');
const path = require('path');
var imageMagick = gm.subClass({ imageMagick: true });

gm('done.png')
.size(function (err, size) {
console.log(arguments);
if (!err)
console.log(size.width > size.height ? 'wider' : 'taller than you');
});

0 comments on commit bb6b8ac

Please sign in to comment.