-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
107 lines (95 loc) · 3.62 KB
/
scripts.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
document.getElementById('uploadContainer').addEventListener('click', () => {
document.getElementById('fileInput').click();
});
document.getElementById('fileInput').addEventListener('change', handleFileSelect);
function handleFileSelect(event) {
const file = event.target.files[0];
if (file) {
if (file.type === 'image/heic' || file.type === 'image/heif') {
convertHeicToJpeg(file);
} else {
displayImage(file);
}
}
}
function convertHeicToJpeg(file) {
heic2any({
blob: file,
toType: "image/jpeg",
}).then(function (result) {
const convertedFile = new Blob([result], { type: "image/jpeg" });
displayImage(convertedFile);
}).catch(function (error) {
console.error("Error converting HEIC to JPEG:", error);
});
}
function displayImage(file) {
const reader = new FileReader();
reader.onload = function (e) {
const img = document.getElementById('selectedImage');
img.src = e.target.result;
img.style.display = 'block';
extractExifData(file);
};
reader.readAsDataURL(file);
}
function extractExifData(file) {
const exifDataContainer = document.getElementById('exifDataContainer');
const exifDataElement = document.getElementById('exifData');
const mapButton = document.getElementById('mapButton');
EXIF.getData(file, function () {
const allMetaData = EXIF.getAllTags(this);
if (Object.keys(allMetaData).length === 0) {
exifDataElement.innerHTML = '<p>We couldn\'t find any EXIF metadata for this image. Please try another image.</p>';
exifDataContainer.style.display = 'block';
mapButton.style.display = 'none';
} else {
const formattedData = formatExifData(allMetaData);
exifDataElement.innerHTML = formattedData;
exifDataContainer.style.display = 'block';
// Check for GPS data and display the map button if present
const lat = EXIF.getTag(this, "GPSLatitude");
const lon = EXIF.getTag(this, "GPSLongitude");
if (lat && lon) {
const latRef = EXIF.getTag(this, "GPSLatitudeRef") || "N";
const lonRef = EXIF.getTag(this, "GPSLongitudeRef") || "W";
const latitude = convertDMSToDD(lat, latRef);
const longitude = convertDMSToDD(lon, lonRef);
mapButton.style.display = 'block';
mapButton.onclick = function () {
const mapUrl = `https://www.google.com/maps/search/?api=1&query=${latitude},${longitude}`;
window.open(mapUrl, '_blank');
};
} else {
mapButton.style.display = 'none';
}
}
});
}
function formatExifData(data) {
let table = '<table><tr><th>Tag</th><th>Value</th></tr>';
for (const tag in data) {
if (data.hasOwnProperty(tag)) {
let value = Array.isArray(data[tag]) ? data[tag].join(', ') : data[tag];
if (tag === 'MakerNote') {
value = truncate(value, 100); // Truncate MakerNote value to 100 characters
}
table += `<tr><td>${tag}</td><td>${value}</td></tr>`;
}
}
table += '</table>';
return table;
}
function truncate(str, n) {
return (str.length > n) ? str.substr(0, n - 1) + '…' : str;
}
function convertDMSToDD(dms, ref) {
const degrees = dms[0];
const minutes = dms[1];
const seconds = dms[2];
let dd = degrees + (minutes / 60) + (seconds / 3600);
if (ref === "S" || ref === "W") {
dd = dd * -1;
}
return dd;
}