Replace Jimp by Sharp for image processing (faster + supports webp/avif)
This commit is contained in:
+3
-1
@@ -33,10 +33,11 @@ var yellowLabTools = function(url, options) {
|
||||
|
||||
// If a screenshot saveFunction was provided in the options
|
||||
if (options && typeof options.saveScreenshotFn === 'function') {
|
||||
const screenshotTmpPath = data.params.options.screenshot;
|
||||
debug('Now optimizing screenshot...');
|
||||
|
||||
// TODO: temporarily set all screenshot sizes to 600px, until we find a solution
|
||||
ScreenshotHandler.findAndOptimizeScreenshot(data.params.options.screenshot, 600)
|
||||
ScreenshotHandler.findAndOptimizeScreenshot(screenshotTmpPath, 600)
|
||||
|
||||
.then(function(screenshotBuffer) {
|
||||
debug('Screenshot optimized, now saving...');
|
||||
@@ -50,6 +51,7 @@ var yellowLabTools = function(url, options) {
|
||||
|
||||
// Remove uneeded temp screenshot path
|
||||
delete data.params.options.screenshot;
|
||||
return ScreenshotHandler.deleteTmpFile(screenshotTmpPath);
|
||||
})
|
||||
|
||||
.catch(function(err) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var debug = require('debug')('ylt:screenshotHandlerAgent');
|
||||
var Jimp = require('jimp');
|
||||
var debug = require('debug')('ylt:screenshotHandler');
|
||||
var sharp = require('sharp');
|
||||
var Q = require('q');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
@@ -7,84 +7,11 @@ var path = require('path');
|
||||
|
||||
var screenshotHandler = function() {
|
||||
|
||||
this.findAndOptimizeScreenshot = function(tmpScreenshotPath, width) {
|
||||
var that = this;
|
||||
|
||||
debug('Starting screenshot transformation');
|
||||
|
||||
return this.openImage(tmpScreenshotPath)
|
||||
|
||||
.then(function(image) {
|
||||
that.deleteTmpFile(tmpScreenshotPath);
|
||||
return that.resizeImage(image, width);
|
||||
})
|
||||
|
||||
.then(this.toBuffer);
|
||||
};
|
||||
|
||||
|
||||
this.openImage = function(imagePath) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
Jimp.read(imagePath, function(err, image){
|
||||
if (err) {
|
||||
debug('Could not open imagePath %s', imagePath);
|
||||
debug(err);
|
||||
|
||||
deferred.reject(err);
|
||||
} else {
|
||||
debug('Image correctly open');
|
||||
deferred.resolve(image);
|
||||
}
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
|
||||
this.resizeImage = function(image, newWidth) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
var currentWidth = image.bitmap.width;
|
||||
|
||||
if (currentWidth > 0) {
|
||||
var ratio = newWidth / currentWidth;
|
||||
|
||||
image.scale(ratio, function(err, image){
|
||||
if (err) {
|
||||
debug('Could not resize image');
|
||||
debug(err);
|
||||
|
||||
deferred.reject(err);
|
||||
} else {
|
||||
debug('Image correctly resized');
|
||||
deferred.resolve(image);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
deferred.reject('Could not resize an empty image');
|
||||
}
|
||||
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
|
||||
this.toBuffer = function(image) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
image.quality(85).getBuffer(Jimp.MIME_JPEG, function(err, buffer){
|
||||
if (err) {
|
||||
debug('Could not save image to buffer');
|
||||
debug(err);
|
||||
|
||||
deferred.reject(err);
|
||||
} else {
|
||||
debug('Image correctly transformed to buffer');
|
||||
deferred.resolve(buffer);
|
||||
}
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
this.findAndOptimizeScreenshot = async function(tmpScreenshotPath, width) {
|
||||
return sharp(tmpScreenshotPath)
|
||||
.resize({width: 600})
|
||||
.jpeg({quality: 85})
|
||||
.toBuffer();
|
||||
};
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -45,13 +45,13 @@
|
||||
"is-webp": "1.0.1",
|
||||
"is-woff": "1.0.3",
|
||||
"is-woff2": "1.0.0",
|
||||
"jimp": "0.22.8",
|
||||
"md5": "2.3.0",
|
||||
"meow": "5.0.0",
|
||||
"parse-color": "1.0.0",
|
||||
"phantomas": "2.8.0",
|
||||
"q": "1.5.1",
|
||||
"request": "2.88.2",
|
||||
"sharp": "0.32.3",
|
||||
"ttf2woff2": "5.0.0",
|
||||
"uglify-js": "3.17.4",
|
||||
"woff-tools": "0.1.0"
|
||||
|
||||
Reference in New Issue
Block a user