Skip to content

Build your personal knowledge base with Trilium Notes

client/image compression tool: update default PNG handling option for automatic compression

Adorian Doran7e516ed19c94
diff --git a/apps/client/src/widgets/dialogs/image_compression/image_compression_options.ts b/apps/client/src/widgets/dialogs/image_compression/image_compression_options.ts
index 05b1199474..76c485cd9b 100644
--- a/apps/client/src/widgets/dialogs/image_compression/image_compression_options.ts
+++ b/apps/client/src/widgets/dialogs/image_compression/image_compression_options.ts
@@ -107,8 +107,8 @@ export const CONSERVATIVE_IMAGE_COMPRESSION_DEFAULTS: ImageCompressionDefaults =
};
/**
- * What automatic compression falls back to, which is what it has always done: an arriving JPEG
- * recompressed, an arriving PNG written back as a JPEG.
+ * What automatic compression falls back to: an arriving JPEG recompressed, an arriving PNG made
+ * smaller without ceasing to be one.
*
* Only ever reached by a stored option that cannot be read, since every one of these settings has
* a value from the moment the database is created. It is here so that the unreadable case answers
@@ -117,7 +117,7 @@ export const CONSERVATIVE_IMAGE_COMPRESSION_DEFAULTS: ImageCompressionDefaults =
*/
export const AUTOMATIC_IMAGE_COMPRESSION_DEFAULTS: ImageCompressionDefaults = {
jpegHandling: "compress",
- pngHandling: "jpeg"
+ pngHandling: "optimize"
};
/** The longest edge the first run offers to scale down to: the width of a Full HD screen. */
diff --git a/apps/client/src/widgets/type_widgets/options/media.spec.tsx b/apps/client/src/widgets/type_widgets/options/media.spec.tsx
index c1422a6005..881ac8c78f 100644
--- a/apps/client/src/widgets/type_widgets/options/media.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/media.spec.tsx
@@ -55,7 +55,7 @@ const DEFAULTS: Record<string, string> = {
imageResize: "true",
imageMaxWidthHeight: "2000",
imageJpegHandling: "compress",
- imagePngHandling: "jpeg",
+ imagePngHandling: "optimize",
imageJpegQuality: "75",
imageConversionQuality: "75",
downloadImagesAutomatically: "true"
@@ -106,16 +106,17 @@ describe("the image compression card", () => {
it("hangs the whole group off its switch, nesting what qualifies each choice", () => {
open();
- // Every row the settings can offer, in order: the switch, scaling with its bound, then one
- // exclusive choice per format, each followed by the quality that qualifies it.
+ // What an untouched install shows, in order: the switch, scaling with its bound, then one
+ // exclusive choice per format. Recompressing a JPEG brings a quality with it; optimizing a
+ // PNG does not, there being no quality to reducing it to a palette — so only one of the two
+ // choices carries a nested row here.
expect(rowTitles()).toEqual([
"images.automatic_image_compression",
"space_usage.compress_resize",
"space_usage.compress_max_dimensions",
"space_usage.compress_jpeg_handling",
"space_usage.compress_quality",
- "space_usage.compress_png_handling",
- "space_usage.compress_quality"
+ "space_usage.compress_png_handling"
]);
// Switched off, the settings are not merely greyed out but gone: there is nothing for them
diff --git a/apps/server/src/services/image_compression.spec.ts b/apps/server/src/services/image_compression.spec.ts
index 670301fdb5..8c64f8d8ad 100644
--- a/apps/server/src/services/image_compression.spec.ts
+++ b/apps/server/src/services/image_compression.spec.ts
@@ -59,7 +59,7 @@ afterEach(() => {
imageConversionQuality: "75",
imageResize: "true",
imageJpegHandling: "compress",
- imagePngHandling: "jpeg"
+ imagePngHandling: "optimize"
});
});
@@ -315,7 +315,7 @@ describe("what automatic compression is set to do", () => {
expect(automaticCompressionRequest()).toMatchObject({
maxWidthHeight: 2000,
jpegHandling: "compress",
- pngHandling: "jpeg",
+ pngHandling: "optimize",
quality: 75,
conversionQuality: 75
});
diff --git a/apps/server/src/services/image_provider.spec.ts b/apps/server/src/services/image_provider.spec.ts
index 6b517e0892..429df3743e 100644
--- a/apps/server/src/services/image_provider.spec.ts
+++ b/apps/server/src/services/image_provider.spec.ts
@@ -111,7 +111,7 @@ afterEach(() => {
imageMaxWidthHeight: '2000',
imageResize: 'true',
imageJpegHandling: 'compress',
- imagePngHandling: 'jpeg'
+ imagePngHandling: 'optimize'
});
});
@@ -234,9 +234,10 @@ describe('serverImageProvider.processImage', () => {
const result = await serverImageProvider.processImage(noisyPng, 'wide.png', true);
- // Noisy 600x400 -> resized to width 100, re-encoded as JPEG (smaller).
+ // Noisy 600x400 -> resized to width 100, and written back as the same kind of image:
+ // the default handling optimizes a PNG rather than turning it into a JPEG.
expect(result.buffer.byteLength).toBeLessThan(noisyPng.byteLength);
- expect(result.format).toEqual({ ext: 'jpg', mime: 'image/jpeg' });
+ expect(result.format).toEqual({ ext: 'png', mime: 'image/png' });
});
it('resizes a tall image by height', async () => {
@@ -245,7 +246,7 @@ describe('serverImageProvider.processImage', () => {
// tallPng (200x600) is taller than wide, so the height-resize branch runs.
const result = await serverImageProvider.processImage(tallPng, 'tall.png', true);
- expect(result.format).toEqual({ ext: 'jpg', mime: 'image/jpeg' });
+ expect(result.format).toEqual({ ext: 'png', mime: 'image/png' });
expect(result.buffer.byteLength).toBeLessThan(tallPng.byteLength);
});
@@ -270,9 +271,12 @@ describe('serverImageProvider.processImage', () => {
expect(result.format).toEqual({ ext: 'png', mime: 'image/png' });
});
- /** Shrinks the noisy PNG, which the default handling converts, under the given settings. */
+ /**
+ * Shrinks the noisy PNG under the given settings, converting it — which the default handling
+ * does not do, so the two tests below that are about conversion ask for it by name.
+ */
async function shrunkSize(values: Record<string, string>): Promise<number> {
- setOptions({ imageMaxWidthHeight: '100', ...values });
+ setOptions({ imageMaxWidthHeight: '100', imagePngHandling: 'jpeg', ...values });
const result = await serverImageProvider.processImage(noisyPng, 'wide.png', true);
expect(result.format).toEqual({ ext: 'jpg', mime: 'image/jpeg' });
return result.buffer.byteLength;
diff --git a/packages/trilium-core/src/services/image_compression.ts b/packages/trilium-core/src/services/image_compression.ts
index 2742a75c49..08b74735c0 100644
--- a/packages/trilium-core/src/services/image_compression.ts
+++ b/packages/trilium-core/src/services/image_compression.ts
@@ -209,7 +209,7 @@ export function automaticCompressionRequest(): ImageCompressionRequest {
resize: optionService.getOptionBool("imageResize"),
maxWidthHeight: defaultMaxWidthHeight(),
jpegHandling: storedHandling("imageJpegHandling", IMAGE_JPEG_HANDLINGS, "compress"),
- pngHandling: storedHandling("imagePngHandling", IMAGE_PNG_HANDLINGS, "jpeg"),
+ pngHandling: storedHandling("imagePngHandling", IMAGE_PNG_HANDLINGS, "optimize"),
quality: defaultQuality(),
conversionQuality: storedQuality("imageConversionQuality")
};
diff --git a/packages/trilium-core/src/services/options_init.ts b/packages/trilium-core/src/services/options_init.ts
index f174d10054..5f22c020a2 100644
--- a/packages/trilium-core/src/services/options_init.ts
+++ b/packages/trilium-core/src/services/options_init.ts
@@ -166,13 +166,15 @@ const defaultOptions: DefaultOption[] = [
{ name: "spellCheckLanguageCode", value: "en-US", isSynced: false },
{ name: "imageMaxWidthHeight", value: "2000", isSynced: true },
{ name: "imageJpegQuality", value: "75", isSynced: true },
- // The three below are what automatic compression always did, said out loud rather than left
- // implicit: oversized images scaled down, JPEGs recompressed, PNGs written back as JPEGs. They
- // are defaults for what an install already does, not a recommendation — `optimize` keeps a PNG
- // a PNG, and is the gentler answer for anyone who would rather it did.
+ // What automatic compression does to an arriving image, said out loud rather than left implicit.
+ // Scaling and recompressing are what it always did; the PNG answer is not. It used to turn every
+ // PNG into a JPEG, which is lossy, permanent, and throws away transparency — the only thing it
+ // could do before there was anything else. Optimizing makes a PNG smaller and leaves it a PNG,
+ // which is the right default for a step that runs unattended on everything a user pastes.
+ // Converting is still there for anyone who wants the space more than the format.
{ name: "imageResize", value: "true", isSynced: true },
{ name: "imageJpegHandling", value: "compress", isSynced: true },
- { name: "imagePngHandling", value: "jpeg", isSynced: true },
+ { name: "imagePngHandling", value: "optimize", isSynced: true },
{ name: "imageConversionQuality", value: "75", isSynced: true },
{ name: "autoFixConsistencyIssues", value: "true", isSynced: false },
{ name: "vimKeymapEnabled", value: "false", isSynced: false },