-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathutils.js
65 lines (59 loc) · 2.06 KB
/
utils.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
function resizeImage(file, maxWidth) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.onload = function () {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const scaleFactor = maxWidth / img.width;
canvas.width = maxWidth;
canvas.height = img.height * scaleFactor;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
canvas.toBlob((blob) => {
resolve(new File([blob], file.name, { type: file.type }));
}, file.type);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
});
}
function uploadImageToCloudinary(imageFile) {
const cloudName = localStorage.getItem('cloudName'); // Replace with your Cloudinary cloud name
const uploadPreset = localStorage.getItem('uploadPreset'); // Replace with your Cloudinary upload preset
return resizeImage(imageFile, 500).then((resizedFile) => {
const formData = new FormData();
formData.append('file', resizedFile);
formData.append('upload_preset', uploadPreset);
return fetch(`https://api.cloudinary.com/v1_1/${cloudName}/image/upload`, {
method: 'POST',
body: formData,
})
.then((response) => response.json())
.then((data) => {
console.log(data);
if (data.secure_url) {
return data.secure_url;
} else {
throw new Error('Failed to upload image to Cloudinary');
}
});
});
}
function showSettings() {
document.getElementById('mainContent').style.display = 'none';
document.getElementById('settingsContent').style.display = 'block';
}
function hideSettings() {
document.getElementById('mainContent').style.display = 'block';
document.getElementById('settingsContent').style.display = 'none';
}
function toggleSettings() {
const settingsContent = document.getElementById('settingsContent');
if (settingsContent.style.display === 'none') {
showSettings();
} else {
hideSettings();
}
}