image-size
Advanced tools
Comparing version 2.0.0-beta.2 to 2.0.0-beta.4
@@ -28,9 +28,7 @@ #!/usr/bin/env node | ||
const greyImage = colorize(image, grey) | ||
const size = await imageSize(image) | ||
const sizes = size.images || [size] | ||
const result = await imageSize(image) | ||
const sizes = result.images || [result] | ||
sizes.forEach((size) => { | ||
let greyType = '' | ||
if (size.type) { | ||
greyType = colorize(' (' + size.type + ')', grey) | ||
} | ||
const type = size.type ?? result.type; | ||
const greyType = type ? colorize(` (${type})`, grey) : '' | ||
console.info( | ||
@@ -42,7 +40,7 @@ colorize(size.width, green) + | ||
greyImage + | ||
greyType | ||
greyType, | ||
) | ||
}) | ||
} else { | ||
console.error('file doesn\'t exist - ', image) | ||
console.error("file doesn't exist - ", image) | ||
} | ||
@@ -49,0 +47,0 @@ } catch (e) { |
{ | ||
"name": "image-size", | ||
"version": "2.0.0-beta.2", | ||
"version": "2.0.0-beta.4", | ||
"description": "get dimensions of any image file", | ||
"main": "./dist/cjs/index.js", | ||
"module": "./dist/esm/index.mjs", | ||
"types": "./dist/dts/index.d.ts", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"require": "./dist/cjs/index.js", | ||
"import": "./dist/esm/index.mjs", | ||
"types": "./dist/dts/index.d.ts" | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.cjs" | ||
} | ||
}, | ||
"./*": { | ||
"require": "./dist/cjs/*.js", | ||
"import": "./dist/esm/*.mjs", | ||
"types": "./dist/dts/*.d.ts" | ||
"./fromFile": { | ||
"import": { | ||
"types": "./dist/fromFile.d.ts", | ||
"default": "./dist/fromFile.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/fromFile.d.ts", | ||
"default": "./dist/fromFile.cjs" | ||
} | ||
}, | ||
"./types/*": { | ||
"require": "./dist/cjs/types/*.js", | ||
"import": "./dist/esm/types/*.mjs", | ||
"types": "./dist/dts/types/*.d.ts" | ||
"import": { | ||
"types": "./dist/types/*.d.ts", | ||
"default": "./dist/types/*.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/types/*.d.ts", | ||
"default": "./dist/types/*.cjs" | ||
} | ||
} | ||
@@ -30,14 +45,17 @@ }, | ||
"engines": { | ||
"node": ">=18.18.0" | ||
"node": ">=16.x" | ||
}, | ||
"packageManager": "yarn@4.0.2", | ||
"bin": "bin/image-size.js", | ||
"bin": { | ||
"image-size": "bin/image-size.js" | ||
}, | ||
"scripts": { | ||
"lint": "eslint --ext .ts,.js bin lib specs", | ||
"format": "prettier --write lib specs", | ||
"test": "TS_NODE_PROJECT=tsconfig.cjs.json nyc mocha", | ||
"lint": "biome check lib specs", | ||
"format": "biome format --write lib specs", | ||
"test": "TS_NODE_PROJECT=tsconfig.test.json c8 --reporter=text --reporter=lcov node --require ts-node/register --test --test-reporter=dot specs/*.spec.ts", | ||
"test:watch": "TS_NODE_PROJECT=tsconfig.test.json node --require ts-node/register --test --watch specs/*.spec.ts", | ||
"clean": "rm -rf dist docs", | ||
"generate-docs": "typedoc", | ||
"build": "tsc && tsc -p tsconfig.cjs.json", | ||
"prepack": "yarn clean && yarn build && node scripts/fix-package.js" | ||
"build": "tsup", | ||
"prepack": "yarn clean && yarn build" | ||
}, | ||
@@ -60,2 +78,3 @@ "keywords": [ | ||
"jpeg", | ||
"jxl", | ||
"png", | ||
@@ -68,27 +87,18 @@ "psd", | ||
], | ||
"repository": "git://github.com/image-size/image-size.git", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/image-size/image-size.git" | ||
}, | ||
"author": "netroy <aditya@netroy.in> (http://netroy.in/)", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/chai": "4.3.11", | ||
"@types/chai-as-promised": "7.1.8", | ||
"@biomejs/biome": "1.9.4", | ||
"@types/glob": "8.1.0", | ||
"@types/mocha": "10.0.6", | ||
"@types/node": "18.19.3", | ||
"@types/sinon": "17.0.2", | ||
"@typescript-eslint/eslint-plugin": "6.16.0", | ||
"@typescript-eslint/parser": "6.16.0", | ||
"chai": "4.3.10", | ||
"chai-as-promised": "7.1.1", | ||
"eslint": "8.56.0", | ||
"eslint-config-prettier": "9.1.0", | ||
"eslint-plugin-prettier": "5.1.2", | ||
"glob": "10.3.10", | ||
"mocha": "10.2.0", | ||
"nyc": "15.1.0", | ||
"prettier": "3.1.1", | ||
"sinon": "17.0.1", | ||
"@types/node": "18.19.39", | ||
"c8": "10.1.3", | ||
"glob": "10.4.2", | ||
"ts-node": "10.9.2", | ||
"typedoc": "0.25.4", | ||
"typescript": "5.3.3" | ||
"tsup": "8.3.5", | ||
"typedoc": "0.25.13", | ||
"typescript": "5.4.5" | ||
}, | ||
@@ -95,0 +105,0 @@ "nyc": { |
112
Readme.md
@@ -5,6 +5,14 @@ # image-size | ||
[![Package Version](https://img.shields.io/npm/v/image-size.svg)](https://www.npmjs.com/package/image-size) | ||
[![Downloads](https://img.shields.io/npm/dm/image-size.svg)](http://npm-stat.com/charts.html?package=image-size&author=&from=&to=) | ||
[![Downloads](https://img.shields.io/npm/dm/image-size.svg)](http://npm-stat.com/charts.html?package=image-size&author=netroy&from=&to=) | ||
A [Node](https://nodejs.org/en/) module to get dimensions of any image file | ||
Fast, lightweight NodeJS package to get dimensions of any image file or buffer. | ||
## Key Features | ||
- Zero dependencies | ||
- Supports all major image formats | ||
- Works with both files and buffers | ||
- Minimal memory footprint - reads only image headers | ||
- ESM and CommonJS support | ||
- TypeScript types included | ||
## Supported formats | ||
@@ -20,4 +28,5 @@ | ||
- J2C | ||
- JP2 | ||
- JPEG-2000 (JP2) | ||
- JPEG | ||
- JPEG-XL | ||
- KTX (1 and 2) | ||
@@ -35,40 +44,55 @@ - PNG | ||
```shell | ||
npm install image-size --save | ||
``` | ||
or | ||
```shell | ||
npm install image-size | ||
# or | ||
yarn add image-size | ||
# or | ||
pnpm add image-size | ||
``` | ||
## Programmatic Usage | ||
## Usage | ||
### Passing in a Buffer/Uint8Array | ||
Best for streams, network requests, or when you already have the image data in memory. | ||
```javascript | ||
import { imageSize } from 'image-size' | ||
// or | ||
const { imageSize } = require('image-size') | ||
const { width, height } = imageSize(bufferObject) | ||
console.log(width, height) | ||
const dimensions = imageSize(buffer) | ||
console.log(dimensions.width, dimensions.height) | ||
``` | ||
### Reading from a file | ||
Best for local files. Returns a promise. | ||
```javascript | ||
const { imageSize } = require('image-size/fromFile') | ||
const dimensions = await imageSize('images/funny-cats.png') | ||
import { imageSizeFromFile } from 'image-size/fromFile' | ||
// or | ||
const { imageSizeFromFile } = require('image-size/fromFile') | ||
const dimensions = await imageSizeFromFile('photos/image.jpg') | ||
console.log(dimensions.width, dimensions.height) | ||
``` | ||
NOTE: Reading from files haa a default concurrency limit of **100** | ||
Note: Reading from files has a default concurrency limit of **100** | ||
To change this limit, you can call the `setConcurrency` function like this: | ||
```javascript | ||
const sizeOf = require('image-size/fromFile') | ||
sizeOf.setConcurrency(123456) | ||
import { setConcurrency } from 'image-size/fromFile' | ||
// or | ||
const { setConcurrency } = require('image-size/fromFile') | ||
setConcurrency(123456) | ||
``` | ||
### 3. Command Line | ||
Useful for quick checks. | ||
```shell | ||
npx image-size image1.jpg image2.png | ||
``` | ||
### Multi-size | ||
If the target file/buffer is an icon (.ico) or a cursor (.cur), the `width` and `height` will be the ones of the first found image. | ||
If the target file/buffer is an HEIF, an ICO, or a CUR file, the `width` and `height` will be the ones of the largest image in the set. | ||
@@ -78,4 +102,7 @@ An additional `images` array is available and returns the dimensions of all the available images | ||
```javascript | ||
const { imageSize } = require('image-size/fromFile') | ||
const { images } = await imageSize('images/multi-size.ico') | ||
import { imageSizeFromFile } from 'image-size/fromFile' | ||
// or | ||
const { imageSizeFromFile } = require('image-size/fromFile') | ||
const { images } = await imageSizeFromFile('images/multi-size.ico') | ||
for (const dimensions of images) { | ||
@@ -89,7 +116,6 @@ console.log(dimensions.width, dimensions.height) | ||
```javascript | ||
const url = require('node:url') | ||
const http = require('node:http') | ||
import url from 'node:url' | ||
import http from 'node:http' | ||
import { imageSize } from 'image-size' | ||
const { imageSize } = require('image-size') | ||
const imgUrl = 'http://my-amazing-website.com/image.jpeg' | ||
@@ -117,3 +143,6 @@ const options = url.parse(imgUrl) | ||
```javascript | ||
import { disableTypes } from 'image-size' | ||
// or | ||
const { disableTypes } = require('image-size') | ||
disableTypes(['tiff', 'ico']) | ||
@@ -127,25 +156,32 @@ ``` | ||
```javascript | ||
const { imageSize } = require('image-size/fromFile') | ||
const { width, height, orientation } = imageSize('images/photo.jpeg') | ||
import { imageSizeFromFile } from 'image-size/fromFile' | ||
// or | ||
const { imageSizeFromFile } = require('image-size/fromFile') | ||
const { width, height, orientation } = await imageSizeFromFile('images/photo.jpeg') | ||
console.log(width, height, orientation) | ||
``` | ||
## Command-Line Usage (CLI) | ||
# Limitations | ||
```shell | ||
npm install image-size --global | ||
``` | ||
1. **Partial File Reading** | ||
- Only reads image headers, not full files | ||
- Some corrupted images might still report dimensions | ||
or | ||
2. **SVG Limitations** | ||
- Only supports pixel dimensions and viewBox | ||
- Percentage values not supported | ||
```shell | ||
yarn global add image-size | ||
``` | ||
3. **File Access** | ||
- Reading from files has a default concurrency limit of 100 | ||
- Can be adjusted using `setConcurrency()` | ||
followed by | ||
4. **Buffer Requirements** | ||
- Some formats (like TIFF) require the full header in buffer | ||
- Streaming partial buffers may not work for all formats | ||
```shell | ||
image-size image1 [image2] [image3] ... | ||
``` | ||
## License | ||
MIT | ||
## Credits | ||
@@ -152,0 +188,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
377939
9
116
12319
186
1