# analyze-image

> Analyzes an image and detects potential improvements for a webpage

Latest version **1.0.0** (published 2023-08-16) · BSD-2-Clause license · 0 weekly downloads

## Install

```sh
npm install analyze-image
pnpm add analyze-image
yarn add analyze-image
bun add analyze-image
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2023-08-16 |
| First published | 2023-08-16 |
| Weekly downloads | 0 |
| License | BSD-2-Clause |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 527.5 KB |
| Known vulnerabilities | 0 (+8 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Gaël Métais |
| Maintainers | gmetais |
| Keywords | image, webperf, performance, analyze, phantomas, test |

## Links

- npm: https://www.npmjs.com/package/analyze-image
- Repository: https://github.com/gmetais/analyze-image
- Homepage: https://github.com/gmetais/analyze-image#readme
- Issues: https://github.com/gmetais/analyze-image/issues
- npm.io page: https://npm.io/package/analyze-image

## Dependencies (11)

- [debug](https://npm.io/package/debug.md) 4.3.4
- [sharp](https://npm.io/package/sharp.md) 0.32.4
- [is-svg](https://npm.io/package/is-svg.md) 3.0.0
- [srcset](https://npm.io/package/srcset.md) 4.0.0
- [imagemin](https://npm.io/package/imagemin.md) 7.0.1
- [file-type](https://npm.io/package/file-type.md) 16.5.3
- [htmlparser2](https://npm.io/package/htmlparser2.md) 9.0.0
- [imagemin-svgo](https://npm.io/package/imagemin-svgo.md) 9.0.0
- [media-query-fns](https://npm.io/package/media-query-fns.md) 1.4.0
- [imagemin-gifsicle](https://npm.io/package/imagemin-gifsicle.md) 7.0.0
- [css-calc-transform](https://npm.io/package/css-calc-transform.md) 1.0.0

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2023-08-16

## README

# analyze-image
Analyzes an image for improvements on a webpage

Deeply inspired by https://github.com/macbre/analyze-css

## Promise example

```js
const analyzeImage = require('analyze-image');
const browserData = {
    html: '<img srcset="image1.jpg 400w, image2.jpg 800w" sizes="50vw">'
    displayWidth: 300,
    displayHeight: 200,
    viewportWidth: 1200,
    viewportHeight: 800
};
const options = {
    // ...
};

const file = // An image file as a Buffer or a String (base64 encoded image or textual SVG image)
// Supported formats are jpg, png, webp, avif, gif, svg

analyzeImage(image, browserData, options)

    .then(function(result) {
        console.log("SUCESS:");
        console.log(result);
    })

    .catch(function(error) {
        console.log("ERROR:");
        console.log(error);
    });
```

## Async/await example

```js
const analyzeImage = require('analyze-image');
const browserData = {
    html: '<img src="image.jpg">'
    displayWidth: 300,
    displayHeight: 200
};
const options = {
    // ...
};
const file = // An image file as a Buffer or a String (base64 encoded image or textual SVG image)

try {
    const result = await analyzeImage(image, browserData, options);
    console.log("SUCESS:");
    console.log(result);

} catch(error) {
    console.log("ERROR:");
    console.log(error);
}



