Skip to content

Commit bac6f22

Browse files
committed
Add image-size functions. Fixes #1378
1 parent 4ddcf63 commit bac6f22

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

lib/less-node/image-size.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var Dimension = require("../less/tree/dimension"),
2+
Expression = require("../less/tree/expression"),
3+
functionRegistry = require("./../less/functions/function-registry"),
4+
path = require("path");
5+
6+
function imageSize(filePathNode) {
7+
var filePath = filePathNode.value;
8+
var currentDirectory = filePathNode.currentFileInfo.relativeUrls ?
9+
filePathNode.currentFileInfo.currentDirectory : filePathNode.currentFileInfo.entryPath;
10+
11+
var sizeOf = require('image-size');
12+
filePath = path.join(currentDirectory, filePath);
13+
return sizeOf(filePath);
14+
}
15+
16+
var imageFunctions = {
17+
"image-size": function(filePathNode) {
18+
var size = imageSize(filePathNode);
19+
return new Expression([
20+
new Dimension(size.width, "px"),
21+
new Dimension(size.height, "px")
22+
]);
23+
},
24+
"image-width": function(filePathNode) {
25+
var size = imageSize(filePathNode);
26+
return new Dimension(size.width, "px");
27+
},
28+
"image-height": function(filePathNode) {
29+
var size = imageSize(filePathNode);
30+
return new Dimension(size.height, "px");
31+
}
32+
};
33+
34+
functionRegistry.addMultiple(imageFunctions);

lib/less-node/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@ less.writeError = function (ctx, options) {
6868
console.error(less.formatError(ctx, options));
6969
};
7070

71+
// provide image-size functionality
72+
require('./image-size');
73+
7174
module.exports = less;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"request": "^2.48.0",
4646
"mkdirp": "^0.5.0",
4747
"source-map": "^0.1.x",
48-
"promise": "^6.0.1"
48+
"promise": "^6.0.1",
49+
"image-size": "~0.3.5"
4950
},
5051
"devDependencies": {
5152
"diff": "^1.0",

test/css/urls.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@
6666
uri-1: url("data:text/html,%3Ch1%3EThis%20page%20is%20100%25%20Awesome.%3C%2Fh1%3E%0A");
6767
uri-2: url("data:text/html,%3Ch1%3EThis%20page%20is%20100%25%20Awesome.%3C%2Fh1%3E%0A");
6868
}
69-
#data-uri-toobig {
69+
#file-functions {
7070
uri: url('../data/data-uri-fail.png');
7171
svg-not-base-64: url("data:image/svg+xml,%3Csvg%20height%3D%22100%22%20width%3D%22100%22%3E%0D%0A%20%20%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2240%22%20stroke%3D%22black%22%20stroke-width%3D%221%22%20fill%3D%22blue%22%20%2F%3E%0D%0A%3C%2Fsvg%3E");
72+
size: 640px 430px;
73+
width: 640px;
74+
height: 430px;
7275
}
7376
#svg-functions {
7477
background-image: url('data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20%3F%3E%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20width%3D%22100%25%22%20height%3D%22100%25%22%20viewBox%3D%220%200%201%201%22%20preserveAspectRatio%3D%22none%22%3E%3ClinearGradient%20id%3D%22gradient%22%20gradientUnits%3D%22userSpaceOnUse%22%20x1%3D%220%25%22%20y1%3D%220%25%22%20x2%3D%220%25%22%20y2%3D%22100%25%22%3E%3Cstop%20offset%3D%220%25%22%20stop-color%3D%22%23000000%22%2F%3E%3Cstop%20offset%3D%22100%25%22%20stop-color%3D%22%23ffffff%22%2F%3E%3C%2FlinearGradient%3E%3Crect%20x%3D%220%22%20y%3D%220%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22url(%23gradient)%22%20%2F%3E%3C%2Fsvg%3E');

test/less/urls.less

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@
5757
uri-2: data-uri('../data/page.html');
5858
}
5959

60-
#data-uri-toobig {
60+
#file-functions {
6161
uri: data-uri('../data/data-uri-fail.png');
6262
svg-not-base-64: data-uri('../data/image.svg');
63+
size: image-size('../data/data-uri-fail.png');
64+
width: image-width('../data/data-uri-fail.png');
65+
height: image-height('../data/data-uri-fail.png');
6366
}
6467
.add_an_import(@file_to_import) {
6568
@import "@{file_to_import}";

0 commit comments

Comments
 (0)