-
-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathimage-dimensions.js
More file actions
41 lines (34 loc) · 1.31 KB
/
image-dimensions.js
File metadata and controls
41 lines (34 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const Handlebars = require('handlebars');
const sizeOf = require('./../../../../helpers/rotation-aware-sizeof.js');
const path = require('path');
const normalizePath = require('normalize-path');
/**
* Helper for creating width/height attributes from the provided image
*
* {{imageDimensions @config.custom.imageOptionName}}
*
* @returns {string} - string with width and height attributes based on a given image
*/
function imageDimensionsHelper(rendererInstance, Handlebars) {
Handlebars.registerHelper('imageDimensions', function (url) {
if (!url) {
return '';
}
url = normalizePath(url);
let basicUrl = normalizePath(rendererInstance.siteConfig.domain);
let basicDir = normalizePath(rendererInstance.inputDir);
let imagePath = url.replace(basicUrl, '');
imagePath = path.join(basicDir, imagePath);
let output = '';
try {
let dimensions = sizeOf(imagePath);
if(dimensions) {
output = ' width="' + dimensions.width + '" height="' + dimensions.height + '" ';
}
} catch(e) {
console.log('Image dimensions HSB helper: wrong image path - missing dimensions');
}
return new Handlebars.SafeString(output);
});
}
module.exports = imageDimensionsHelper;