Sha256: a05103ddc07109c73bedbb38bae7462516fb91db2f5a5bc4d77ef3c0e57537b9

Contents?: true

Size: 1.15 KB

Versions: 39

Compression:

Stored size: 1.15 KB

Contents

var utils = require('../utils')
  , nodes = require('../nodes')
  , Image = require('./image');

/**
 * Return the width and height of the given `img` path.
 *
 * Examples:
 *
 *    image-size('foo.png')
 *    // => 200px 100px
 *
 *    image-size('foo.png')[0]
 *    // => 200px
 *
 *    image-size('foo.png')[1]
 *    // => 100px
 *
 * Can be used to test if the image exists,
 * using an optional argument set to `true`
 * (without this argument this function throws error
 * if there is no such image).
 *
 * Example:
 *
 *    image-size('nosuchimage.png', true)[0]
 *    // => 0
 *
 * @param {String} img
 * @param {Boolean} ignoreErr
 * @return {Expression}
 * @api public
 */

module.exports = function imageSize(img, ignoreErr) {
  utils.assertType(img, 'string', 'img');
  try {
    var img = new Image(this, img.string);
  } catch (err) {
    if (ignoreErr) {
      return [new nodes.Unit(0), new nodes.Unit(0)];
    } else {
      throw err;
    }
  }

  // Read size
  img.open();
  var size = img.size();
  img.close();

  // Return (w h)
  var expr = [];
  expr.push(new nodes.Unit(size[0], 'px'));
  expr.push(new nodes.Unit(size[1], 'px'));

  return expr;
};

Version data entries

39 entries across 20 versions & 2 rubygems

Version Path
ela-4.1.6 node_modules/stylus/lib/functions/image-size.js
ela-4.1.6 node_modules/nib/node_modules/stylus/lib/functions/image-size.js
ela-4.1.5 node_modules/stylus/lib/functions/image-size.js
ela-4.1.5 node_modules/nib/node_modules/stylus/lib/functions/image-size.js
ela-4.1.4 node_modules/nib/node_modules/stylus/lib/functions/image-size.js
ela-4.1.4 node_modules/stylus/lib/functions/image-size.js
ela-4.1.3 node_modules/stylus/lib/functions/image-size.js
ela-4.1.3 node_modules/nib/node_modules/stylus/lib/functions/image-size.js
ela-4.1.2 node_modules/stylus/lib/functions/image-size.js
ela-4.1.2 node_modules/nib/node_modules/stylus/lib/functions/image-size.js
ela-4.1.1 node_modules/stylus/lib/functions/image-size.js
ela-4.1.1 node_modules/nib/node_modules/stylus/lib/functions/image-size.js
ela-4.1.0 node_modules/stylus/lib/functions/image-size.js
ela-4.1.0 node_modules/nib/node_modules/stylus/lib/functions/image-size.js
ela-4.0.0 node_modules/stylus/lib/functions/image-size.js
ela-4.0.0 node_modules/nib/node_modules/stylus/lib/functions/image-size.js
ela-3.4.3 node_modules/stylus/lib/functions/image-size.js
ela-3.4.3 node_modules/nib/node_modules/stylus/lib/functions/image-size.js
ela-3.4.2 node_modules/nib/node_modules/stylus/lib/functions/image-size.js
ela-3.4.2 node_modules/stylus/lib/functions/image-size.js