Skip to content

Commit 51bceec

Browse files
committed
added support for image source as arraybuffer, buffer and blob
1 parent 45865cd commit 51bceec

File tree

8 files changed

+197
-79
lines changed

8 files changed

+197
-79
lines changed

CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## 2.0.0
2+
3+
- Add support for `arraybuffer`, `blob` and `buffer` as image source
4+
- Use `buffer-image-size` package instread of `probe-image-size`
5+
- Use `centra` package in nodejs to fetch image.
6+
7+
## 1.0.2
8+
9+
- Initial Release
10+
11+
```sh
12+
npm install @coderosh/image-size
13+
```

README.md

+52-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# image-size
22

3-
> Get height and width of image using [Image](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image) api in browser and [prob-image-size](https://npmjs.com/package/probe-image-size) package in nodejs.
3+
> Get height and width of image using [Image](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image) api in browser and [buffer-image-size](https://npmjs.com/package/buffer-image-size) package in nodejs.
44
55
<a href="https://npmjs.com/package/@coderosh/image-size"><img alt="NPM" src="https://img.shields.io/npm/v/@coderosh/image-size" /></a>
66
<a href="https://github.com/coderosh/image-size"><img alt="MIT" src="https://img.shields.io/badge/license-MIT-blue.svg" /></a>
@@ -19,17 +19,60 @@ yarn add @coderosh/image-size
1919

2020
## Usage
2121

22-
```js
23-
import imageSize from '@coderosh/image-size'
22+
- Image source as url (nodejs and browser)
2423

25-
async function main() {
26-
const size = await imageSize('https://ulka.js.org/logo.png')
24+
```js
25+
import imageSize from '@coderosh/image-size'
2726

28-
console.log(size) // { height: 827, width: 738 }
29-
}
27+
const main = async () => {
28+
const url = 'https://ulka.js.org/logo.png'
3029

31-
main()
32-
```
30+
const size = await imageSize(url)
31+
console.log(size) // { height: 827, width: 738 }
32+
}
33+
```
34+
35+
- Image source as arraybuffer (nodejs and browser)
36+
37+
```js
38+
import imageSize from '@coderosh/image-size'
39+
40+
const main = async () => {
41+
const url = 'https://ulka.js.org/logo.png'
42+
const ab = await fetch(url).then((res) => res.arrayBuffer())
43+
44+
const size = await imageSize(ab)
45+
console.log(size) // { height: 827, width: 738 }
46+
}
47+
```
48+
49+
- Image source as buffer (nodejs only)
50+
51+
```js
52+
import imageSize from '@coderosh/image-size'
53+
54+
const main = async () => {
55+
const url = 'https://ulka.js.org/logo.png'
56+
const buffer = await fetch(url).then((res) => res.buffer())
57+
58+
const size = await imageSize(buffer)
59+
console.log(size) // { height: 827, width: 738 }
60+
}
61+
```
62+
63+
- Image source as blob (browser only)
64+
65+
```js
66+
import imageSize from '@coderosh/image-size'
67+
68+
const main = async () => {
69+
const url = 'https://ulka.js.org/logo.png'
70+
const blob = await fetch(url).then((res) => res.blob())
71+
72+
const size = await imageSize(blob)
73+
console.log(size) // { height: 827, width: 738 }
74+
}
75+
```
3376

3477
## License
3578

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@coderosh/image-size",
3-
"version": "1.0.2",
3+
"version": "2.0.0",
44
"description": "Get height and width of image in node and browser",
55
"main": "dist/index.js",
66
"browser": "dist/browser.js",
@@ -16,18 +16,18 @@
1616
"test": "jest",
1717
"clean": "rimraf dist",
1818
"cb": "yarn clean && yarn build",
19-
"prepublishOnly": "yarn lint && yarn test && yarn build"
19+
"prepublishOnly": "yarn lint && yarn test && yarn cb"
2020
},
2121
"engines": {
2222
"node": ">=12"
2323
},
2424
"devDependencies": {
25+
"@types/centra": "^2.2.0",
2526
"@types/jest": "^26.0.23",
2627
"@types/node": "^15.12.4",
27-
"@types/node-fetch": "^2.5.10",
28-
"@types/probe-image-size": "^7.0.1",
2928
"@typescript-eslint/eslint-plugin": "^4.27.0",
3029
"@typescript-eslint/parser": "^4.27.0",
30+
"axios": "^0.24.0",
3131
"canvas": "^2.8.0",
3232
"eslint": "^7.29.0",
3333
"eslint-config-prettier": "^8.3.0",
@@ -74,6 +74,7 @@
7474
"url": "git+https://github.com/coderosh/image-size.git"
7575
},
7676
"dependencies": {
77-
"probe-image-size": "^7.2.1"
77+
"buffer-image-size": "^0.6.4",
78+
"centra": "^2.5.0"
7879
}
7980
}

