Skip to content

Commit

Permalink
feat: add fetch image conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jan 26, 2022
1 parent 81ab6eb commit 348549b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ 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
```

### Development
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "image2uri",
"version": "1.0.5",
"version": "1.1.0",
"description": "Convert image file to data URI.",
"homepage": "https://jaywcjlove.github.io/image2uri/",
"license": "MIT",
Expand Down Expand Up @@ -30,7 +30,10 @@
"image-to-uri",
"image2uri"
],
"dependencies": {
"node-fetch": "~2.6.1"
},
"devDependencies": {
"tsbb": "3.3.7"
"tsbb": "3.5.4"
}
}
19 changes: 14 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
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',
Expand All @@ -14,6 +17,7 @@ export const extTypeMap = {
}

export type ExtType = '.png'
| '.apng'
| '.gif'
| '.jpg'
| '.jpeg'
Expand All @@ -23,9 +27,14 @@ export type ExtType = '.png'
| '.ico'
| '.svg';

export default function image2uri(file: string) {
const image = fs.readFileSync(file)
const ext = path.extname(file) as ExtType;
const contentType = extTypeMap[ext] || 'image/jpeg'
return `data:${contentType};base64,${image.toString('base64')}`
export default function image2uri(file: string, options: { ext?: string } = {}): string | Promise<string> {
const ext = 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');
}
Binary file added test/imgs/uiw-jpeg
Binary file not shown.
24 changes: 24 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ it('test uiw.bm', () => {
const ext = path.extname(bmPath) as ExtType;
expect(image2uri).toBeDefined()
expect(typeof uri).toBe('string');
// @ts-ignore
expect(extTypeMap[ext]).toEqual(uri.replace(/^data:(.*);base64\,.+/, "$1"));
});

Expand All @@ -15,6 +16,7 @@ it('test uiw.bmp', () => {
const uri = image2uri(bmpPath);
const ext = path.extname(bmpPath) as ExtType;
expect(typeof uri).toBe('string');
// @ts-ignore
expect(extTypeMap[ext]).toEqual(uri.replace(/^data:(.*);base64\,.+/, "$1"));
});

Expand All @@ -23,6 +25,7 @@ it('test uiw.gif', () => {
const uri = image2uri(gifPath);
const ext = path.extname(gifPath) as ExtType;
expect(typeof uri).toBe('string');
// @ts-ignore
expect(extTypeMap[ext]).toEqual(uri.replace(/^data:(.*);base64\,.+/, "$1"));
});

Expand All @@ -31,6 +34,7 @@ it('test uiw.ico', () => {
const uri = image2uri(icoPath)
const ext = path.extname(icoPath) as ExtType;
expect(typeof uri).toBe('string');
// @ts-ignore
expect(extTypeMap[ext]).toEqual(uri.replace(/^data:(.*);base64\,.+/, "$1"));
});

Expand All @@ -39,13 +43,24 @@ it('test uiw.jpeg', () => {
const uri = image2uri(jpegPath);
const ext = path.extname(jpegPath) as ExtType;
expect(typeof uri).toBe('string');
// @ts-ignore
expect(extTypeMap[ext]).toEqual(uri.replace(/^data:(.*);base64\,.+/, "$1"));
});

it('test uiw-jpeg', () => {
const jpegPath = path.join(__dirname, 'imgs', 'uiw-jpeg');
const uri = image2uri(jpegPath);
const ext = path.extname(jpegPath) as ExtType;
expect(typeof uri).toBe('string');
// @ts-ignore
expect(uri.indexOf('data:image')).toEqual(-1);
});

it('test uiw.jpg', () => {
const jpgPath = path.join(__dirname, 'imgs', 'uiw.jpg');
const uri = image2uri(jpgPath);
const ext = path.extname(jpgPath) as ExtType;
// @ts-ignore
expect(extTypeMap[ext]).toEqual(uri.replace(/^data:(.*);base64\,.+/, "$1"));
});

Expand All @@ -54,13 +69,15 @@ it('test uiw.png', () => {
const uri = image2uri(pngPath);
const ext = path.extname(pngPath) as ExtType;
expect(typeof uri).toBe('string');
// @ts-ignore
expect(extTypeMap[ext]).toEqual(uri.replace(/^data:(.*);base64\,.+/, "$1"));
});

it('test uiw.svg', () => {
const svgPath = path.join(__dirname, 'imgs', 'uiw.svg');
const uri = image2uri(svgPath);
const ext = path.extname(svgPath) as ExtType;
// @ts-ignore
expect(extTypeMap[ext]).toEqual(uri.replace(/^data:(.*);base64\,.+/, "$1"));
});

Expand All @@ -69,5 +86,12 @@ it('test uiw.webp', () => {
const uri = image2uri(webpPath);
const ext = path.extname(webpPath) as ExtType;
expect(typeof uri).toBe('string');
// @ts-ignore
expect(extTypeMap[ext]).toEqual(uri.replace(/^data:(.*);base64\,.+/, "$1"));
});

it('test https....', async () => {
const uri = await image2uri('https://avatars.githubusercontent.com/u/1680273?v=4', { ext: '.apng' });
expect(uri.indexOf('data:image/apng;base64')).toEqual(0);
});

0 comments on commit 348549b

Please sign in to comment.