e4677885d5efee8f9b7002a750292c5d703c4abfd968ee9e5bf2b9692a7ed13b3a0744ecd1900a28ea7cfa04d920c8f83b9769d8c7462b3410e07324708566 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # image-size
  2. [![NPM Version](https://img.shields.io/npm/v/image-size.svg)](https://www.npmjs.com/package/image-size)
  3. [![Build Status](https://travis-ci.org/image-size/image-size.svg?branch=master)](https://travis-ci.org/image-size/image-size)
  4. [![NPM Downloads](https://img.shields.io/npm/dm/image-size.svg)](http://npm-stat.com/charts.html?package=image-size&author=&from=&to=)
  5. [![Coverage Status](https://img.shields.io/coveralls/image-size/image-size/master.svg)](https://coveralls.io/github/image-size/image-size?branch=master)
  6. [![devDependency Status](https://david-dm.org/image-size/image-size/dev-status.svg)](https://david-dm.org/image-size/image-size#info=devDependencies)
  7. A [Node](https://nodejs.org/en/) module to get dimensions of any image file
  8. ## Supported formats
  9. * BMP
  10. * GIF
  11. * JPEG
  12. * PNG
  13. * PSD
  14. * TIFF
  15. * WebP
  16. * SVG
  17. * DDS
  18. ### Upcoming
  19. * SWF
  20. ## Programmatic Usage
  21. ```
  22. npm install image-size --save
  23. ```
  24. ### Synchronous
  25. ```javascript
  26. var sizeOf = require('image-size');
  27. var dimensions = sizeOf('images/funny-cats.png');
  28. console.log(dimensions.width, dimensions.height);
  29. ```
  30. ### Asynchronous
  31. ```javascript
  32. var sizeOf = require('image-size');
  33. sizeOf('images/funny-cats.png', function (err, dimensions) {
  34. console.log(dimensions.width, dimensions.height);
  35. });
  36. ```
  37. NOTE: The asynchronous version doesn't work if the input is a Buffer. Use synchronous version instead.
  38. ### Using a URL
  39. ```javascript
  40. var url = require('url');
  41. var http = require('http');
  42. var sizeOf = require('image-size');
  43. var imgUrl = 'http://my-amazing-website.com/image.jpeg';
  44. var options = url.parse(imgUrl);
  45. http.get(options, function (response) {
  46. var chunks = [];
  47. response.on('data', function (chunk) {
  48. chunks.push(chunk);
  49. }).on('end', function() {
  50. var buffer = Buffer.concat(chunks);
  51. console.log(sizeOf(buffer));
  52. });
  53. });
  54. ```
  55. You can optionally check the buffer lengths & stop downloading the image after a few kilobytes.
  56. **You don't need to download the entire image**
  57. ## Command-Line Usage (CLI)
  58. ```
  59. npm install image-size --global
  60. image-size image1 [image2] [image3] ...
  61. ```
  62. ## Credits
  63. not a direct port, but an attempt to have something like
  64. [dabble's imagesize](https://github.com/dabble/imagesize/blob/master/lib/image_size.rb) as a node module.
  65. ## [Contributors](Contributors.md)