Pasted images
react-smart-copy/image gets pasted and dropped images ready to upload. A photo from a
phone usually carries the exact location it was taken, the device and its serial number,
and an embedded preview that can show what was cropped out. This module removes that
data, and makes the image fit your size and byte limits. No dependencies, about 7.3 kB
(about 4.8 kB for the server-safe stripMetadata and readImageInfo).
Try it
Section titled “Try it”Or paste or drop one here
Click here and press Ctrl + V or ⌘ + V, or drop an image file.
No image yet.
Nothing leaves with the photo
Phone photos carry where they were taken, the device and an embedded preview. prepareImage
removes all of it. When only the metadata has to go, no pixel is touched.
For limits, it searches for the highest quality that fits before it makes the image smaller. Sizes are read from the header and checked before decoding, so a hostile file can't exhaust memory.
import { prepareImage } from 'react-smart-copy/image';
usePaste({ accept: ['image'], onPaste: async ({ imageFiles }) => {
const image = await prepareImage(imageFiles[0], {
maxWidth: 2048, maxBytes: 1_000_000,
});
upload(image.file);
} });
import { usePaste } from "react-smart-copy";import { prepareImage } from "react-smart-copy/image";
usePaste({ accept: ["image"], onPaste: async ({ imageFiles }) => { const image = await prepareImage(imageFiles[0], { maxWidth: 2048, maxBytes: 1_000_000 }); upload(image.file); // a File named like the original, metadata removed if (image.original.info.metadata.includes("gps")) notice("Location removed from the photo."); },});Lossless whenever it can be. When only the metadata has to go, nothing is
re-encoded. JPEG, PNG, WebP and GIF are rewritten without their metadata, and every
pixel stays the same, so screenshots stay sharp and photos lose no quality. The image is
decoded and encoded again only when it has to change: to fit maxWidth, maxHeight,
maxPixels or maxBytes, to change its format, or because it is AVIF or HEIC.
What is removed: Exif (camera, lens, dates, serial numbers), the GPS location, embedded
thumbnails, XMP (edit history, authors), IPTC (captions, credits), comments, PNG text chunks,
multi-picture and depth images (MPF), content credentials (C2PA), and data appended after
the image (motion-photo videos). The colour profile is kept (keepColorProfile: false
removes it). The orientation is kept in a 26-byte Exif block holding nothing else, so a
photo still displays the right way up (keepOrientation: false removes it).
Fits a byte budget. For maxBytes, JPEG and WebP search for the highest quality that
fits (down to minQuality, 0.5 by default), and only then reduce the size. A screenshot
stays PNG when it fits. When it doesn’t, type: "auto" turns it into WebP, or JPEG where
the browser can’t encode WebP. If the limit can’t be met, you get a too-large error,
never a file that is still too big. Large reductions are drawn in halving steps, so text
and edges stay clean.
Safe with hostile files. The format is read from the bytes, never from the file name
or the claimed type. Sizes come from the header and are checked against
maxInputPixels (100 million) before decoding, which stops decompression bombs. Every
parser read is bounded by its own box or chunk. Fuzz tests feed random and mutated files
to every parser. Animations keep every frame by default (animated: "keep"): a resize
they would need is a too-large error, "first-frame" makes a still image, and
"reject" refuses them. Every failure is a typed error: too-large,
unsupported-format, unsupported (re-encoding outside a browser), timeout or
aborted (signal).
import { readImageInfo, stripMetadata } from "react-smart-copy/image";
// Anywhere, including the server (Node 18+): no decoding, no canvas.const info = await readImageInfo(file);// { format: "jpeg", width: 4032, height: 3024, orientation: 6, animated: false,// metadata: ["exif", "gps", "thumbnail", "icc", "other"] }
const { blob, removed } = await stripMetadata(file); // bit-for-bit the same pictureOption (prepareImage) |
Default | Notes |
|---|---|---|
maxWidth / maxHeight |
none | only ever makes images smaller |
maxPixels |
16,777,216 | Safari’s canvas limit |
maxBytes |
none | quality first, then size; too-large when it can’t |
type |
"auto" |
"image/jpeg", "image/png" or "image/webp" |
quality / minQuality |
0.85 / 0.5 | JPEG and WebP |
background |
"#fff" |
under transparent pixels in JPEG |
animated |
"keep" |
"first-frame" or "reject" |
keepColorProfile |
true |
re-encoded images are written in sRGB |
maxInputPixels / maxInputBytes |
100,000,000 / 100 MiB | checked before decoding |
timeoutMs / signal |
30 s / none | |
environment |
the browser | your own decoder (e.g. WebAssembly HEIC) or a test double |
width and height in the result are the displayed size. Browsers turn JPEG and PNG by
their orientation, and Chromium doesn’t turn WebP. removed is read back from the
output, not assumed, because some browsers’ encoders add their own colour profile.