Auto setting width and height of img tag
npm install posthtml-img-autosizePostHTML plugin that automatically sets width and height of .
It supports JPG, PNG, GIF, BMP, TIFF, SVG, and WebP.
It autosizes both local and remote images.
width="auto" and height="auto":``js
var posthtml = require('posthtml');
posthtml([require('posthtml-img-autosize')()])
.process('
')
.then(function (result) {
console.log(result.html);
});
// 
// 
`
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('
')
.then(function (result) {
console.log(result.html);
});
// 
// 
`
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('
')
.then(function (result) {
console.log(result.html);
});// 
`Without that option the plugin would search for a file with name
photo.png?v=2 on your disk.$3
You can use the usual Promise.catch() to handle errors:`js
posthtml([require('posthtml-img-autosize')()])
.process('
')
.then(function (result) {
// ...
})
.catch(function (error) {
console.log(error.message);
});// ENOENT: no such file or directory, open '/notExists.jpg'
``