forked from cianclarke/node-gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexif.js
More file actions
84 lines (75 loc) · 2.3 KB
/
Copy pathexif.js
File metadata and controls
84 lines (75 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var ExifImage = require('exif').ExifImage;
/*
* Utility function to convert exif data into something a bit more consumable
* by a template
*/
var exif = function(staticPath, photo, callback){
// We don't care about errors in here - we can always return an undefined exif
photo.exif = undefined;
try {
new ExifImage({
image : staticPath//'resources/photos/Ireland/West Coast/_MG_4174.jpg'
}, function (error, data) {
if (error){
//console.log('[exif.js] error in ' + staticPath + ': ' + JSON.stringify(error));
photo.exif = false;
return callback(null, photo);
}else{
var exifMap = {};
var image = data.image,
exif = data.exif,
gps = data.gps,
arrays = image.concat(exif, gps);
for (var i=0; i<arrays.length; i++){
var t = arrays[i],
careAbout = { // what props we're interested in, and what we call them in output, rather than silly exif-ey names
"Make" : "Make",
"Model" : "Model",
"DateTimeOriginal" : "Time",
"ApertureValue" : "aperture",
"FocalLength" : "focalLength",
"ISOSpeedRatings" : "ISO",
"ExposureTime" : "Shutter Speed",
"GPSLatitude" : "Lat",
"GPSLongitude" : "Long",
"ImageDescription" : "Description"
};
if (careAbout.hasOwnProperty(t.tagName)){
var key = careAbout[t.tagName],
value = t.value;
if (key == "Shutter Speed"){
// Transform shutter speed to a fraction
value = dec2frac(value);
}
if (typeof value=="number"){
value = Math.round(value*100)/100; // no long decimals
}
exifMap[key] = value;
}
}
photo.exif = exifMap;
return callback(null, photo);
}
});
} catch (error) {
return callback(null, photo);
}
}
// source: http://stackoverflow.com/questions/95727/how-to-convert-floats-to-human-readable-fractions
function dec2frac(d) {
var df = 1;
var top = 1;
var bot = 1;
while (df != d) {
if (df < d) {
top += 1;
}
else {
bot += 1;
top = parseInt(d * bot);
}
df = top / bot;
}
return top + '/' + bot;
}
module.exports = exif;