mirror of
https://github.com/bpatrik/pigallery2.git
synced 2024-12-25 02:04:15 +02:00
implementing basic thread pooling for thumbnail generation
This commit is contained in:
parent
827dd42418
commit
f134d74b13
@ -1,6 +1,5 @@
|
||||
///<reference path="customtypings/jimp.d.ts"/>
|
||||
import * as path from "path";
|
||||
import * as Jimp from "jimp";
|
||||
import * as crypto from "crypto";
|
||||
import * as fs from "fs";
|
||||
import {NextFunction, Request, Response} from "express";
|
||||
@ -11,6 +10,38 @@ import {DirectoryDTO} from "../../common/entities/DirectoryDTO";
|
||||
import {ProjectPath} from "../ProjectPath";
|
||||
import {PhotoDTO} from "../../common/entities/PhotoDTO";
|
||||
|
||||
const Pool = require('threads').Pool;
|
||||
const pool = new Pool();
|
||||
|
||||
pool.run(
|
||||
(input: {imagePath: string, size: number, thPath: string}, done) => {
|
||||
|
||||
//generate thumbnail
|
||||
let Jimp = require("jimp");
|
||||
Jimp.read(input.imagePath).then((image) => {
|
||||
/**
|
||||
* newWidth * newHeight = size*size
|
||||
* newHeight/newWidth = height/width
|
||||
*
|
||||
* newHeight = (height/width)*newWidth
|
||||
* newWidth * newWidth = (size*size) / (height/width)
|
||||
*
|
||||
* @type {number}
|
||||
*/
|
||||
let ratio = image.bitmap.height / image.bitmap.width;
|
||||
let newWidth = Math.sqrt((input.size * input.size) / ratio);
|
||||
|
||||
image.resize(newWidth, Jimp.AUTO, Jimp.RESIZE_BEZIER);
|
||||
|
||||
image.quality(60); // set JPEG quality
|
||||
image.write(input.thPath, () => { // save
|
||||
return done();
|
||||
});
|
||||
}).catch(function (err) {
|
||||
return done(new Error(ErrorCodes.GENERAL_ERROR));
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
export class ThumbnailGeneratorMWs {
|
||||
|
||||
@ -93,30 +124,18 @@ export class ThumbnailGeneratorMWs {
|
||||
fs.mkdirSync(ProjectPath.ThumbnailFolder);
|
||||
}
|
||||
|
||||
//generate thumbnail
|
||||
Jimp.read(imagePath).then((image) => {
|
||||
/**
|
||||
* newWidth * newHeight = size*size
|
||||
* newHeight/newWidth = height/width
|
||||
*
|
||||
* newHeight = (height/width)*newWidth
|
||||
* newWidth * newWidth = (size*size) / (height/width)
|
||||
*
|
||||
* @type {number}
|
||||
*/
|
||||
let ratio = image.bitmap.height / image.bitmap.width;
|
||||
let newWidth = Math.sqrt((size * size) / ratio);
|
||||
|
||||
image.resize(newWidth, Jimp.AUTO, Jimp.RESIZE_BEZIER);
|
||||
|
||||
image.quality(60); // set JPEG quality
|
||||
image.write(thPath, () => { // save
|
||||
return next();
|
||||
});
|
||||
}).catch(function (err) {
|
||||
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
||||
//run on other thread
|
||||
pool.send({imagePath: imagePath, size: size, thPath: thPath})
|
||||
.on('done', (out) => {
|
||||
return next(out);
|
||||
}).on('error', (job, error) => {
|
||||
return next(new Error(ErrorCodes.GENERAL_ERROR, error));
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static generateThumbnailName(imagePath: string, size: number): string {
|
||||
|
@ -49,6 +49,7 @@
|
||||
"reflect-metadata": "^0.1.9",
|
||||
"rxjs": "^5.0.2",
|
||||
"systemjs": "0.19.41",
|
||||
"threads": "^0.7.2",
|
||||
"typeorm": "0.0.6",
|
||||
"zone.js": "^0.7.4"
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user