{"id":13454537,"url":"https://github.com/sindresorhus/image-type","last_synced_at":"2025-05-15T23:06:04.661Z","repository":{"id":16253041,"uuid":"19000970","full_name":"sindresorhus/image-type","owner":"sindresorhus","description":"Detect the image type of a Buffer/Uint8Array","archived":false,"fork":false,"pushed_at":"2024-06-26T12:20:09.000Z","size":347,"stargazers_count":374,"open_issues_count":0,"forks_count":16,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-05-06T07:23:49.153Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sindresorhus.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/security.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","buy_me_a_coffee":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2014-04-21T17:46:50.000Z","updated_at":"2025-03-31T13:44:01.000Z","dependencies_parsed_at":"2023-11-11T11:03:54.163Z","dependency_job_id":"bfb0780a-c9df-4901-b2d1-8da1426c9848","html_url":"https://github.com/sindresorhus/image-type","commit_stats":{"total_commits":49,"total_committers":7,"mean_commits":7.0,"dds":"0.20408163265306123","last_synced_commit":"36cd06dd7a7894b74c4e891ab0be7dca4bdf6a56"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fimage-type","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fimage-type/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fimage-type/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fimage-type/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/image-type/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253002858,"owners_count":21838641,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T08:00:55.119Z","updated_at":"2025-05-15T23:06:04.625Z","avatar_url":"https://github.com/sindresorhus.png","language":"JavaScript","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://buymeacoffee.com/sindresorhus","https://sindresorhus.com/donate"],"categories":["JavaScript","Packages","Repository","包","目录","Image","Uncategorized"],"sub_categories":["Image","图像","图像处理","Uncategorized"],"readme":"# image-type\n\n\u003e Detect the image type of an ArrayBuffer/Uint8Array\n\nSee the [`file-type`](https://github.com/sindresorhus/file-type) module for more file types and a CLI.\n\n## Install\n\n```sh\nnpm install image-type\n```\n\n## Usage\n\n##### Node.js\n\n```js\nimport {readChunk} from 'read-chunk';\nimport imageType, {minimumBytes} from 'image-type';\n\nconst buffer = await readChunk('unicorn.png', {length: minimumBytes});\n\nawait imageType(buffer);\n//=\u003e {ext: 'png', mime: 'image/png'}\n```\n\nOr from a remote location:\n\n```js\nimport https from 'node:https';\nimport imageType, {minimumBytes} from 'image-type';\n\nconst url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';\n\nhttps.get(url, response =\u003e {\n\tresponse.on('readable', () =\u003e {\n\t\t(async () =\u003e {\n\t\t\tconst chunk = response.read(minimumBytes);\n\t\t\tresponse.destroy();\n\t\t\tconsole.log(await imageType(chunk));\n\t\t\t//=\u003e {ext: 'jpg', mime: 'image/jpeg'}\n\t\t})();\n\t});\n});\n```\n\n##### Browser\n\n```js\nconst xhr = new XMLHttpRequest();\nxhr.open('GET', 'unicorn.png');\nxhr.responseType = 'arraybuffer';\n\nxhr.onload = () =\u003e {\n\t(async () =\u003e {\n\t\tawait imageType(new Uint8Array(this.response));\n\t\t//=\u003e {ext: 'png', mime: 'image/png'}\n\t})();\n};\n\nxhr.send();\n```\n\n## API\n\n### imageType(input)\n\nReturns an `Promise\u003cobject\u003e` with:\n\n- `ext` - One of the [supported file types](#supported-file-types)\n- `mime` - The [MIME type](https://en.wikipedia.org/wiki/Internet_media_type)\n\nOr `undefined` when there is no match.\n\n#### input\n\nType: `ArrayBuffer | Uint8Array`\n\nIt only needs the first `minimumBytes` amount of bytes.\n\n### minimumBytes\n\nType: `number`\n\nThe minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hardcode it.\n\n## Supported file types\n\n- [`jpg`](https://en.wikipedia.org/wiki/JPEG)\n- [`png`](https://en.wikipedia.org/wiki/Portable_Network_Graphics)\n- [`gif`](https://en.wikipedia.org/wiki/GIF)\n- [`webp`](https://en.wikipedia.org/wiki/WebP)\n- [`flif`](https://en.wikipedia.org/wiki/Free_Lossless_Image_Format)\n- [`cr2`](https://fileinfo.com/extension/cr2)\n- [`tif`](https://en.wikipedia.org/wiki/Tagged_Image_File_Format)\n- [`bmp`](https://en.wikipedia.org/wiki/BMP_file_format)\n- [`jxr`](https://en.wikipedia.org/wiki/JPEG_XR)\n- [`psd`](https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format)\n- [`ico`](https://en.wikipedia.org/wiki/ICO_(file_format))\n- [`bpg`](https://bellard.org/bpg/)\n- [`jp2`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000\n- [`jpm`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000\n- [`jpx`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000\n- [`heic`](https://nokiatech.github.io/heif/technical.html)\n- [`cur`](https://en.wikipedia.org/wiki/ICO_(file_format))\n- [`dcm`](https://en.wikipedia.org/wiki/DICOM#Data_format) - DICOM Image File\n\n*SVG isn't included as it requires the whole file to be read, but you can get it [here](https://github.com/sindresorhus/is-svg).*\n\n## Related\n\n- [file-type](https://github.com/sindresorhus/file-type) - Detect the type of a file\n- [image-dimensions](https://github.com/sindresorhus/image-dimensions) - Get the dimensions of an image\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fimage-type","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Fimage-type","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fimage-type/lists"}