Created
February 10, 2018 18:11
-
-
Save danilvalov/04f7b2bb5de91e8d02ce08eaec15cf4b to your computer and use it in GitHub Desktop.
[SASS/SCSS] `image-width` and `image-height` asset functions (mixins)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const sass = require('node-sass'); | |
const imageSizeOf = require('image-size'); | |
module.exports = { | |
'image-width($imageUrl)': function (imageUrl) { | |
const absoluteImagePath = this.options.includePaths.split(':').pop(); | |
const absoluteImageUrl = path.resolve(absoluteImagePath, imageUrl.getValue()); | |
const dimensions = imageSizeOf(absoluteImageUrl); | |
return sass.types.Number(dimensions.width, 'px'); | |
}, | |
'image-height($imageUrl)': function (imageUrl) { | |
const absoluteImagePath = this.options.includePaths.split(':').pop(); | |
const absoluteImageUrl = path.resolve(absoluteImagePath, imageUrl.getValue()); | |
const dimensions = imageSizeOf(absoluteImageUrl); | |
return sass.types.Number(dimensions.height, 'px'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment