new changes

This commit is contained in:
Niranjan
2026-04-07 05:05:28 +05:30
parent 7c070224bd
commit a18bba15f2
29975 changed files with 3247495 additions and 2761 deletions

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 小弟调调™
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,91 @@
Image To URI
===
[![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor)
[![NPM Downloads](https://img.shields.io/npm/dm/image2uri.svg?style=flat)](https://www.npmjs.com/package/image2uri)
[![Build & Test](https://github.com/jaywcjlove/image2uri/actions/workflows/ci.yml/badge.svg)](https://github.com/jaywcjlove/image2uri/actions/workflows/ci.yml)
[![npm version](https://img.shields.io/npm/v/image2uri.svg)](https://www.npmjs.com/package/image2uri)
[![Coverage Status](https://jaywcjlove.github.io/image2uri/badges.svg)](https://jaywcjlove.github.io/image2uri/lcov-report/)
Convert image file to data URI. Support `.png`,`.gif`,`.jpg`,`.jpeg`,`.bm`,`.bmp`,`.webp`,`.ico`,`.svg`.
### Install
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): Node 12+ is needed to use it and it must be import instead of require.
```bash
npm i image2uri
```
```js
const recursiveReaddirFiles = await import('image2uri');
// Fix compiling in typescript.
// https://github.com/microsoft/TypeScript/issues/43329#issuecomment-922544562
const { getExt, recursiveReaddirFiles } = await (Function('return import("image2uri")')()) as Promise<typeof import("image2uri")>;
```
### Basic Usage
```js
import image2uri from "image2uri";
console.log(image2uri('./example.bmp'));
// data:image/bmp;base64,Qk0YCAAAAAAAADYAAAAoAAAAGAAAABwAAAABABgAAAAAAOIHAAA....
console.log(image2uri('./example.jpg'));
// data:image/jpeg;base64,Qk0YCAAAAAAAADYAAAAoAAAAGAAAABwAAAABABgAAAAAAOIHAAA....
const uri = await image2uri('https://avatars.githubusercontent.com/u/1680273?v=4', { ext: '.apng' });
// data:image/apng;base64,/9j/2wCEAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDc
const avatar = await image2uri('https://avatars.githubusercontent.com/u/1680273?v=4');
// /9j/2wCEAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDc
```
### API
```ts
export declare const validUrl: (url: string) => boolean;
export declare const extTypeMap: {
'.png': string;
'.apng': string;
'.gif': string;
'.jpg': string;
'.jpeg': string;
'.bm': string;
'.bmp': string;
'.webp': string;
'.ico': string;
'.svg': string;
};
export type ExtType = keyof typeof extTypeMap;
export default function image2uri(file: string, options?: {
ext?: string;
}): string | Promise<string>;
```
### Development
```bash
npm run watch # Listen compile .ts files.
npm run build # compile .ts files.
npm run start
```
### Related
- [recursive-readdir-files](https://github.com/jaywcjlove/recursive-readdir-files) Node.js module to list all files in a directory or any subdirectories.
## Contributors
As always, thanks to our amazing contributors!
<a href="https://github.com/jaywcjlove/image2uri/graphs/contributors">
<img src="https://jaywcjlove.github.io/image2uri/CONTRIBUTORS.svg" />
</a>
Made with [action-contributors](https://github.com/jaywcjlove/github-action-contributors).
### License
Licensed under the MIT License.

View File

@@ -0,0 +1,17 @@
export declare const validUrl: (url: string) => boolean;
export declare const extTypeMap: {
'.png': string;
'.apng': string;
'.gif': string;
'.jpg': string;
'.jpeg': string;
'.bm': string;
'.bmp': string;
'.webp': string;
'.ico': string;
'.svg': string;
};
export type ExtType = keyof typeof extTypeMap;
export default function image2uri(file: string, options?: {
ext?: string;
}): string | Promise<string>;

View File

@@ -0,0 +1,28 @@
import fs from 'fs';
import path from 'path';
import fetch from 'node-fetch';
export const validUrl = (url) => /http(s)?:\/\/(\w+:?\w*@)?(\S+)(:\d+)?((?<=\.)\w+)+(\/([\w#!:.?+=&%@!\-/])*)?/gi.test(url);
export const extTypeMap = {
'.png': 'image/png',
'.apng': 'image/apng',
'.gif': 'image/gif',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.bm': 'image/bmp',
'.bmp': 'image/bmp',
'.webp': 'image/webp',
'.ico': 'image/x-icon',
'.svg': 'image/svg+xml'
};
export default function image2uri(file, options = {}) {
const ext = (options.ext || path.extname(file));
const contentType = extTypeMap[ext];
if (validUrl(file)) {
return fetch(file).then((response) => response.buffer()).then((buffer) => {
return contentType ? `data:${contentType};base64,${buffer.toString('base64')}` : buffer.toString('base64');
});
}
const image = fs.readFileSync(file);
return contentType ? `data:${contentType};base64,${image.toString('base64')}` : image.toString('base64');
}
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,MAAM,YAAY,CAAC;AAE/B,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,gFAAgF,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACpI,MAAM,CAAC,MAAM,UAA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uri/",
"funding": "https://jaywcjlove.github.io/#/sponsor",
"license": "MIT",
"author": "Kenny <wowohoo@qq.com>",
"type": "module",
"exports": "./lib/index.js",
"types": "./lib/index.d.ts",
"main": "./lib/index.js",
"scripts": {
"prepare": "npm run build",
"watch": "tsbb watch",
"build": "tsbb build",
"test": "tsbb test",
"coverage": "tsbb test --coverage"
},
"repository": {
"type": "git",
"url": "https://github.com/jaywcjlove/image2uri"
},
"files": [
"lib",
"src"
],
"jest": {
"transformIgnorePatterns": [
"<rootDir>/node_modules/?!(.*)"
],
"coverageReporters": [
"lcov",
"json-summary"
]
},
"keywords": [
"image",
"uri",
"fs",
"base64",
"image-to-uri",
"image2uri"
],
"dependencies": {
"node-fetch": "^3.3.1"
},
"devDependencies": {
"@types/node": "^18.15.11",
"tsbb": "^4.1.14"
}
}

View File

@@ -0,0 +1,30 @@
import fs from 'fs';
import path from 'path';
import fetch from 'node-fetch';
export const validUrl = (url: string) => /http(s)?:\/\/(\w+:?\w*@)?(\S+)(:\d+)?((?<=\.)\w+)+(\/([\w#!:.?+=&%@!\-/])*)?/gi.test(url);
export const extTypeMap = {
'.png': 'image/png',
'.apng': 'image/apng',
'.gif': 'image/gif',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.bm': 'image/bmp',
'.bmp': 'image/bmp',
'.webp': 'image/webp',
'.ico': 'image/x-icon',
'.svg': 'image/svg+xml'
}
export type ExtType = keyof typeof extTypeMap;
export default function image2uri(file: string, options: { ext?: string } = {}): string | Promise<string> {
const ext: ExtType = (options.ext || path.extname(file)) as ExtType;
const contentType = extTypeMap[ext]
if (validUrl(file)) {
return fetch(file).then((response) => response.buffer()).then((buffer) => {
return contentType ? `data:${contentType};base64,${buffer.toString('base64')}` : buffer.toString('base64');
});
}
const image = fs.readFileSync(file);
return contentType ? `data:${contentType};base64,${image.toString('base64')}` : image.toString('base64');
}