mirror of
https://github.com/xuthus83/pigallery2.git
synced 2025-01-14 14:43:17 +08:00
implementing thumbnail generation quality settings
This commit is contained in:
parent
6b16125576
commit
c7658ec106
@ -5,6 +5,7 @@ export interface RendererInput {
|
|||||||
size: number;
|
size: number;
|
||||||
makeSquare: boolean;
|
makeSquare: boolean;
|
||||||
thPath: string;
|
thPath: string;
|
||||||
|
qualityPriority: boolean,
|
||||||
__dirname: string;
|
__dirname: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -13,6 +14,9 @@ export const softwareRenderer = (input: RendererInput, done) => {
|
|||||||
//generate thumbnail
|
//generate thumbnail
|
||||||
const Jimp = require("jimp");
|
const Jimp = require("jimp");
|
||||||
Jimp.read(input.imagePath).then((image) => {
|
Jimp.read(input.imagePath).then((image) => {
|
||||||
|
|
||||||
|
const Logger = require(input.__dirname + "/../../Logger").Logger;
|
||||||
|
Logger.silly("[SoftwareThRenderer] rendering thumbnail:", input.imagePath);
|
||||||
/**
|
/**
|
||||||
* newWidth * newHeight = size*size
|
* newWidth * newHeight = size*size
|
||||||
* newHeight/newWidth = height/width
|
* newHeight/newWidth = height/width
|
||||||
@ -23,12 +27,13 @@ export const softwareRenderer = (input: RendererInput, done) => {
|
|||||||
* @type {number}
|
* @type {number}
|
||||||
*/
|
*/
|
||||||
const ratio = image.bitmap.height / image.bitmap.width;
|
const ratio = image.bitmap.height / image.bitmap.width;
|
||||||
|
const algo = input.qualityPriority == true ? Jimp.RESIZE_BEZIER : Jimp.RESIZE_NEAREST_NEIGHBOR;
|
||||||
if (input.makeSquare == false) {
|
if (input.makeSquare == false) {
|
||||||
let newWidth = Math.sqrt((input.size * input.size) / ratio);
|
let newWidth = Math.sqrt((input.size * input.size) / ratio);
|
||||||
|
|
||||||
image.resize(newWidth, Jimp.AUTO, Jimp.RESIZE_BEZIER);
|
image.resize(newWidth, Jimp.AUTO, algo);
|
||||||
} else {
|
} else {
|
||||||
image.resize(input.size / Math.min(ratio, 1), Jimp.AUTO, Jimp.RESIZE_BEZIER);
|
image.resize(input.size / Math.min(ratio, 1), Jimp.AUTO, algo);
|
||||||
image.crop(0, 0, input.size, input.size);
|
image.crop(0, 0, input.size, input.size);
|
||||||
}
|
}
|
||||||
image.quality(60); // set JPEG quality
|
image.quality(60); // set JPEG quality
|
||||||
@ -51,6 +56,9 @@ export const hardwareRenderer = (input: RendererInput, done) => {
|
|||||||
image
|
image
|
||||||
.metadata()
|
.metadata()
|
||||||
.then((metadata: Metadata) => {
|
.then((metadata: Metadata) => {
|
||||||
|
|
||||||
|
const Logger = require(input.__dirname + "/../../Logger").Logger;
|
||||||
|
Logger.silly("[SoftwareThRenderer] rendering thumbnail:", input.imagePath);
|
||||||
/**
|
/**
|
||||||
* newWidth * newHeight = size*size
|
* newWidth * newHeight = size*size
|
||||||
* newHeight/newWidth = height/width
|
* newHeight/newWidth = height/width
|
||||||
@ -62,13 +70,21 @@ export const hardwareRenderer = (input: RendererInput, done) => {
|
|||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
const ratio = metadata.height / metadata.width;
|
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) {
|
if (input.makeSquare == false) {
|
||||||
const newWidth = Math.round(Math.sqrt((input.size * input.size) / ratio));
|
const newWidth = Math.round(Math.sqrt((input.size * input.size) / ratio));
|
||||||
image.resize(newWidth);
|
image.resize(newWidth, null, {
|
||||||
|
kernel: kernel,
|
||||||
|
interpolator: interpolator
|
||||||
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
image
|
image
|
||||||
.resize(input.size, input.size)
|
.resize(input.size, input.size, {
|
||||||
|
kernel: kernel,
|
||||||
|
interpolator: interpolator
|
||||||
|
})
|
||||||
.crop(sharp.strategy.center);
|
.crop(sharp.strategy.center);
|
||||||
}
|
}
|
||||||
image
|
image
|
||||||
|
@ -139,7 +139,14 @@ export class ThumbnailGeneratorMWs {
|
|||||||
|
|
||||||
this.initPools();
|
this.initPools();
|
||||||
//run on other thread
|
//run on other thread
|
||||||
pool.send({imagePath: imagePath, size: size, thPath: thPath, makeSquare: makeSquare, __dirname: __dirname})
|
pool.send({
|
||||||
|
imagePath: imagePath,
|
||||||
|
size: size,
|
||||||
|
thPath: thPath,
|
||||||
|
makeSquare: makeSquare,
|
||||||
|
qualityPriority: Config.Server.thumbnail.qualityPriority,
|
||||||
|
__dirname: __dirname,
|
||||||
|
})
|
||||||
.on('done', (out) => {
|
.on('done', (out) => {
|
||||||
return next(out);
|
return next(out);
|
||||||
}).on('error', (error) => {
|
}).on('error', (error) => {
|
||||||
|
@ -18,6 +18,7 @@ export interface DataBaseConfig {
|
|||||||
export interface ThumbnailConfig {
|
export interface ThumbnailConfig {
|
||||||
folder: string;
|
folder: string;
|
||||||
hardwareAcceleration: boolean;
|
hardwareAcceleration: boolean;
|
||||||
|
qualityPriority: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ServerConfig {
|
export interface ServerConfig {
|
||||||
|
@ -12,7 +12,8 @@ export class PrivateConfigClass extends PublicConfigClass {
|
|||||||
imagesFolder: "demo/images",
|
imagesFolder: "demo/images",
|
||||||
thumbnail: {
|
thumbnail: {
|
||||||
folder: "demo/TEMP",
|
folder: "demo/TEMP",
|
||||||
hardwareAcceleration: true
|
hardwareAcceleration: true,
|
||||||
|
qualityPriority: true
|
||||||
},
|
},
|
||||||
database: {
|
database: {
|
||||||
type: DatabaseType.mysql,
|
type: DatabaseType.mysql,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user