mediaTypeOf
Finds the media type of an image from its first bytes (its magic number), whatever its name
says: PNG, JPEG, GIF, WebP, AVIF and SVG. SVG has no magic number, so it is recognized as text
whose first element is <svg, possibly after a byte order mark, whitespace, an XML
declaration, comments or a doctype.
Available since v3.1.10
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
bytes | Uint8Array | The start of the file: 512 bytes are enough for every type but a long SVG prolog |
Returns
Section titled “Returns”string | undefined — The media type ('image/png', …), or undefined when it is none of these
Examples
Section titled “Examples”Sniff an image
Section titled “Sniff an image”The first bytes tell the format, whatever the file is called.
