Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

Disable GIF upload compression

tianma96602b12d93f
diff --git a/apps/web/src/lib/image-compression.ts b/apps/web/src/lib/image-compression.ts
index 48de3ee4..03d60061 100644
--- a/apps/web/src/lib/image-compression.ts
+++ b/apps/web/src/lib/image-compression.ts
@@ -1,14 +1,8 @@
-import type { ParsedFrame } from "gifuct-js";
-import type { WebPAnimationFrame } from "wasm-webp";
-
-const COMPRESSIBLE_IMAGE_TYPES = new Set(["image/png", "image/jpeg", "image/webp", "image/avif", "image/gif"]);
+const COMPRESSIBLE_IMAGE_TYPES = new Set(["image/png", "image/jpeg", "image/webp", "image/avif"]);
const COMPRESSED_IMAGE_TYPE = "image/webp";
const COMPRESSED_IMAGE_EXTENSION = ".webp";
const MAX_COMPRESSED_IMAGE_EDGE = 2560;
const IMAGE_COMPRESSION_QUALITY = 0.82;
-const ANIMATED_IMAGE_COMPRESSION_QUALITY = 76;
-const MAX_ANIMATED_GIF_FRAMES = 320;
-const MIN_ANIMATION_FRAME_DURATION_MS = 20;
export type ImageCompressionResult = {
file: File;
@@ -23,10 +17,6 @@ export const compressImageForUpload = async (file: File): Promise<ImageCompressi
}
try {
- if (file.type === "image/gif") {
- return compressAnimatedGifForUpload(file);
- }
-
const image = await loadImage(file);
const width = image.naturalWidth;
const height = image.naturalHeight;
@@ -68,124 +58,6 @@ export const compressImageForUpload = async (file: File): Promise<ImageCompressi
}
};
-const compressAnimatedGifForUpload = async (file: File): Promise<ImageCompressionResult> => {
- const [{ parseGIF, decompressFrames }, { encodeAnimation }] = await Promise.all([
- import("gifuct-js"),
- import("wasm-webp"),
- ]);
- const parsedGif = parseGIF(await file.arrayBuffer());
- const frames = decompressFrames(parsedGif, true);
-
- if (frames.length === 0 || frames.length > MAX_ANIMATED_GIF_FRAMES) {
- return unchanged(file);
- }
-
- const sourceWidth = parsedGif.lsd.width;
- const sourceHeight = parsedGif.lsd.height;
-
- if (sourceWidth <= 0 || sourceHeight <= 0) {
- return unchanged(file);
- }
-
- const scale = Math.min(1, MAX_COMPRESSED_IMAGE_EDGE / Math.max(sourceWidth, sourceHeight));
- const outputWidth = Math.max(1, Math.round(sourceWidth * scale));
- const outputHeight = Math.max(1, Math.round(sourceHeight * scale));
- const animationFrames = renderGifFramesForWebp(frames, sourceWidth, sourceHeight, outputWidth, outputHeight);
-
- if (animationFrames.length === 0) {
- return unchanged(file);
- }
-
- const data = await encodeAnimation(outputWidth, outputHeight, true, animationFrames);
-
- if (!data || data.byteLength >= file.size) {
- return unchanged(file);
- }
-
- const blob = new Blob([toArrayBuffer(data)], { type: COMPRESSED_IMAGE_TYPE });
-
- return {
- file: new File([blob], toCompressedFilename(file.name), {
- type: COMPRESSED_IMAGE_TYPE,
- lastModified: file.lastModified,
- }),
- compressed: true,
- originalSize: file.size,
- outputSize: blob.size,
- };
-};
-
-const renderGifFramesForWebp = (
- frames: ParsedFrame[],
- sourceWidth: number,
- sourceHeight: number,
- outputWidth: number,
- outputHeight: number
-): WebPAnimationFrame[] => {
- const gifCanvas = document.createElement("canvas");
- gifCanvas.width = sourceWidth;
- gifCanvas.height = sourceHeight;
- const gifContext = gifCanvas.getContext("2d", { alpha: true });
- const patchCanvas = document.createElement("canvas");
- const patchContext = patchCanvas.getContext("2d", { alpha: true });
- const outputCanvas = document.createElement("canvas");
- outputCanvas.width = outputWidth;
- outputCanvas.height = outputHeight;
- const outputContext = outputCanvas.getContext("2d", { alpha: true });
-
- if (!gifContext || !patchContext || !outputContext) {
- return [];
- }
-
- const outputFrames: WebPAnimationFrame[] = [];
-
- for (const frame of frames) {
- const previousFrame =
- frame.disposalType === 3 ? gifContext.getImageData(0, 0, sourceWidth, sourceHeight) : null;
-
- drawGifPatch(frame, patchCanvas, patchContext, gifContext);
-
- outputContext.clearRect(0, 0, outputWidth, outputHeight);
- outputContext.drawImage(gifCanvas, 0, 0, outputWidth, outputHeight);
-
- const imageData = outputContext.getImageData(0, 0, outputWidth, outputHeight);
- outputFrames.push({
- data: new Uint8Array(imageData.data),
- duration: Math.max(frame.delay || 100, MIN_ANIMATION_FRAME_DURATION_MS),
- config: {
- lossless: 0,
- quality: ANIMATED_IMAGE_COMPRESSION_QUALITY,
- },
- });
-
- if (frame.disposalType === 2) {
- gifContext.clearRect(frame.dims.left, frame.dims.top, frame.dims.width, frame.dims.height);
- } else if (previousFrame) {
- gifContext.putImageData(previousFrame, 0, 0);
- }
- }
-
- return outputFrames;
-};
-
-const drawGifPatch = (
- frame: ParsedFrame,
- patchCanvas: HTMLCanvasElement,
- patchContext: CanvasRenderingContext2D,
- gifContext: CanvasRenderingContext2D
-) => {
- const { dims } = frame;
-
- patchCanvas.width = dims.width;
- patchCanvas.height = dims.height;
- patchContext.clearRect(0, 0, dims.width, dims.height);
- patchContext.putImageData(new ImageData(new Uint8ClampedArray(toArrayBuffer(frame.patch)), dims.width, dims.height), 0, 0);
- gifContext.drawImage(patchCanvas, dims.left, dims.top);
-};
-
-const toArrayBuffer = (bytes: Uint8Array | Uint8ClampedArray) =>
- bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer;
-
const unchanged = (file: File): ImageCompressionResult => ({
file,
compressed: false,
diff --git a/bun.lock b/bun.lock
index 1e73d755..3341c1e0 100644
--- a/bun.lock
+++ b/bun.lock
@@ -25,7 +25,6 @@
"clsx": "latest",
"cmdk": "^1.1.1",
"dexie": "latest",
- "gifuct-js": "^2.1.2",
"hono": "latest",
"lucide-react": "latest",
"react": "latest",
@@ -33,7 +32,6 @@
"tailwind-merge": "latest",
"tailwindcss-animate": "^1.0.7",
"vaul": "^1.1.2",
- "wasm-webp": "^0.1.0",
"workbox-window": "^7.4.1",
"zod": "latest",
},
@@ -883,8 +881,6 @@
"get-symbol-description": ["[email protected]", "", { "dependencies": { "call-bound": "^1.0.3", "es-errors": "^1.3.0", "get-intrinsic": "^1.2.6" } }, "sha512-w9UMqWwJxHNOvoNzSJ2oPF5wvYcvP7jUvYzhp67yEhTi17ZDBBC1z9pTdGuzjD+EFIqLSYRweZjqfiPzQ06Ebg=="],
- "gifuct-js": ["[email protected]", "", { "dependencies": { "js-binary-schema-parser": "^2.0.3" } }, "sha512-rI2asw77u0mGgwhV3qA+OEgYqaDn5UNqgs+Bx0FGwSpuqfYn+Ir6RQY5ENNQ8SbIiG/m5gVa7CD5RriO4f4Lsg=="],
-
"glob": ["[email protected]", "", { "dependencies": { "foreground-child": "^3.3.1", "jackspeak": "^4.1.1", "minimatch": "^10.1.1", "minipass": "^7.1.2", "package-json-from-dist": "^1.0.0", "path-scurry": "^2.0.0" }, "bin": { "glob": "dist/esm/bin.mjs" } }, "sha512-vuNwKSaKiqm7g0THUBu2x7ckSs3XJLXE+2ssL7/MfTGPLLcrJQ/4Uq1CjPTtO5cCIiRxqvN6Twy1qOwhL0Xjcw=="],
"glob-parent": ["[email protected]", "", { "dependencies": { "is-glob": "^4.0.3" } }, "sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A=="],
@@ -985,8 +981,6 @@
"jiti": ["[email protected]", "", { "bin": { "jiti": "bin/jiti.js" } }, "sha512-/imKNG4EbWNrVjoNC/1H5/9GFy+tqjGBHCaSsN+P2RnPqjsLmv6UD3Ej+Kj8nBWaRAwyk7kK5ZUc+OEatnTR3A=="],
- "js-binary-schema-parser": ["[email protected]", "", {}, "sha512-xezGJmOb4lk/M1ZZLTR/jaBHQ4gG/lqQnJqdIv4721DMggsa1bDVlHXNeHYogaIEHD9vCRv0fcL4hMA+Coarkg=="],
-
"js-tokens": ["[email protected]", "", {}, "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="],
"jsesc": ["[email protected]", "", { "bin": { "jsesc": "bin/jsesc" } }, "sha512-/sM3dO2FOzXjKQhJuo0Q173wf2KOo8t4I8vHy6lF9poUp7bKT0/NHE8fPX23PwfhnykfqnC2xRxOnVw5XuGIaA=="],
@@ -1339,8 +1333,6 @@
"w3c-keyname": ["[email protected]", "", {}, "sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ=="],
- "wasm-webp": ["[email protected]", "", {}, "sha512-Z1Sop+mLKogzDK3vJzSPfSxRF1yj/zbmn+XJuU9+7fvarxcuGhGkeLBqJr5H8LdgWuAwEKuZelCzx7ujztfhVQ=="],
-
"webidl-conversions": ["[email protected]", "", {}, "sha512-YQ+BmxuTgd6UXZW3+ICGfyqRyHXVlD5GtQr5+qjiNW7bF0cqrzX500HVXPBOvgXb5YnzDd+h0zqyv61KUD7+Sg=="],
"whatwg-url": ["[email protected]", "", { "dependencies": { "lodash.sortby": "^4.7.0", "tr46": "^1.0.1", "webidl-conversions": "^4.0.2" } }, "sha512-WUu7Rg1DroM7oQvGWfOiAK21n74Gg+T4elXEQYkOhtyLeWiJFoOGLXPKI/9gzIie9CtwVLm8wtw6YJdKyxSjeg=="],
diff --git a/package.json b/package.json
index f2c778b9..7bc70400 100644
--- a/package.json
+++ b/package.json
@@ -60,7 +60,6 @@
"clsx": "latest",
"cmdk": "^1.1.1",
"dexie": "latest",
- "gifuct-js": "^2.1.2",
"hono": "latest",
"lucide-react": "latest",
"react": "latest",
@@ -68,7 +67,6 @@
"tailwind-merge": "latest",
"tailwindcss-animate": "^1.0.7",
"vaul": "^1.1.2",
- "wasm-webp": "^0.1.0",
"workbox-window": "^7.4.1",
"zod": "latest"
},