2017-05-28 18:33:18 +08:00
|
|
|
import {Metadata, SharpInstance} from "@types/sharp";
|
2017-06-12 05:33:47 +08:00
|
|
|
import {Dimensions, State} from "@types/gm";
|
2017-05-28 18:33:18 +08:00
|
|
|
|
2017-06-12 05:33:47 +08:00
|
|
|
export module ThumbnailRenderers {
|
|
|
|
|
|
|
|
export interface RendererInput {
|
2017-05-28 18:33:18 +08:00
|
|
|
imagePath: string;
|
|
|
|
size: number;
|
|
|
|
makeSquare: boolean;
|
|
|
|
thPath: string;
|
2017-06-04 22:23:20 +08:00
|
|
|
qualityPriority: boolean,
|
2017-05-28 18:33:18 +08:00
|
|
|
__dirname: string;
|
2017-06-12 05:33:47 +08:00
|
|
|
}
|
2017-05-28 18:33:18 +08:00
|
|
|
|
2017-06-12 05:33:47 +08:00
|
|
|
export const jimp = (input: RendererInput, done) => {
|
2017-05-28 18:33:18 +08:00
|
|
|
|
|
|
|
//generate thumbnail
|
|
|
|
const Jimp = require("jimp");
|
|
|
|
Jimp.read(input.imagePath).then((image) => {
|
2017-06-04 22:23:20 +08:00
|
|
|
|
2017-06-12 05:33:47 +08:00
|
|
|
const Logger = require(input.__dirname + "/../../Logger").Logger;
|
|
|
|
Logger.silly("[JimpThRenderer] rendering thumbnail:", input.imagePath);
|
|
|
|
/**
|
|
|
|
* newWidth * newHeight = size*size
|
|
|
|
* newHeight/newWidth = height/width
|
|
|
|
*
|
|
|
|
* newHeight = (height/width)*newWidth
|
|
|
|
* newWidth * newWidth = (size*size) / (height/width)
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
const ratio = image.bitmap.height / image.bitmap.width;
|
|
|
|
const algo = input.qualityPriority == true ? Jimp.RESIZE_BEZIER : Jimp.RESIZE_NEAREST_NEIGHBOR;
|
|
|
|
if (input.makeSquare == false) {
|
|
|
|
let newWidth = Math.sqrt((input.size * input.size) / ratio);
|
|
|
|
|
|
|
|
image.resize(newWidth, Jimp.AUTO, algo);
|
|
|
|
} else {
|
|
|
|
image.resize(input.size / Math.min(ratio, 1), Jimp.AUTO, algo);
|
|
|
|
image.crop(0, 0, input.size, input.size);
|
|
|
|
}
|
|
|
|
image.quality(60); // set JPEG quality
|
|
|
|
image.write(input.thPath, () => { // save
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
}).catch(function (err) {
|
|
|
|
const Error = require(input.__dirname + "/../../../common/entities/Error").Error;
|
|
|
|
const ErrorCodes = require(input.__dirname + "/../../../common/entities/Error").ErrorCodes;
|
|
|
|
return done(new Error(ErrorCodes.GENERAL_ERROR, err));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const sharp = (input: RendererInput, done) => {
|
|
|
|
|
|
|
|
//generate thumbnail
|
|
|
|
const sharp = require("sharp");
|
|
|
|
|
|
|
|
const image: SharpInstance = sharp(input.imagePath);
|
|
|
|
image
|
|
|
|
.metadata()
|
|
|
|
.then((metadata: Metadata) => {
|
|
|
|
|
2017-06-04 22:23:20 +08:00
|
|
|
const Logger = require(input.__dirname + "/../../Logger").Logger;
|
2017-06-12 05:33:47 +08:00
|
|
|
Logger.silly("[SharpThRenderer] rendering thumbnail:", input.imagePath);
|
2017-05-28 18:33:18 +08:00
|
|
|
/**
|
|
|
|
* newWidth * newHeight = size*size
|
|
|
|
* newHeight/newWidth = height/width
|
|
|
|
*
|
|
|
|
* newHeight = (height/width)*newWidth
|
|
|
|
* newWidth * newWidth = (size*size) / (height/width)
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2017-06-12 05:33:47 +08:00
|
|
|
try {
|
|
|
|
const ratio = metadata.height / metadata.width;
|
|
|
|
const kernel = input.qualityPriority == true ? sharp.kernel.lanczos3 : sharp.kernel.nearest;
|
|
|
|
const interpolator = input.qualityPriority == true ? sharp.interpolator.bicubic : sharp.interpolator.nearest;
|
|
|
|
if (input.makeSquare == false) {
|
|
|
|
const newWidth = Math.round(Math.sqrt((input.size * input.size) / ratio));
|
|
|
|
image.resize(newWidth, null, {
|
|
|
|
kernel: kernel,
|
|
|
|
interpolator: interpolator
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
image
|
|
|
|
.resize(input.size, input.size, {
|
|
|
|
kernel: kernel,
|
|
|
|
interpolator: interpolator
|
|
|
|
})
|
|
|
|
.crop(sharp.strategy.center);
|
|
|
|
}
|
|
|
|
image
|
|
|
|
.jpeg()
|
|
|
|
.toFile(input.thPath).then(() => {
|
2017-05-28 18:33:18 +08:00
|
|
|
return done();
|
2017-06-12 05:33:47 +08:00
|
|
|
}).catch(function (err) {
|
|
|
|
const Error = require(input.__dirname + "/../../../common/entities/Error").Error;
|
|
|
|
const ErrorCodes = require(input.__dirname + "/../../../common/entities/Error").ErrorCodes;
|
|
|
|
return done(new Error(ErrorCodes.GENERAL_ERROR, err));
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
const Error = require(input.__dirname + "/../../../common/entities/Error").Error;
|
|
|
|
const ErrorCodes = require(input.__dirname + "/../../../common/entities/Error").ErrorCodes;
|
|
|
|
return done(new Error(ErrorCodes.GENERAL_ERROR, err));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
2017-05-28 18:33:18 +08:00
|
|
|
|
2017-06-12 05:33:47 +08:00
|
|
|
|
|
|
|
export const gm = (input: RendererInput, done) => {
|
2017-05-28 18:33:18 +08:00
|
|
|
|
|
|
|
//generate thumbnail
|
2017-06-12 05:33:47 +08:00
|
|
|
const gm = require("gm");
|
2017-05-28 18:33:18 +08:00
|
|
|
|
2017-06-12 05:33:47 +08:00
|
|
|
let image: State = gm(input.imagePath);
|
2017-05-28 18:33:18 +08:00
|
|
|
image
|
2017-06-12 05:33:47 +08:00
|
|
|
.size((err, value: Dimensions) => {
|
|
|
|
if (err) {
|
|
|
|
const Error = require(input.__dirname + "/../../../common/entities/Error").Error;
|
|
|
|
const ErrorCodes = require(input.__dirname + "/../../../common/entities/Error").ErrorCodes;
|
|
|
|
return done(new Error(ErrorCodes.GENERAL_ERROR, err));
|
|
|
|
}
|
|
|
|
const Logger = require(input.__dirname + "/../../Logger").Logger;
|
|
|
|
Logger.silly("[GMThRenderer] rendering thumbnail:", input.imagePath);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* newWidth * newHeight = size*size
|
|
|
|
* newHeight/newWidth = height/width
|
|
|
|
*
|
|
|
|
* newHeight = (height/width)*newWidth
|
|
|
|
* newWidth * newWidth = (size*size) / (height/width)
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
const ratio = value.height / value.width;
|
|
|
|
const filter = input.qualityPriority == true ? 'Lanczos' : 'Point';
|
|
|
|
image.filter(filter);
|
|
|
|
|
|
|
|
if (input.makeSquare == false) {
|
|
|
|
const newWidth = Math.round(Math.sqrt((input.size * input.size) / ratio));
|
|
|
|
image = image.resize(newWidth);
|
|
|
|
} else {
|
|
|
|
image = image.resize(input.size, input.size)
|
|
|
|
.crop(input.size, input.size);
|
|
|
|
}
|
|
|
|
image.write(input.thPath, (err) => {
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
const Error = require(input.__dirname + "/../../../common/entities/Error").Error;
|
|
|
|
const ErrorCodes = require(input.__dirname + "/../../../common/entities/Error").ErrorCodes;
|
|
|
|
return done(new Error(ErrorCodes.GENERAL_ERROR, err));
|
2017-05-28 18:33:18 +08:00
|
|
|
}
|
2017-06-12 05:33:47 +08:00
|
|
|
return done();
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
const Error = require(input.__dirname + "/../../../common/entities/Error").Error;
|
|
|
|
const ErrorCodes = require(input.__dirname + "/../../../common/entities/Error").ErrorCodes;
|
|
|
|
return done(new Error(ErrorCodes.GENERAL_ERROR, err));
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2017-05-28 18:33:18 +08:00
|
|
|
|
2017-06-12 05:33:47 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
}
|