# posthtml-img-autosize

> Auto setting width and height of img tag

Latest version **0.1.6** (published 2020-10-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install posthtml-img-autosize
pnpm add posthtml-img-autosize
yarn add posthtml-img-autosize
bun add posthtml-img-autosize
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.6 |
| Published | 2020-10-09 |
| First published | 2016-01-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 111.2 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 28 |
| Author | Kirill Maltsev |
| Maintainers | scrum, maltsev |
| Keywords | posthtml, posthtml-plugin, html, postproccessor, autosize, img, image |

## Links

- npm: https://www.npmjs.com/package/posthtml-img-autosize
- Repository: https://github.com/posthtml/posthtml-img-autosize
- Issues: https://github.com/posthtml/posthtml-img-autosize/issues
- npm.io page: https://npm.io/package/posthtml-img-autosize

## Dependencies (2)

- [image-size](https://npm.io/package/image-size.md) ^0.9.1
- [normalize-path](https://npm.io/package/normalize-path.md) ^3.0.0

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 0.1.6 (latest) — 2020-10-09
- 0.1.5 — 2020-06-08
- 0.1.4 — 2019-03-20
- 0.1.3 — 2018-06-28
- 0.1.2 — 2018-02-25
- 0.1.1 — 2016-01-19
- 0.1.0 — 2016-01-04

## README

# posthtml-img-autosize [![npm version](https://badge.fury.io/js/posthtml-img-autosize.svg)](http://badge.fury.io/js/posthtml-img-autosize) [![Build Status](https://travis-ci.org/posthtml/posthtml-img-autosize.svg?branch=master)](https://travis-ci.org/posthtml/posthtml-img-autosize)

[PostHTML](https://github.com/posthtml/posthtml) plugin that automatically sets `width` and `height` of `<img>`.
It supports JPG, PNG, GIF, BMP, TIFF, SVG, and WebP.
It autosizes both local and remote images.


## Usage
By default the plugin will autosize only images with `width="auto"` and `height="auto"`:

```js
var posthtml = require('posthtml');

posthtml([require('posthtml-img-autosize')()])
    .process('<img src="photo.png" width="auto" height="auto"><img src="user.jpg">')
    .then(function (result) {
        console.log(result.html);
    });

// <img src="photo.png" width="111" height="52">
// <img src="user.jpg">
```


But if you set `processEmptySize: true`, the plugin will autosize all images with undefined or empty `width` and `height`:
```js
posthtml([
        require('posthtml-img-autosize')({
            root: './', // Path to images base directory (default: './')
            processEmptySize: true
        })
    ])
    .process('<img src="photo.png" width="auto" height="auto"><img src="user.jpg">')
    .then(function (result) {
        console.log(result.html);
    });

// <img src="photo.png" width="111" height="52">
// <img src="user.jpg" width="100" height="201">
```


### Image versioning
If you use `?..` for image versioning in your HTML you should set `questionMarkAsVersion: true` in the config:
```js
posthtml([
        require('posthtml-img-autosize')({
            questionMarkAsVersion: true
        })
    ])
     // The image file has "photo.png" name
    .process('<img src="photo.png?v=2" width="auto" height="auto">')
    .then(function (result) {
        console.log(result.html);
    });

// <img src="photo.png?v=2" width="111" height="52">
```

Without that option the plugin would search for a file with name `photo.png?v=2` on your disk.



### Error handling
You can use the usual `Promise.catch()` to handle errors:

```js
posthtml([require('posthtml-img-autosize')()])
    .process('<img src="notExists.jpg" width="auto" height="auto">')
    .then(function (result) {
        // ...
    })
    .catch(function (error) {
        console.log(error.message);
    });

// ENOENT: no such file or directory, open '/notExists.jpg'
```

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