grunt-image-size
Retrieve image size information. The image-size port for grunt.
Getting Started
This plugin requires Grunt.
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-image-size --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt;
The "image_size" task
Overview
This task produces a vector of an image size data from all provided source files:
name : 'path/to/file' width : 100 height : 100 ...
And outputs it to grunt Config Data and/or to dest file (as json).
Options
options.configObject
Type: String
Default: undefined
A string value that is used as a name for grunt config object for outputing data.
options.processName
Type: Function
Default: undefined
Allows to alter name (file path) of each src
.
Will be invoked with injected values for following arguments:
src
— currentsrc
;file
—file
of currentsrc
.
this
will correspond to the current file
of the task.
Should return a String
.
options.processEntry
Type: Function
Default: undefined
Allows to alter formed data of each src
.
Will be invoked with injected values for following arguments:
entry
— originalsrc
data;src
— file path of currentsrc
;file
—file
of currentsrc
.
this
will correspond to the current file
.
Should return an Object
.
options.processSizes
Type: Function
Default: undefined
Allows to alter final data.
Result of this function will be used for printing Object to dest
file.
Will be invoked with injected values for following arguments:
sizes
— extracted and optionally processed final data;file
— currentfile
instance.
this
will correspond to the current file
of the task.
Should return whatever you want to print as a result.
options.replacer
Type: Function|String[]|Number[]
Default: null
Pass replacer parameter to JSON.stringify()
.
options.space
Type: String|Integer
Default: 2
Pass space parameter to JSON.stringify()
.
Controls indentation for outputted JSON file.
Usage Examples
After processing all images from img/
script writes size information to image_data
config object and image_data.json file.
grunt
From image_data
it can be accessed with grunt.config.get('image_data')
or via template <%= image_data %>
Development and testing
Please, lint and test the code with npm test
before posting PRs.
Got some lint errors? Run npm run format
.
Tests failing? Fix the code to make them pass. Use npm run test:watch
for easier workflow.
License
Copyright (c) 2014 SAPer. Licensed under the MIT license.