Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 30 additions & 6 deletions packages/less/lib/less-node/image-size.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createRequire } from 'module';
import fs from 'fs';
import Dimension from '../less/tree/dimension.js';
import Expression from '../less/tree/expression.js';
import functionRegistry from './../less/functions/function-registry.js';
Expand Down Expand Up @@ -33,27 +34,50 @@ export default environment => {
throw fileSync.error;
}

const sizeOf = require('image-size');
return sizeOf ? sizeOf(fileSync.filename) : {width: 0, height: 0};
let probe;
try {
probe = require('probe-image-size/sync');
} catch (_) {
return { width: 0, height: 0 };
}

// Read the file as raw bytes via its resolved filename to ensure binary
// integrity — the file manager may return string contents for text-mode
// reads which corrupts binary image data and causes probe to return null.
const contents = fs.readFileSync(fileSync.filename);

const size = probe(contents);

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
if (!size) {
throw {
type: 'File',
message: `Unrecognised image format for '${filePath}'`
};
}

return {
width: size.width,
height: size.height
};
}

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

functionRegistry.addMultiple(imageFunctions);
};
};
2 changes: 1 addition & 1 deletion packages/less/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"optionalDependencies": {
"errno": "^0.1.1",
"graceful-fs": "^4.1.2",
"image-size": "~0.5.0",
"probe-image-size": "^7.2.3",
"make-dir": "^5.1.0",
"mime": "^1.4.1",
"needle": "^3.1.0",
Expand Down
Loading
Loading