Add a JPEG optimizer

This commit is contained in:
Gaël Métais
2015-05-31 18:39:52 +02:00
parent d7ddae38e5
commit 74eabd700c
11 changed files with 274 additions and 30 deletions
+79
View File
@@ -0,0 +1,79 @@
var should = require('chai').should();
var imageOptimizer = require('../../lib/tools/weightChecker/imageOptimizer');
var fs = require('fs');
var path = require('path');
describe('imageOptimizer', function() {
it('should optimize a JPEG image losslessly', function(done) {
var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/jpeg-image.jpg'));
var fileSize = fileContent.length;
imageOptimizer.compressJpegLosslessly(fileContent).then(function(newFile) {
var newFileSize = newFile.contents.length;
newFileSize.should.be.below(fileSize);
done();
}).fail(function(err) {
done(err);
});
});
it('should optimize a JPEG image lossly', function(done) {
var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/jpeg-image.jpg'));
var fileSize = fileContent.length;
imageOptimizer.compressJpegLossly(fileContent).then(function(newFile) {
var newFileSize = newFile.contents.length;
newFileSize.should.be.below(fileSize);
done();
}).fail(function(err) {
done(err);
});
});
it('should find the best optimization for a jpeg', function(done) {
var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/jpeg-image.jpg'));
var fileSize = fileContent.length;
var entry = {
method: 'GET',
url: 'http://localhost:8388/an-image.jpg',
requestHeaders: {
'User-Agent': 'something',
Referer: 'http://www.google.fr/',
Accept: '*/*',
'Accept-Encoding': 'gzip, deflate'
},
status: 200,
isImage: true,
type: 'image',
contentType: 'image/jpeg',
contentLength: 999,
weightCheck: {
body: fileContent,
totalWeight: fileSize + 200,
headersSize: 200,
bodySize: fileSize,
isCompressed: false,
uncompressedSize: fileSize
}
};
imageOptimizer.optimizeImage(entry)
.then(function(newEntry) {
newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
newEntry.weightCheck.should.have.a.property('lossy').that.is.below(newEntry.weightCheck.lossless);
done();
})
.fail(function(err) {
done(err);
});
});
});
-1
View File
@@ -48,7 +48,6 @@ describe('phantomasWrapper', function() {
}
}).then(function(data) {
console.log(data);
done('Error: unwanted success');
}).fail(function(err) {
+20 -7
View File
@@ -1,5 +1,5 @@
var should = require('chai').should();
var weightChecker = require('../../lib/tools/weightChecker');
var weightChecker = require('../../lib/tools/weightChecker/weightChecker');
describe('weightChecker', function() {
@@ -69,8 +69,9 @@ describe('weightChecker', function() {
isJS: true
};
weightChecker.redownloadEntry(entry, function(err, newEntry) {
should.not.exist(err);
weightChecker.redownloadEntry(entry)
.then(function(newEntry) {
newEntry.weightCheck.bodySize.should.equal(93636);
newEntry.weightCheck.uncompressedSize.should.equal(newEntry.weightCheck.bodySize);
@@ -79,6 +80,10 @@ describe('weightChecker', function() {
newEntry.weightCheck.body.should.have.string('1.8.3');
done();
})
.fail(function(err) {
done(err);
});
});
@@ -96,12 +101,16 @@ describe('weightChecker', function() {
contentLength: 999
};
weightChecker.redownloadEntry(entry, function(err, newEntry) {
should.not.exist(err);
weightChecker.redownloadEntry(entry)
.then(function(errnewEntry) {
newEntry.weightCheck.should.have.a.property('message').that.equals('error while downloading: 404');
done();
})
.fail(function(err) {
done(err);
});
});
@@ -119,12 +128,16 @@ describe('weightChecker', function() {
contentLength: 999
};
weightChecker.redownloadEntry(entry, function(err, newEntry) {
should.not.exist(err);
weightChecker.redownloadEntry(entry)
.then(function(newEntry) {
newEntry.weightCheck.should.have.a.property('message').that.equals('only downloading requests with status code 200');
done();
})
.fail(function(err) {
done(err);
});
});
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 115 KiB