Skip to content

Commit

Permalink
Merge pull request #2526 from bassjobsen/image-size
Browse files Browse the repository at this point in the history
Image size
  • Loading branch information
lukeapage committed Apr 2, 2015
2 parents fc93876 + ea43e7d commit 2b42605
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 31 deletions.
80 changes: 51 additions & 29 deletions lib/less-node/image-size.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,57 @@
var Dimension = require("../less/tree/dimension"),
module.exports = function(environment) {
var Dimension = require("../less/tree/dimension"),
Expression = require("../less/tree/expression"),
functionRegistry = require("./../less/functions/function-registry"),
path = require("path");
functionRegistry = require("./../less/functions/function-registry");

function imageSize(functionContext, filePathNode) {
var filePath = filePathNode.value;
var currentFileInfo = functionContext.currentFileInfo;
var currentDirectory = currentFileInfo.relativeUrls ?
function imageSize(functionContext, filePathNode) {
var filePath = filePathNode.value;
var currentFileInfo = functionContext.currentFileInfo;
var currentDirectory = currentFileInfo.relativeUrls ?
currentFileInfo.currentDirectory : currentFileInfo.entryPath;

var sizeOf = require('image-size');
filePath = path.join(currentDirectory, filePath);
return sizeOf(filePath);
}

var imageFunctions = {
"image-size": function(filePathNode) {
var size = imageSize(this, filePathNode);
return new Expression([
new Dimension(size.width, "px"),
new Dimension(size.height, "px")
]);
},
"image-width": function(filePathNode) {
var size = imageSize(this, filePathNode);
return new Dimension(size.width, "px");
},
"image-height": function(filePathNode) {
var size = imageSize(this, filePathNode);
return new Dimension(size.height, "px");
var fragmentStart = filePath.indexOf('#');
var fragment = '';
if (fragmentStart !== -1) {
fragment = filePath.slice(fragmentStart);
filePath = filePath.slice(0, fragmentStart);
}

var fileManager = environment.getFileManager(filePath, currentDirectory, functionContext.context, environment, true);

if (!fileManager) {
throw {
type: "File",
message: "Can not set up FileManager for " + filePathNode
};
}

var fileSync = fileManager.loadFileSync(filePath, currentDirectory, functionContext.context, environment);

if (fileSync.error) {
throw fileSync.error;
}

var sizeOf = require('image-size');
return sizeOf(fileSync.filename);
}
};

functionRegistry.addMultiple(imageFunctions);
var imageFunctions = {
"image-size": function(filePathNode) {
var size = imageSize(this, filePathNode);
return new Expression([
new Dimension(size.width, "px"),
new Dimension(size.height, "px")
]);
},
"image-width": function(filePathNode) {
var size = imageSize(this, filePathNode);
return new Dimension(size.width, "px");
},
"image-height": function(filePathNode) {
var size = imageSize(this, filePathNode);
return new Dimension(size.height, "px");
}
};

functionRegistry.addMultiple(imageFunctions);
};
2 changes: 1 addition & 1 deletion lib/less-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ less.writeError = function (ctx, options) {
};

// provide image-size functionality
require('./image-size');
require('./image-size')(less.environment);

module.exports = less;
3 changes: 3 additions & 0 deletions test/css/include-path/include-path.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ body {
data-uri {
property: url("data:image/svg+xml,%3Csvg%20height%3D%22100%22%20width%3D%22100%22%3E%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%0A%3C%2Fsvg%3E");
}
image-size {
property: 100px 100px;
}
4 changes: 3 additions & 1 deletion test/less/include-path/include-path.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
data-uri {
property: data-uri('image.svg');
}

image-size {
property: image-size('image.svg');
}

0 comments on commit 2b42605

Please sign in to comment.