Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

Avoid upscaling web and desktop upload thumbnails

Neeraj Gupta8c58a7d982fd
diff --git a/desktop/src/main/services/image.ts b/desktop/src/main/services/image.ts
index 4d33a6fb52..5a35ca42dc 100644
--- a/desktop/src/main/services/image.ts
+++ b/desktop/src/main/services/image.ts
@@ -70,17 +70,22 @@ export const generateImageThumbnail = async (
const outputFilePath = await makeTempFilePath("jpeg");
- let quality = 70;
- let command = generateImageThumbnailCommand(
- inputFilePath,
- outputFilePath,
- maxDimension,
- quality,
- );
-
try {
await writeToTemporaryInputFile();
+ const shouldResize = await shouldResizeImage(
+ inputFilePath,
+ maxDimension,
+ );
+ let quality = 70;
+ let command = generateImageThumbnailCommand(
+ inputFilePath,
+ outputFilePath,
+ maxDimension,
+ quality,
+ shouldResize,
+ );
+
let thumbnail: Uint8Array<ArrayBuffer>;
do {
await execAsync(command);
@@ -91,6 +96,7 @@ export const generateImageThumbnail = async (
outputFilePath,
maxDimension,
quality,
+ shouldResize,
);
} while (thumbnail.length > maxSize && quality > 50);
return thumbnail;
@@ -106,6 +112,7 @@ const generateImageThumbnailCommand = (
outputFilePath: string,
maxDimension: number,
quality: number,
+ shouldResize: boolean,
) => {
switch (process.platform) {
case "darwin":
@@ -117,8 +124,7 @@ const generateImageThumbnailCommand = (
"-s",
"formatOptions",
`${quality}`,
- "-Z",
- `${maxDimension}`,
+ ...(shouldResize ? ["-Z", `${maxDimension}`] : []),
inputFilePath,
"--out",
outputFilePath,
@@ -132,9 +138,40 @@ const generateImageThumbnailCommand = (
inputFilePath,
`${outputFilePath}[Q=${quality}]`,
`${maxDimension}`,
+ "--size",
+ "down",
];
default:
throw new Error("Not available on the current OS/arch");
}
};
+
+const shouldResizeImage = async (
+ inputFilePath: string,
+ maxDimension: number,
+) => {
+ if (process.platform != "darwin") return true;
+
+ const { stdout } = await execAsync([
+ "sips",
+ "-g",
+ "pixelWidth",
+ "-g",
+ "pixelHeight",
+ inputFilePath,
+ ]);
+ const width = parseSipsDimension(stdout, "pixelWidth");
+ const height = parseSipsDimension(stdout, "pixelHeight");
+ return Math.max(width, height) > maxDimension;
+};
+
+const parseSipsDimension = (output: string, property: string) => {
+ const match = new RegExp(`^\\s*${property}:\\s*(\\d+)\\s*$`, "m").exec(
+ output,
+ );
+ const dimension = match?.[1] ? Number.parseInt(match[1], 10) : 0;
+ if (dimension <= 0)
+ throw new Error(`Could not read ${property} from sips output`);
+ return dimension;
+};
diff --git a/web/apps/photos/tests/image.test.ts b/web/apps/photos/tests/image.test.ts
new file mode 100644
index 0000000000..6462157554
--- /dev/null
+++ b/web/apps/photos/tests/image.test.ts
@@ -0,0 +1,28 @@
+import { scaledImageDimensions } from "ente-media/image";
+import { describe, expect, test } from "vitest";
+
+describe("scaledImageDimensions", () => {
+ test.each([
+ [64, 64, 720, 64, 64],
+ [320, 240, 720, 320, 240],
+ [640, 480, 720, 640, 480],
+ [720, 540, 720, 720, 540],
+ [4000, 3000, 720, 720, 540],
+ [3000, 4000, 720, 540, 720],
+ ])(
+ "scales %d×%d within %d to %d×%d",
+ (width, height, maxDimension, expectedWidth, expectedHeight) => {
+ expect(scaledImageDimensions(width, height, maxDimension)).toEqual({
+ width: expectedWidth,
+ height: expectedHeight,
+ });
+ },
+ );
+
+ test("preserves the invalid zero-dimension sentinel", () => {
+ expect(scaledImageDimensions(0, 64, 720)).toEqual({
+ width: 0,
+ height: 0,
+ });
+ });
+});
diff --git a/web/packages/media/image.ts b/web/packages/media/image.ts
index 7be022ce36..0cff4ed578 100644
--- a/web/packages/media/image.ts
+++ b/web/packages/media/image.ts
@@ -6,7 +6,7 @@ export const scaledImageDimensions = (
if (width == 0 || height == 0) return { width: 0, height: 0 };
const widthScaleFactor = maxDimension / width;
const heightScaleFactor = maxDimension / height;
- const scaleFactor = Math.min(widthScaleFactor, heightScaleFactor);
+ const scaleFactor = Math.min(1, widthScaleFactor, heightScaleFactor);
const resizedDimensions = {
width: Math.round(width * scaleFactor),
height: Math.round(height * scaleFactor),