src/browser.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
const imageSize = async (imgUrl: string) => {
1+
const imageSize = async (src: string | ArrayBuffer | Blob) => {
22
return new Promise<{ height: number; width: number }>((resolve, reject) => {
33
const image = new Image()
4+
let source: string
5+
6+
if (typeof src === 'string') {
7+
source = src
8+
} else if (src instanceof ArrayBuffer) {
9+
source = URL.createObjectURL(new Blob([new Uint8Array(src)]))
10+
} else if (src instanceof Blob) {
11+
source = URL.createObjectURL(src)
12+
} else {
13+
throw new Error(`Invalid argument provided`)
14+
}
415

516
image.onload = () => {
617
const { height, width } = image
18+
19+
if (source.startsWith('blob:')) URL.revokeObjectURL(source)
20+
721
resolve({ height, width })
822
}
923

1024
image.onerror = (err) => reject(err)
1125

12-
image.src = imgUrl
26+
image.src = source
1327
})
1428
}
1529

src/index.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
import probe from 'probe-image-size'
1+
import sizeOf from 'buffer-image-size'
2+
import centra from 'centra'
23

3-
const imageSize = async (imgUrl: string) => {
4-
const { height, width } = await probe(imgUrl)
4+
const imageSize = async (src: string | ArrayBuffer | Buffer) => {
5+
let buffer: Buffer
6+
7+
if (typeof src === 'string') {
8+
buffer = await centra(src)
9+
.send()
10+
.then((res) => res.body)
11+
} else if (src instanceof ArrayBuffer) {
12+
buffer = Buffer.from(src)
13+
} else if (src instanceof Buffer) {
14+
buffer = src
15+
} else {
16+
throw new Error(`Invalid argument provided`)
17+
}
18+
19+
const { height, width } = sizeOf(buffer)
520

621
return { height, width }
722
}

tests/browser.test.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
import imageSize from '../src/browser'
2+
import axios from 'axios'
23

3-
it('imageSize:browser should return width and height', async () => {
4-
const size = await imageSize('https://ulka.js.org/logo.png')
4+
const imgUrl = 'https://ulka.js.org/logo.png'
55

6-
expect(size).toEqual({ height: 827, width: 738 })
6+
describe('imageSize:browser', () => {
7+
beforeAll(() => {
8+
window.URL.createObjectURL = () => imgUrl
9+
})
10+
11+
it('should return width and height if src is url', async () => {
12+
const size = await imageSize(imgUrl)
13+
14+
expect(size).toEqual({ height: 827, width: 738 })
15+
})
16+
17+
it('should return height and width if src is arraybuffer', async () => {
18+
const res = await axios.get(imgUrl, { responseType: 'arraybuffer' })
19+
20+
const size = await imageSize(res.data)
21+
22+
expect(size).toEqual({ height: 827, width: 738 })
23+
})
24+
25+
it('should return height and width if src is Blob', async () => {
26+
const res = await axios.get(imgUrl, { responseType: 'blob' })
27+
28+
const size = await imageSize(res.data)
29+
30+
expect(size).toEqual({ height: 827, width: 738 })
31+
})
732
})

tests/index.test.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1+
import axios from 'axios'
12
import imageSize from '../src/'
3+
import centra from 'centra'
24

3-
test('imageSize:node should return width and height', async () => {
4-
const size = await imageSize('https://ulka.js.org/logo.png')
5-
expect(size).toEqual({ height: 827, width: 738 })
5+
const imgUrl = 'https://ulka.js.org/logo.png'
6+
7+
describe('imageSize:node', () => {
8+
it('should return width and height if src is string', async () => {
9+
const size = await imageSize('https://ulka.js.org/logo.png')
10+
expect(size).toEqual({ height: 827, width: 738 })
11+
})
12+
13+
it('should return height and width if src is arraybuffer', async () => {
14+
const res = await axios.get(imgUrl, { responseType: 'arraybuffer' })
15+
const size = await imageSize(res.data)
16+
expect(size).toEqual({ height: 827, width: 738 })
17+
})
18+
19+
it('should return height and width if src is buffer', async () => {
20+
const buffer = await centra(imgUrl)
21+
.send()
22+
.then((res) => res.body)
23+
24+
const size = await imageSize(buffer)
25+
expect(size).toEqual({ height: 827, width: 738 })
26+
})
627
})

0 commit comments

Comments
 (0)