Skip to content

Commit

Permalink
Do not trim all uploaded images
Browse files Browse the repository at this point in the history
  • Loading branch information
thaapasa committed Mar 3, 2024
1 parent e990fd5 commit 79cb7e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/server/content/ImageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface ImageManagerOptions {
type StringKey<V> = keyof V & string;

interface SaveImageOptions {
trim?: boolean;
margin?: number;
fit?: keyof sharp.FitEnum;
}
Expand Down Expand Up @@ -97,17 +98,19 @@ export class ImageManager<V extends Record<string, ImageSpecData>> {
private async saveImage<K extends StringKey<V>>(
variant: K,
file: FileUploadResult,
{ margin, fit }: SaveImageOptions = {},
{ margin, fit, trim }: SaveImageOptions = {},
): Promise<FileWriteResult> {
const info = this.getVariant(variant, file.filename, '.webp');
let imgTransform = sharp(file.filepath)
.trim()
.resize({
width: info.width,
height: info.height,
fit: fit ?? 'contain',
background: 'transparent',
});
let imgTransform = sharp(file.filepath);
if (trim) {
imgTransform = imgTransform.trim();
}
imgTransform = imgTransform.resize({
width: info.width,
height: info.height,
fit: fit ?? 'contain',
background: 'transparent',
});
if (margin) {
const marginPx = margin * info.scale;
logger.debug(`Adding ${marginPx}px margin to image`);
Expand Down
2 changes: 1 addition & 1 deletion src/server/data/ShortcutService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export async function uploadShortcutIcon(
try {
await getShortcutById(tx, groupId, userId, shortcutId);
logger.info(image, `Updating shortcut icon for user ${userId}, shortcut ${shortcutId}`);
const file = await shortcutImageHandler.saveImages(image, { margin });
const file = await shortcutImageHandler.saveImages(image, { margin, trim: true });
await deleteShortcutIcon(tx, groupId, userId, shortcutId);
await setShortcutIconById(tx, shortcutId, file);
return getShortcutById(tx, groupId, userId, shortcutId);
Expand Down

0 comments on commit 79cb7e5

Please sign in to comment.