From 4b1ae76baabb4217a2dfbc1a1ec0428251a142e4 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Thu, 19 Nov 2026 08:09:59 -0700 Subject: [PATCH] Support HEIC images for Face Recognition (#25024) --- web/package-lock.json | 7 + web/package.json | 1 + web/public/locales/en/views/faceLibrary.json | 4 +- web/src/components/input/ImageEntry.tsx | 132 +++++++++++++++---- web/src/utils/heicUtil.ts | 44 +++++++ 5 files changed, 161 insertions(+), 27 deletions(-) create mode 100644 web/src/utils/heicUtil.ts diff --git a/web/package-lock.json b/web/package-lock.json index 6eabb5b0da..83601d6005 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -46,6 +46,7 @@ "date-fns": "^4.4.0", "date-fns-tz": "^3.2.0", "framer-motion": "^14.12.0", + "heic-to": "^1.6.7", "hls.js": "^1.7.4", "i18next": "^27.1.1", "i18next-http-backend": "^4.0.10", @@ -6018,6 +6019,12 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/heic-to": { + "version": "1.6.7", + "resolved": "https://registry.npmjs.org/heic-to/-/heic-to-1.6.7.tgz", + "integrity": "sha512-GCPYNl86u6eZ7mr2aa+uQB+PrQOVKSBYfH8esovr8kh2c6EYQSDYIJ6uhXSmM2hPC8PdBVJ/Trn0jnfM9H9gvg==", + "license": "LGPL-3.0" + }, "node_modules/hermes-estree": { "version": "0.25.1", "resolved": "https://registry.npmjs.org/hermes-estree/-/hermes-estree-0.25.1.tgz", diff --git a/web/package.json b/web/package.json index f193fa1050..3079566a95 100644 --- a/web/package.json +++ b/web/package.json @@ -58,6 +58,7 @@ "date-fns": "^4.4.0", "date-fns-tz": "^3.2.0", "framer-motion": "^14.12.0", + "heic-to": "^1.6.7", "hls.js": "^1.7.4", "i18next": "^27.1.1", "i18next-http-backend": "^4.0.10", diff --git a/web/public/locales/en/views/faceLibrary.json b/web/public/locales/en/views/faceLibrary.json index 27e5454601..0357e4f7be 100644 --- a/web/public/locales/en/views/faceLibrary.json +++ b/web/public/locales/en/views/faceLibrary.json @@ -61,8 +61,10 @@ }, "imageEntry": { "validation": { - "selectImage": "Please select an image file." + "selectImage": "Please select an image file.", + "heicConversionFailed": "Unable to convert this HEIC image. Try exporting it as a JPEG instead." }, + "converting": "Converting image…", "dropActive": "Drop the image here…", "dropInstructions": "Drag and drop or paste an image here, or click to select", "maxSize": "Max size: {{size}}MB" diff --git a/web/src/components/input/ImageEntry.tsx b/web/src/components/input/ImageEntry.tsx index 493d107f2d..9d987ff75d 100644 --- a/web/src/components/input/ImageEntry.tsx +++ b/web/src/components/input/ImageEntry.tsx @@ -1,3 +1,4 @@ +import ActivityIndicator from "@/components/indicators/activity-indicator"; import { Button } from "@/components/ui/button"; import { Form, @@ -7,6 +8,11 @@ import { FormMessage, } from "@/components/ui/form"; import { cn } from "@/lib/utils"; +import { + convertHeicToJpeg, + HEIC_EXTENSIONS, + isHeicFile, +} from "@/utils/heicUtil"; import { zodResolver } from "@hookform/resolvers/zod"; import { useCallback, useEffect, useRef, useState } from "react"; import { useDropzone } from "react-dropzone"; @@ -26,11 +32,25 @@ export default function ImageEntry({ onSave, children, maxSize = 20 * 1024 * 1024, // 20MB default - accept = { "image/*": [".jpeg", ".jpg", ".png", ".gif", ".webp"] }, + accept = { + "image/*": [".jpeg", ".jpg", ".png", ".gif", ".webp", ...HEIC_EXTENSIONS], + }, }: ImageEntryProps) { const { t } = useTranslation(["views/faceLibrary"]); const [preview, setPreview] = useState(null); + const [converting, setConverting] = useState(false); const dropzoneRef = useRef(null); + // Bumped on every selection and clear, so a HEIC conversion that finishes + // after the selection changed is discarded instead of applied + const selectionId = useRef(0); + + // Discard conversions that finish after the component unmounts + useEffect(() => { + const id = selectionId; + return () => { + id.current += 1; + }; + }, []); // Auto focus the dropzone useEffect(() => { @@ -60,18 +80,48 @@ export default function ImageEntry({ resolver: zodResolver(formSchema), }); + const selectFile = useCallback( + async (file: File) => { + const id = ++selectionId.current; + let selected = file; + + if (isHeicFile(file)) { + setConverting(true); + const converted = await convertHeicToJpeg(file).catch(() => null); + + // the selection was cleared or replaced while converting + if (id !== selectionId.current) { + return; + } + + setConverting(false); + + if (!converted) { + form.setError("file", { + message: t("imageEntry.validation.heicConversionFailed"), + }); + return; + } + + selected = converted; + } + + form.setValue("file", selected, { shouldValidate: true }); + + // Create preview + const objectUrl = URL.createObjectURL(selected); + setPreview(objectUrl); + }, + [form, t], + ); + const onDrop = useCallback( (acceptedFiles: File[]) => { if (acceptedFiles.length > 0) { - const file = acceptedFiles[0]; - form.setValue("file", file, { shouldValidate: true }); - - // Create preview - const objectUrl = URL.createObjectURL(file); - setPreview(objectUrl); + selectFile(acceptedFiles[0]); } }, - [form], + [selectFile], ); const { getRootProps, getInputProps, isDragActive, isDragReject } = @@ -80,11 +130,17 @@ export default function ImageEntry({ maxSize, accept, multiple: false, + disabled: converting, }); const handlePaste = useCallback( (event: React.ClipboardEvent) => { event.preventDefault(); + + if (converting) { + return; + } + const clipboardItems = Array.from(event.clipboardData.items); for (const item of clipboardItems) { if (item.type.startsWith("image/")) { @@ -95,27 +151,30 @@ export default function ImageEntry({ if (accept["image/*"].includes(extension)) { const fileName = blob.name || `pasted-image.${mimeType}`; const file = new File([blob], fileName, { type: blob.type }); - form.setValue("file", file, { shouldValidate: true }); - const objectUrl = URL.createObjectURL(file); - setPreview(objectUrl); + selectFile(file); return; // Take the first valid image } } } } }, - [form, maxSize, accept], + [converting, maxSize, accept, selectFile], ); const onSubmit = useCallback( (data: z.infer) => { - if (!data.file) return; + // the form still holds the previous image until a replacement + // finishes converting, so saving now would upload the wrong one + if (!data.file || converting) return; onSave(data.file); }, - [onSave], + [converting, onSave], ); const clearSelection = () => { + // drop any running conversion so it can't restore the selection + selectionId.current += 1; + setConverting(false); form.reset(); setPreview(null); }; @@ -142,21 +201,34 @@ export default function ImageEntry({ "flex h-40 flex-col items-center justify-center rounded-lg border-2 border-dashed p-6 transition-colors", isDragActive && "border-primary bg-primary/5", isDragReject && "border-destructive bg-destructive/5", - "cursor-pointer hover:border-primary hover:bg-primary/5", + converting + ? "cursor-wait" + : "cursor-pointer hover:border-primary hover:bg-primary/5", )} > - -

- {isDragActive - ? t("imageEntry.dropActive") - : t("imageEntry.dropInstructions")} -

-

- {t("imageEntry.maxSize", { - size: Math.round(maxSize / (1024 * 1024)), - })} -

+ {converting ? ( + <> + +

+ {t("imageEntry.converting")} +

+ + ) : ( + <> + +

+ {isDragActive + ? t("imageEntry.dropActive") + : t("imageEntry.dropInstructions")} +

+

+ {t("imageEntry.maxSize", { + size: Math.round(maxSize / (1024 * 1024)), + })} +

+ + )} ) : (
@@ -165,6 +237,14 @@ export default function ImageEntry({ alt="Preview" className="h-full w-full rounded-lg border object-contain" /> + {converting && ( +
+ +

+ {t("imageEntry.converting")} +

+
+ )}