├── .gitignore
├── LICENSE
├── README.md
├── demo
├── app.js
└── index.html
├── package.json
└── src
├── .babelrc
├── .eslintrc.js
└── index.js
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 |
6 | # Runtime data
7 | pids
8 | *.pid
9 | *.seed
10 |
11 | # Directory for instrumented libs generated by jscoverage/JSCover
12 | lib-cov
13 |
14 | # Coverage directory used by tools like istanbul
15 | coverage
16 |
17 | # nyc test coverage
18 | .nyc_output
19 |
20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21 | .grunt
22 |
23 | # node-waf configuration
24 | .lock-wscript
25 |
26 | # Compiled binary addons (http://nodejs.org/api/addons.html)
27 | build/Release
28 |
29 | # Dependency directories
30 | node_modules
31 | jspm_packages
32 |
33 | # Optional npm cache directory
34 | .npm
35 |
36 | # Optional REPL history
37 | .node_repl_history
38 | demo/bundle.js
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Hector Zarco
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | #Maggie
4 | > :sunrise: Get precious image info from an input file
5 |
6 | This module does one thing right, returns information about the image selected in a html input file.
7 |
8 | ###Install
9 |
10 | ```
11 | $ npm i maggie -S
12 | ```
13 |
14 | ###Examples
15 |
16 | **Log image width and height** :fireworks:
17 |
18 | ```javascript
19 | import {getInfo} from 'maggie';
20 |
21 | const inputElement = document.getElementById('my-input');
22 |
23 | getInfo(inputElement, info => {
24 | console.log(`Image dimensions ${info.width}x${info.height}`);
25 | });
26 | ```
27 |
28 | **Preview the selected image** :ocean:
29 |
30 | ```javascript
31 | getInfo('#my-input', info => {
32 | const preview = document.getElementById('img-preview');
33 |
34 | preview.src = info.src;
35 | });
36 | ```
37 |
38 | **Get the average color** :alien:
39 |
40 | ```javascript
41 | getInfo('#my-input', info => {
42 | const data = info.imageData;
43 | const length = data.length;
44 | const channelCount = 4; //red, green, blue, alpha
45 | const colorCount = length / channelCount;
46 | let red = 0; let green = 0; let blue = 0;
47 |
48 | for (let i = 0; i < length; i += channelCount) {
49 | red += data[i];
50 | green += data[i + 1];
51 | blue += data[i + 2];
52 | }
53 |
54 | red = Math.floor(red / colorCount);
55 | green = Math.floor(green / colorCount);
56 | blue = Math.floor(blue / colorCount);
57 |
58 | console.log(red, green, blue);
59 | });
60 | ```
61 |
62 | ###Info Object
63 |
64 | The returned `info` object has the following properties
65 |
66 | | Property | Type | Description
67 | |:-------------:|:--------------:|--------------
68 | | width | Number | Image width
69 | | height | Number | Image height
70 | | src | String | Image source
71 | | element | [HTMLImageElement](https://developer.mozilla.org/en/docs/Web/API/HTMLImageElement) | Image Dom element
72 | | imageData | [ImageData](https://developer.mozilla.org/en/docs/Web/API/ImageData) | ImageData element based on the image
73 |
74 | 
75 |
76 | ###Author
77 |
78 | [@zzarcon](https://twitter.com/zzarcon) :beers:
--------------------------------------------------------------------------------
/demo/app.js:
--------------------------------------------------------------------------------
1 | import {getInfo} from '../src';
2 |
3 | const init = _ => {
4 | const input = document.getElementById('my-file');
5 | const preview = document.getElementById('preview');
6 |
7 | getInfo(input, info => {
8 | preview.src = info.src;
9 | console.log(info)
10 | });
11 | };
12 |
13 | document.addEventListener("DOMContentLoaded", init);
--------------------------------------------------------------------------------
/demo/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |