mirror of
https://github.com/xuthus83/pigallery2.git
synced 2024-11-03 21:04:03 +08:00
simplfying photoworker
This commit is contained in:
parent
74e4a8160b
commit
d606df9479
@ -3,9 +3,13 @@ import {Metadata, Sharp} from 'sharp';
|
|||||||
import {Logger} from '../../Logger';
|
import {Logger} from '../../Logger';
|
||||||
import {FfmpegCommand, FfprobeData} from 'fluent-ffmpeg';
|
import {FfmpegCommand, FfprobeData} from 'fluent-ffmpeg';
|
||||||
import {FFmpegFactory} from '../FFmpegFactory';
|
import {FFmpegFactory} from '../FFmpegFactory';
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const sharp = require('sharp');
|
||||||
|
|
||||||
|
sharp.cache(false);
|
||||||
|
|
||||||
export class PhotoWorker {
|
export class PhotoWorker {
|
||||||
private static imageRenderer: (input: RendererInput) => Promise<void> = null;
|
|
||||||
private static videoRenderer: (input: RendererInput) => Promise<void> = null;
|
private static videoRenderer: (input: RendererInput) => Promise<void> = null;
|
||||||
|
|
||||||
public static render(input: RendererInput): Promise<void> {
|
public static render(input: RendererInput): Promise<void> {
|
||||||
@ -19,10 +23,7 @@ export class PhotoWorker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static renderFromImage(input: RendererInput): Promise<void> {
|
public static renderFromImage(input: RendererInput): Promise<void> {
|
||||||
if (PhotoWorker.imageRenderer === null) {
|
return ImageRendererFactory.render(input);
|
||||||
PhotoWorker.imageRenderer = ImageRendererFactory.build();
|
|
||||||
}
|
|
||||||
return PhotoWorker.imageRenderer(input);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static renderFromVideo(input: RendererInput): Promise<void> {
|
public static renderFromVideo(input: RendererInput): Promise<void> {
|
||||||
@ -58,7 +59,6 @@ export interface RendererInput {
|
|||||||
export class VideoRendererFactory {
|
export class VideoRendererFactory {
|
||||||
public static build(): (input: RendererInput) => Promise<void> {
|
public static build(): (input: RendererInput) => Promise<void> {
|
||||||
const ffmpeg = FFmpegFactory.get();
|
const ffmpeg = FFmpegFactory.get();
|
||||||
const path = require('path');
|
|
||||||
return (input: RendererInput): Promise<void> => {
|
return (input: RendererInput): Promise<void> => {
|
||||||
return new Promise((resolve, reject): void => {
|
return new Promise((resolve, reject): void => {
|
||||||
Logger.silly('[FFmpeg] rendering thumbnail: ' + input.mediaPath);
|
Logger.silly('[FFmpeg] rendering thumbnail: ' + input.mediaPath);
|
||||||
@ -121,49 +121,43 @@ export class VideoRendererFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class ImageRendererFactory {
|
export class ImageRendererFactory {
|
||||||
public static build(): (input: RendererInput) => Promise<void> {
|
|
||||||
return ImageRendererFactory.Sharp();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Sharp(): (input: RendererInput) => Promise<void> {
|
public static async render(input: RendererInput): Promise<void> {
|
||||||
const sharp = require('sharp');
|
Logger.silly(
|
||||||
sharp.cache(false);
|
'[SharpRenderer] rendering photo:' +
|
||||||
return async (input: RendererInput): Promise<void> => {
|
input.mediaPath +
|
||||||
Logger.silly(
|
', size:' +
|
||||||
'[SharpRenderer] rendering photo:' +
|
input.size
|
||||||
input.mediaPath +
|
);
|
||||||
', size:' +
|
const image: Sharp = sharp(input.mediaPath, {failOnError: false});
|
||||||
input.size
|
const metadata: Metadata = await image.metadata();
|
||||||
);
|
|
||||||
const image: Sharp = sharp(input.mediaPath, {failOnError: false});
|
|
||||||
const metadata: Metadata = await image.metadata();
|
|
||||||
|
|
||||||
const kernel =
|
const kernel =
|
||||||
input.useLanczos3 === true
|
input.useLanczos3 === true
|
||||||
? sharp.kernel.lanczos3
|
? sharp.kernel.lanczos3
|
||||||
: sharp.kernel.nearest;
|
: sharp.kernel.nearest;
|
||||||
|
|
||||||
if (input.cut) {
|
if (input.cut) {
|
||||||
image.extract(input.cut);
|
image.extract(input.cut);
|
||||||
}
|
}
|
||||||
if (input.makeSquare === false) {
|
if (input.makeSquare === false) {
|
||||||
if (metadata.height > metadata.width) {
|
if (metadata.height > metadata.width) {
|
||||||
image.resize(Math.min(input.size, metadata.width), null, {
|
image.resize(Math.min(input.size, metadata.width), null, {
|
||||||
kernel,
|
kernel,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
image.resize(null, Math.min(input.size, metadata.height), {
|
image.resize(null, Math.min(input.size, metadata.height), {
|
||||||
kernel,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
image.resize(input.size, input.size, {
|
|
||||||
kernel,
|
kernel,
|
||||||
position: sharp.gravity.centre,
|
|
||||||
fit: 'cover',
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
await image.rotate().webp({effort: 6, quality: input.quality, smartSubsample: input.smartSubsample}).toFile(input.outPath);
|
} else {
|
||||||
};
|
image.resize(input.size, input.size, {
|
||||||
|
kernel,
|
||||||
|
position: sharp.gravity.centre,
|
||||||
|
fit: 'cover',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
await image.rotate().webp({effort: 6, quality: input.quality, smartSubsample: input.smartSubsample}).toFile(input.outPath);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user