Support HEIC and HEIF avatar uploads with in-browser conversion (#13577)

* feat: Support HEIC avatar uploads with in-browser conversion

HEIC and HEIF images picked for an avatar are now converted to JPEG in
the browser before the cropper opens. The browser's own decoder is used
when it supports the format, otherwise a WebAssembly decoder is loaded
on demand.

Also collects the image utilities into an ImageHelper class, fixes the
crop dialog's submit button never disabling, and stops the upload from
forwarding request headers the browser controls itself.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* refactor: Use only the browser's own HEIC decoder

Drops the bundled WebAssembly decoder in favour of the browser's native
support. This removes an LGPL dependency and a 3MB lazy chunk, and lets
the Content Security Policy stay as it was.

Browsers without HEIC support now get a message asking for a JPEG or PNG.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* docs: Add class-level JSDoc to ImageHelper

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tom MoorandClaude Opus 5 authored and GitHub committed 2026-08-28 00:13:56 -04:00
1 parent f49b047648
commit d8d8f2fffe
11 files changed
+244 -164

No files matched your search

@@ -8,7 +8,7 @@ import Input, { LabelText } from "~/components/Input";
import Text from "~/components/Text";
import useStores from "~/hooks/useStores";
import { uploadFile } from "~/utils/files";
import { compressImage } from "~/utils/compressImage";
import { ImageHelper } from "~/utils/ImageHelper";
import { generateEmojiNameFromFilename } from "~/utils/emoji";
import { useEmojiFileUpload, EmojiImageDropZone } from "./Components";
@@ -55,7 +55,7 @@ export function EmojiCreateDialog({ onSubmit }: Props) {
const fileToUpload =
file.type === "image/gif"
? file
: await compressImage(file, {
: await ImageHelper.compress(file, {
maxHeight: 64,
maxWidth: 64,
});
@@ -7,7 +7,7 @@ import Text from "~/components/Text";
import type Emoji from "~/models/Emoji";
import useStores from "~/hooks/useStores";
import { uploadFile } from "~/utils/files";
import { compressImage } from "~/utils/compressImage";
import { ImageHelper } from "~/utils/ImageHelper";
import { useEmojiFileUpload, EmojiImageDropZone } from "./Components";
interface Props {
@@ -38,7 +38,7 @@ export function EmojiReplaceDialog({ emoji, onSubmit }: Props) {
const fileToUpload =
file.type === "image/gif"
? file
: await compressImage(file, {
: await ImageHelper.compress(file, {
maxHeight: 64,
maxWidth: 64,
});
@@ -29,7 +29,7 @@ import Tooltip from "~/components/Tooltip";
import env from "~/env";
import { useMenuAction } from "~/hooks/useMenuAction";
import useStores from "~/hooks/useStores";
import { compressImage } from "~/utils/compressImage";
import { ImageHelper } from "~/utils/ImageHelper";
import { uploadFile } from "~/utils/files";
import {
Popover,
@@ -90,7 +90,7 @@ function ShareSettingsPopover({ share, children }: Props) {
setIsUploading(true);
try {
const compressed = await compressImage(file, {
const compressed = await ImageHelper.compress(file, {
maxHeight: 512,
maxWidth: 512,
});
+38 -27
View File
@@ -14,8 +14,8 @@ import ButtonLarge from "~/components/ButtonLarge";
import Flex from "~/components/Flex";
import LoadingIndicator from "~/components/LoadingIndicator";
import useStores from "~/hooks/useStores";
import { compressImage } from "~/utils/compressImage";
import { uploadFile, dataUrlToBlob } from "~/utils/files";
import { ImageHelper } from "~/utils/ImageHelper";
export type Props = {
onSuccess: (url: string | null) => void | Promise<void>;
@@ -34,13 +34,13 @@ const ImageUpload: React.FC<Props> = ({
const { dialogs } = useStores();
const { t } = useTranslation();
const [isUploading, setIsUploading] = useState(false);
const [isCropping, setIsCropping] = useState(false);
const [isConverting, setIsConverting] = useState(false);
const uploadImage = React.useCallback(
async (blob: Blob, file: File) => {
try {
const compressed = await compressImage(blob, {
const compressed = await ImageHelper.compress(blob, {
maxHeight: 512,
maxWidth: 512,
});
@@ -52,7 +52,6 @@ const ImageUpload: React.FC<Props> = ({
} catch (err) {
onError(errToString(err));
} finally {
setIsUploading(false);
setIsCropping(false);
dialogs.closeAllModals();
}
@@ -62,29 +61,44 @@ const ImageUpload: React.FC<Props> = ({
const handleUpload = React.useCallback(
(blob: Blob, file: File) => {
setIsUploading(true);
// allow the UI to update before converting the canvas to a Blob
// for large images this can cause the page rendering to hang.
setTimeout(() => uploadImage(blob, file), 0);
void uploadImage(blob, file);
},
[uploadImage]
);
const handleClose = React.useCallback(() => {
setIsUploading(false);
setIsCropping(false);
}, []);
const onDropAccepted = React.useCallback(
async (files: File[]) => {
let file = files[0];
// Most browsers cannot display HEIC images, so convert to JPEG before
// the image is shown in the cropper.
if (ImageHelper.isHeic(file)) {
setIsConverting(true);
const converted = await ImageHelper.convertHeicToJpeg(file);
setIsConverting(false);
if (!converted) {
onError(
t(
"Your browser cannot read HEIC images, please convert it to JPEG or PNG first"
)
);
return;
}
file = converted;
}
setIsCropping(true);
dialogs.openModal({
title: "",
content: (
<AvatarEditorDialog
file={files[0]}
file={file}
onUpload={handleUpload}
isUploading={isUploading}
borderRadius={borderRadius ?? 150}
submitText={submitText || t("Crop image")}
/>
@@ -92,19 +106,11 @@ const ImageUpload: React.FC<Props> = ({
onClose: handleClose,
});
},
[
t,
dialogs,
handleUpload,
handleClose,
isUploading,
borderRadius,
submitText,
]
[t, dialogs, handleUpload, handleClose, onError, borderRadius, submitText]
);
const { getRootProps, getInputProps } = useDropzone({
accept: AttachmentValidation.avatarContentTypes.join(", "),
accept: AttachmentValidation.avatarInputContentTypes.join(", "),
onDropAccepted,
});
@@ -114,6 +120,7 @@ const ImageUpload: React.FC<Props> = ({
return (
<div {...getRootProps()}>
{isConverting && <LoadingIndicator />}
<input {...getInputProps()} />
{children}
</div>
@@ -123,23 +130,27 @@ const ImageUpload: React.FC<Props> = ({
type AvatarEditorDialogProps = {
file: File;
onUpload: (blob: Blob, file: File) => void;
isUploading: boolean;
borderRadius: number;
submitText: string;
};
const AvatarEditorDialog: React.FC<AvatarEditorDialogProps> = observer(
({ file, onUpload, isUploading, borderRadius, submitText }) => {
({ file, onUpload, borderRadius, submitText }) => {
const { ui } = useStores();
const { t } = useTranslation();
const [zoom, setZoom] = useState(1);
const [isUploading, setIsUploading] = useState(false);
const avatarEditorRef = useRef<AvatarEditor>(null);
const handleUpload = React.useCallback(() => {
const canvas = avatarEditorRef.current?.getImage();
invariant(canvas, "canvas is not defined");
const blob = dataUrlToBlob(canvas.toDataURL());
onUpload(blob, file);
setIsUploading(true);
// Allow the button to render as disabled before the canvas is converted,
// which hangs rendering for large images.
setTimeout(() => {
const canvas = avatarEditorRef.current?.getImage();
invariant(canvas, "canvas is not defined");
onUpload(dataUrlToBlob(canvas.toDataURL()), file);
}, 0);
}, [file, onUpload]);
const handleZoom = React.useCallback(
+31
View File
@@ -0,0 +1,31 @@
import { ImageHelper } from "./ImageHelper";
const fileWith = (name: string, type: string) => new File([], name, { type });
describe("ImageHelper", () => {
describe("isHeic", () => {
it("matches HEIC and HEIF mime-types", () => {
expect(ImageHelper.isHeic(fileWith("photo", "image/heic"))).toBe(true);
expect(ImageHelper.isHeic(fileWith("photo", "image/heif"))).toBe(true);
expect(ImageHelper.isHeic(fileWith("photo", "image/heic-sequence"))).toBe(
true
);
expect(ImageHelper.isHeic(fileWith("photo", "IMAGE/HEIC"))).toBe(true);
});
it("matches by extension when the mime-type is missing", () => {
expect(ImageHelper.isHeic(fileWith("photo.heic", ""))).toBe(true);
expect(ImageHelper.isHeic(fileWith("photo.HEIF", ""))).toBe(true);
});
it("does not match other images", () => {
expect(ImageHelper.isHeic(fileWith("photo.png", "image/png"))).toBe(
false
);
expect(ImageHelper.isHeic(fileWith("photo.jpg", "image/jpeg"))).toBe(
false
);
expect(ImageHelper.isHeic(fileWith("heic.png", "image/png"))).toBe(false);
});
});
});
+95
View File
@@ -0,0 +1,95 @@
import Compressor from "compressorjs";
/**
* Helpers for inspecting and transforming images in the browser.
*/
export class ImageHelper {
/**
* Checks whether a file is a HEIC or HEIF image. The extension is also
* checked because some platforms report an empty mime-type for these files.
*
* @param file the file to check.
* @returns true if the file is a HEIC or HEIF image.
*/
public static isHeic(file: File): boolean {
const contentTypes = [
"image/heic",
"image/heif",
"image/heic-sequence",
"image/heif-sequence",
];
const extensions = [".heic", ".heif"];
return (
contentTypes.includes(file.type.toLowerCase()) ||
extensions.some((extension) =>
file.name.toLowerCase().endsWith(extension)
)
);
}
/**
* Compresses an image file or blob, optionally constraining its dimensions.
*
* @param file the image file or blob to compress.
* @param options optional compression options, the result is returned by the
* promise rather than the success and error callbacks.
* @returns a promise resolving to the compressed image blob.
*/
public static compress(
file: File | Blob,
options?: Omit<Compressor.Options, "success" | "error">
): Promise<Blob> {
return new Promise((resolve, reject) => {
new Compressor(file, { ...options, success: resolve, error: reject });
});
}
/**
* Converts a HEIC or HEIF image to a JPEG that all browsers can display,
* using the browser's own decoder. Support is limited, so callers must
* handle an undefined result.
*
* @param file the HEIC or HEIF file to convert.
* @returns a promise resolving to the converted JPEG file, or undefined if
* the browser cannot decode the format.
*/
public static async convertHeicToJpeg(file: File): Promise<File | undefined> {
if (typeof createImageBitmap === "undefined") {
return undefined;
}
let bitmap: ImageBitmap;
try {
bitmap = await createImageBitmap(file, {
imageOrientation: "from-image",
});
} catch (_err) {
return undefined;
}
try {
const canvas = document.createElement("canvas");
canvas.width = bitmap.width;
canvas.height = bitmap.height;
const context = canvas.getContext("2d");
if (!context) {
return undefined;
}
context.drawImage(bitmap, 0, 0);
const blob = await new Promise<Blob | null>((resolve) => {
canvas.toBlob(resolve, "image/jpeg", 0.92);
});
if (!blob) {
return undefined;
}
const name = `${file.name.replace(/\.(heic|heif)$/i, "")}.jpg`;
return new File([blob], name, { type: "image/jpeg" });
} finally {
bitmap.close();
}
}
}
-21
View File
@@ -1,21 +0,0 @@
import Compressor from "compressorjs";
type Options = {
maxWidth?: number;
maxHeight?: number;
};
/**
* Compresses an image file or blob, optionally constraining its dimensions.
*
* @param file the image file or blob to compress.
* @param options optional maximum width and height constraints.
* @returns a promise resolving to the compressed image blob.
*/
export const compressImage = async (
file: File | Blob,
options?: Options
): Promise<Blob> =>
new Promise((resolve, reject) => {
new Compressor(file, { ...options, success: resolve, error: reject });
});
+51
View File
@@ -101,6 +101,9 @@ export const uploadFile = async (
for (const [key, value] of Object.entries(
data.headers as Record<string, string>
)) {
if (isForbiddenRequestHeader(key)) {
continue;
}
xhr.setRequestHeader(key, value);
}
}
@@ -145,6 +148,54 @@ export const uploadFile = async (
export { dataUrlToBlob };
/**
* Header names the browser controls itself - it silently ignores an attempt to
* set one and logs an error to the console.
*
* @see https://fetch.spec.whatwg.org/#forbidden-request-header
*/
const forbiddenRequestHeaders = [
"accept-charset",
"accept-encoding",
"access-control-request-headers",
"access-control-request-method",
"connection",
"content-length",
"cookie",
"cookie2",
"date",
"dnt",
"expect",
"host",
"keep-alive",
"origin",
"referer",
"set-cookie",
"te",
"trailer",
"transfer-encoding",
"upgrade",
"via",
];
const forbiddenRequestHeaderPrefixes = ["proxy-", "sec-"];
/**
* Checks whether the browser refuses to let a header be set on a request.
*
* @param name the header name to check.
* @returns true if the header cannot be set.
*/
const isForbiddenRequestHeader = (name: string): boolean => {
const lowercased = name.toLowerCase();
return (
forbiddenRequestHeaders.includes(lowercased) ||
forbiddenRequestHeaderPrefixes.some((prefix) =>
lowercased.startsWith(prefix)
)
);
};
const CHAR_FORWARD_SLASH = 47; /* / */
const CHAR_DOT = 46; /* . */
@@ -1325,6 +1325,7 @@
"Synced <2></2>": "Synced <2></2>",
"Date created": "Date created",
"Crop Image": "Crop Image",
"Your browser cannot read HEIC images, please convert it to JPEG or PNG first": "Your browser cannot read HEIC images, please convert it to JPEG or PNG first",
"Crop image": "Crop image",
"You can import a zip file that was previously exported from the JSON option in another instance. In {{ appName }}, open <em>Export</em> in the Settings sidebar and click on <em>Export Data</em>.": "You can import a zip file that was previously exported from the JSON option in another instance. In {{ appName }}, open <em>Export</em> in the Settings sidebar and click on <em>Export Data</em>.",
"Drag and drop the zip file from the JSON export option in {{appName}}, or click to upload": "Drag and drop the zip file from the JSON export option in {{appName}}, or click to upload",
+16
View File
@@ -2,6 +2,22 @@ export const AttachmentValidation = {
/** The limited allowable mime-types for user and team avatars */
avatarContentTypes: ["image/jpg", "image/jpeg", "image/png"],
/**
* The mime-types and file extensions offered by the avatar file picker. HEIC
* and HEIF images are converted to a type in `avatarContentTypes` by the
* client before they are uploaded. Extensions are included because some
* platforms report an empty mime-type for these files.
*/
avatarInputContentTypes: [
"image/jpg",
"image/jpeg",
"image/png",
"image/heic",
"image/heif",
".heic",
".heif",
],
/** The most widely supported mime-types across modern browsers */
emojiContentTypes: [
"image/png",
+6 -110
View File
@@ -3293,23 +3293,7 @@ __metadata:
languageName: node
linkType: hard
"@octokit/auth-app@npm:^8.1.2":
version: 8.2.0
resolution: "@octokit/auth-app@npm:8.2.0"
dependencies:
"@octokit/auth-oauth-app": "npm:^9.0.3"
"@octokit/auth-oauth-user": "npm:^6.0.2"
"@octokit/request": "npm:^10.0.6"
"@octokit/request-error": "npm:^7.0.2"
"@octokit/types": "npm:^16.0.0"
toad-cache: "npm:^3.7.0"
universal-github-app-jwt: "npm:^2.2.0"
universal-user-agent: "npm:^7.0.0"
checksum: 10c0/7f1b113c9f2ef4d0aac06ffb0b1930a256c6a8985744c709c36fe8c4565c5c184de742de4c4a52e4d9f25baef00c9b855732cfaeb81db8abe238d6c2ee54d6d6
languageName: node
linkType: hard
"@octokit/auth-app@npm:^8.3.0":
"@octokit/auth-app@npm:^8.1.2, @octokit/auth-app@npm:^8.3.0":
version: 8.3.0
resolution: "@octokit/auth-app@npm:8.3.0"
dependencies:
@@ -3325,20 +3309,7 @@ __metadata:
languageName: node
linkType: hard
"@octokit/auth-oauth-app@npm:^9.0.2, @octokit/auth-oauth-app@npm:^9.0.3":
version: 9.0.3
resolution: "@octokit/auth-oauth-app@npm:9.0.3"
dependencies:
"@octokit/auth-oauth-device": "npm:^8.0.3"
"@octokit/auth-oauth-user": "npm:^6.0.2"
"@octokit/request": "npm:^10.0.6"
"@octokit/types": "npm:^16.0.0"
universal-user-agent: "npm:^7.0.0"
checksum: 10c0/1b47dc2a6ea480c04cc3b03d03f4836916ef666047b639c78134d49bbf9fb112a0610021aa017abaa30d8a63a5e66117b3d2b3a4d3976d10153bd5efef54da8b
languageName: node
linkType: hard
"@octokit/auth-oauth-app@npm:^9.0.4":
"@octokit/auth-oauth-app@npm:^9.0.2, @octokit/auth-oauth-app@npm:^9.0.4":
version: 9.0.4
resolution: "@octokit/auth-oauth-app@npm:9.0.4"
dependencies:
@@ -3351,18 +3322,6 @@ __metadata:
languageName: node
linkType: hard
"@octokit/auth-oauth-device@npm:^8.0.3":
version: 8.0.3
resolution: "@octokit/auth-oauth-device@npm:8.0.3"
dependencies:
"@octokit/oauth-methods": "npm:^6.0.2"
"@octokit/request": "npm:^10.0.6"
"@octokit/types": "npm:^16.0.0"
universal-user-agent: "npm:^7.0.0"
checksum: 10c0/2c43d131fbd0b60abef5f37e6168e31f5ec1b8361bac1d71c19499e1960e5e00957c54db9836d88754b467ad48c4e15f60409b0d66dc740468c49834e787e971
languageName: node
linkType: hard
"@octokit/auth-oauth-device@npm:^8.0.4":
version: 8.0.4
resolution: "@octokit/auth-oauth-device@npm:8.0.4"
@@ -3375,20 +3334,7 @@ __metadata:
languageName: node
linkType: hard
"@octokit/auth-oauth-user@npm:^6.0.1, @octokit/auth-oauth-user@npm:^6.0.2":
version: 6.0.2
resolution: "@octokit/auth-oauth-user@npm:6.0.2"
dependencies:
"@octokit/auth-oauth-device": "npm:^8.0.3"
"@octokit/oauth-methods": "npm:^6.0.2"
"@octokit/request": "npm:^10.0.6"
"@octokit/types": "npm:^16.0.0"
universal-user-agent: "npm:^7.0.0"
checksum: 10c0/7af66189e7c0b19c456462d571f61ac787d8464554375b12ded245d0d209a7a0f873f9189d7a3e9afef8aa59eb268c573f8e98b0ee683ebf0989625c20e2125a
languageName: node
linkType: hard
"@octokit/auth-oauth-user@npm:^6.0.3":
"@octokit/auth-oauth-user@npm:^6.0.1, @octokit/auth-oauth-user@npm:^6.0.3":
version: 6.0.3
resolution: "@octokit/auth-oauth-user@npm:6.0.3"
dependencies:
@@ -3477,19 +3423,7 @@ __metadata:
languageName: node
linkType: hard
"@octokit/oauth-methods@npm:^6.0.1, @octokit/oauth-methods@npm:^6.0.2":
version: 6.0.2
resolution: "@octokit/oauth-methods@npm:6.0.2"
dependencies:
"@octokit/oauth-authorization-url": "npm:^8.0.0"
"@octokit/request": "npm:^10.0.6"
"@octokit/request-error": "npm:^7.0.2"
"@octokit/types": "npm:^16.0.0"
checksum: 10c0/51d2f3218839ce37609e6e0adc274efcc198bdcae57ef3ecf3ada95f6f30dcc3052f6db34e4aa3a1bc94545d7025baa44058d5c044fdd6bdd83562eb9b8510c2
languageName: node
linkType: hard
"@octokit/oauth-methods@npm:^6.0.3":
"@octokit/oauth-methods@npm:^6.0.1, @octokit/oauth-methods@npm:^6.0.3":
version: 6.0.4
resolution: "@octokit/oauth-methods@npm:6.0.4"
dependencies:
@@ -3578,16 +3512,7 @@ __metadata:
languageName: node
linkType: hard
"@octokit/request-error@npm:^7.0.0, @octokit/request-error@npm:^7.0.2":
version: 7.1.0
resolution: "@octokit/request-error@npm:7.1.0"
dependencies:
"@octokit/types": "npm:^16.0.0"
checksum: 10c0/62b90a54545c36a30b5ffdda42e302c751be184d85b68ffc7f1242c51d7ca54dbd185b7d0027b491991776923a910c85c9c51269fe0d86111bac187507a5abc4
languageName: node
linkType: hard
"@octokit/request-error@npm:^7.1.1":
"@octokit/request-error@npm:^7.0.0, @octokit/request-error@npm:^7.0.2, @octokit/request-error@npm:^7.1.1":
version: 7.1.1
resolution: "@octokit/request-error@npm:7.1.1"
dependencies:
@@ -3596,7 +3521,7 @@ __metadata:
languageName: node
linkType: hard
"@octokit/request@npm:^10.0.13":
"@octokit/request@npm:^10.0.13, @octokit/request@npm:^10.0.6":
version: 10.0.15
resolution: "@octokit/request@npm:10.0.15"
dependencies:
@@ -3610,21 +3535,6 @@ __metadata:
languageName: node
linkType: hard
"@octokit/request@npm:^10.0.6":
version: 10.0.9
resolution: "@octokit/request@npm:10.0.9"
dependencies:
"@octokit/endpoint": "npm:^11.0.3"
"@octokit/request-error": "npm:^7.0.2"
"@octokit/types": "npm:^16.0.0"
content-type: "npm:^2.0.0"
fast-content-type-parse: "npm:^3.0.0"
json-with-bigint: "npm:^3.5.3"
universal-user-agent: "npm:^7.0.2"
checksum: 10c0/2a642870202117c8cf0021b7c01a92e18acdec4826c103f7749a942537a265251e033c0ac5178807f0c63b9759e149ce9701015454167d4c1627d30218ddf9f2
languageName: node
linkType: hard
"@octokit/types@npm:^16.0.0":
version: 16.0.0
resolution: "@octokit/types@npm:16.0.0"
@@ -11339,13 +11249,6 @@ __metadata:
languageName: node
linkType: hard
"fast-content-type-parse@npm:^3.0.0":
version: 3.0.0
resolution: "fast-content-type-parse@npm:3.0.0"
checksum: 10c0/06251880c83b7118af3a5e66e8bcee60d44f48b39396fc60acc2b4630bd5f3e77552b999b5c8e943d45a818854360e5e97164c374ec4b562b4df96a2cdf2e188
languageName: node
linkType: hard
"fast-deep-equal@npm:^3.1.3":
version: 3.1.3
resolution: "fast-deep-equal@npm:3.1.3"
@@ -13668,13 +13571,6 @@ __metadata:
languageName: node
linkType: hard
"json-with-bigint@npm:^3.5.3":
version: 3.5.8
resolution: "json-with-bigint@npm:3.5.8"
checksum: 10c0/a0c4e37626d74a9a493539f9f9a94855933fa15ea2f028859a787229a42c5f11803db6f94f1ce7b1d89756c1e80a7c1f11006bac266ec7ce819b75701765ca0a
languageName: node
linkType: hard
"json5@npm:^2.2.3":
version: 2.2.3
resolution: "json5@npm:2.2.3"