mirror of
https://github.com/xuthus83/pigallery2.git
synced 2025-01-14 14:43:17 +08: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"/>
|
///<reference path="customtypings/jimp.d.ts"/>
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import * as Jimp from "jimp";
|
|
||||||
import * as crypto from "crypto";
|
import * as crypto from "crypto";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import {NextFunction, Request, Response} from "express";
|
import {NextFunction, Request, Response} from "express";
|
||||||
@ -11,6 +10,38 @@ import {DirectoryDTO} from "../../common/entities/DirectoryDTO";
|
|||||||
import {ProjectPath} from "../ProjectPath";
|
import {ProjectPath} from "../ProjectPath";
|
||||||
import {PhotoDTO} from "../../common/entities/PhotoDTO";
|
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 {
|
export class ThumbnailGeneratorMWs {
|
||||||
|
|
||||||
@ -93,30 +124,18 @@ export class ThumbnailGeneratorMWs {
|
|||||||
fs.mkdirSync(ProjectPath.ThumbnailFolder);
|
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);
|
//run on other thread
|
||||||
|
pool.send({imagePath: imagePath, size: size, thPath: thPath})
|
||||||
image.quality(60); // set JPEG quality
|
.on('done', (out) => {
|
||||||
image.write(thPath, () => { // save
|
return next(out);
|
||||||
return next();
|
}).on('error', (job, error) => {
|
||||||
});
|
return next(new Error(ErrorCodes.GENERAL_ERROR, error));
|
||||||
}).catch(function (err) {
|
|
||||||
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static generateThumbnailName(imagePath: string, size: number): string {
|
private static generateThumbnailName(imagePath: string, size: number): string {
|
||||||
|
@ -49,6 +49,7 @@
|
|||||||
"reflect-metadata": "^0.1.9",
|
"reflect-metadata": "^0.1.9",
|
||||||
"rxjs": "^5.0.2",
|
"rxjs": "^5.0.2",
|
||||||
"systemjs": "0.19.41",
|
"systemjs": "0.19.41",
|
||||||
|
"threads": "^0.7.2",
|
||||||
"typeorm": "0.0.6",
|
"typeorm": "0.0.6",
|
||||||
"zone.js": "^0.7.4"
|
"zone.js": "^0.7.4"
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user