```

## Browser data

| Data name | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| html | `string` |  | html code for the `img` or `picture` element, can be retrived in JS with `imageElement.outerHTML` |
| displayWidth | `number` |  | number of "CSS pixels" the image is displayed on, can be retrieved in JS with `imageElement.width` |
| displayHeight | `number` |  | number of "CSS pixels" the image is displayed on, can be retrieved in jS with `imageElement.height` |
| viewportWidth | `number` |  | number of "CSS pixels" of the browser's window, can be retrieved in JS with `window.innerWidth` |
| viewportHeight | `number` |  | number of "CSS pixels" of the browser's window, can be retrieved in JS with `window.innerHeight` |
| dpr | `number` | `1` | the browser's device pixel ratio, can be retrieved in JS with `window.devicePixelRatio` |


## Options

| Option name | Type | Default | Description |
| ----------- | ---- | ------- | ----------- |
| jpegQuality | `number` | `85` | quality used for JPEG recompression, integer 1-100 |
| pngQuality | `number` | `90` | quality used for PNG recompression, integer 1-100 |
| webpQuality | `number` | `82` | quality used for WebP recompression, integer 1-100 |
| avifQuality | `number` | `64` | quality used for AVIF recompression, integer 1-100 |
| gifsicleLevel | `number` | `3` | level of GIF compression effort sent to [Gifsicle](https://github.com/imagemin/imagemin-gifsicle) (1 is average but fast, 2 is medium, 3 is efficient but slow) |


## Result object

Result is an object with the following properties:

```js
{
    // Contains various data extracted from the original image
    stats: {
        format: // [String] Name of the image format detected (`jpg`, `png`, `webp`, `avif`, `gif`, `svg`)
        mymeType: // [string] Official mime type of the image format (i.e. `image/jpeg`)
        fileSize: // [number] Number of bytes of the provided image
        width: // [number] Provided image's width in pixel
        height: // [number] Provided image's height in pixel
        displayRatio: // [number] Provided image's dimensions divided by the dimensions of the physical pixels the image is displayed on. The image is too large if > 1, too small if < 1
        displayDensity: // [number] Provided image's dimensions divided by the dimensions of the CSS pixels the image is displayed on. Basically, displayDensity = displayRatio x dpr
    },
    // Contains the various transformations tested on the image
    transforms: {
        // Property only available if the image was successfuly optimized
        optimized: {
            fileSize: // [number] Weight in bytes of the original file
            newFileSize: // [number] Weight in bytes after optimization
            body: // [buffer] Optimized file
        },
        // Property only available if resizing the image provides good results (requires `displayWidth` and `displayHeight` inputs)
        resized: {
            naturalWidth: // [number] Provided image's width in pixel
            naturalHeight: // [number] Provided image's height in pixel
            fileSize: // [number] Weight in bytes of the image before resizing. Using the optimized weight if available, because it makes no sense comparing rescaled+optimized vs unoptimized.
            newWidth: // [number] New file's width in pixel
            newHeight: // [number] New file's height in pixel
            dpr: // [numer] Screen density used to determine new dimensions
            newFileSize: // [number] Weight in bytes after resizing
            body: // [buffer] Resized file
        },
        // Property only available if converting the image to WebP provides good results
        webpEncoded: {
            currentFormat: // [string] Type of the original file
            fileSize: // [number] Weight in bytes of the original file
            newFileSize: // [number] Weight in bytes after conversion
            body: // [buffer] WebP file
        },
        // Property only available if converting the image to AVIF provides good results
        avifEncoded: {
            currentFormat: // [string] Type of the original file
            fileSize: // [number] Weight in bytes of the original file
            newFileSize: // [number] Weight in bytes after conversion
            body: // [buffer] AVIF file
        }
    },
    // Contains the various transformations tested on the image
    offenders: {
        // Appears only if the image could be significantly smaller with a better compression
        imageNotOptimized: {
            fileSize: // [number] Weight in bytes of the original file
            newFileSize: // [number] Weight in bytes after optimization
        },
        // Appears only if the image could be significantly smaller when resized to fit tis display dimensions
        imageScaledDown: {
            naturalWidth: // [number] Provided image's width in pixel
            naturalHeight: // [number] Provided image's height in pixel
            fileSize: // [number] Weight in bytes of the image before resizing. Using the optimized weight if available, because it makes no sense comparing rescaled+optimized vs unoptimized.
            newWidth: // [number] New file's width in pixel
            newHeight: // [number] New file's height in pixel
            dpr: // [numer] Screen density used to determine new dimensions
            newFileSize: // [number] Weight in bytes after resizing
        },
        // Appears only if the image density is >2.2 on a high density screen (>2.2 dpr)
        imageExcessiveDensity: {
            alreadyResized: // [boolean] If true, means that we are using the resized file as a reference, otherwise it is the optimized image or the original image
            referenceDensity: // [number] The density we are comparing from
            fileSize: // [number] Weight of the reference image
            recommendedMaxDensity: // [number] The density above which the human eye hardly sees a difference
            recommendedWidth: // [number] Corresponding dimensions in pixels
            recommendedHeight: // [number] Corresponding dimensions in pixels
            newFileSize: // [number] Weight in bytes after resizing

        },
        // Appears only if the image could be significantly smaller if re-encoded in a new format (WebP or AVIF)
        imageOldFormat: {
            currentFormat: // [string] Type of the original file
            fileSize: // [number] Weight in bytes of the original file
            newFileSize: // [number] Weight in bytes of the best found format
            webpSize: // [number] Weight in bytes in WebP
            avifSize: // [number] Weight in bytes in AVIF
        },
        // Appears only if the sizes parameter on a responsive image is more than 10% smaller or larger compared to the onscreen dimensions (only for images with a "w" srcset)
        imageWithIncorrectSizesParam: {
            sizesAttribute: // [string] The sizes attribute extracted from the HTML
            convertedInPx: // [number] The number of pixels calculated from the sizes attribute
            displayWidth: // [number] The number of pixels the image is displayed on (same as browserdata.displayWidth)
        }
    },
    generator: // [string] Name and version of the current tool (i.e. `analyze-image vX.X.X`)
}
````


## Error codes

| 252 | the provided image is empty |
| 253 | the provided image is not a valid image |

---
_Source: https://npm.io/package/analyze-image · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
