-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcropper.html
More file actions
229 lines (214 loc) · 8.15 KB
/
cropper.html
File metadata and controls
229 lines (214 loc) · 8.15 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Cropper Tool</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/cropperjs@1.5.13/dist/cropper.min.css" rel="stylesheet">
<style>
#imagePreview {
max-width: 100%;
max-height: 500px;
}
</style>
</head>
<body class="p-4">
<div class="container">
<h2 class="mb-4">Image Cropper Tool</h2>
<button id="authButton" class="btn btn-outline-secondary mb-3" disabled>Connect Google Drive</button>
<input type="file" id="imageInput" class="form-control mb-3" accept="image/*">
<div class="text-center">
<img id="imagePreview" class="img-fluid" style="display:none;">
</div>
<div class="mt-3 d-flex justify-content-between align-items-center">
<button id="cropButton" class="btn btn-primary" disabled>Crop & Download</button>
<div>
<button id="driveButton" class="btn btn-outline-warning ms-2" disabled>Upload to Drive</button>
<a id="openDriveButton" class="btn btn-outline-info ms-2" target="_blank" style="display:none;">Open in Drive</a>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/cropperjs@1.5.13/dist/cropper.min.js"></script>
<script>
let cropper;
let croppedBlob = null;
let uploadedFileId = null;
const imageInput = document.getElementById('imageInput');
const imagePreview = document.getElementById('imagePreview');
const cropButton = document.getElementById('cropButton');
const driveButton = document.getElementById('driveButton');
const openDriveButton = document.getElementById('openDriveButton');
const authButton = document.getElementById('authButton');
// Google Drive integration
const CLIENT_ID = '1001017008332-peajlj58k24flbk6q9b8s618mf934uv3.apps.googleusercontent.com'; // for production
const SCOPES = 'https://www.googleapis.com/auth/drive.file';
let tokenClient;
let accessToken = null;
let gapiInited = false;
let gisInited = false;
function gapiLoaded() {
gapi.load('client', initializeGapiClient);
}
async function initializeGapiClient() {
await gapi.client.init({});
gapiInited = true;
maybeEnableAuth();
}
function gisLoaded() {
tokenClient = google.accounts.oauth2.initTokenClient({
client_id: CLIENT_ID,
scope: SCOPES,
callback: ''
});
gisInited = true;
maybeEnableAuth();
}
function maybeEnableAuth() {
if (gapiInited && gisInited) {
authButton.disabled = false;
}
}
authButton.onclick = handleAuthClick;
function handleAuthClick() {
if (accessToken === null) {
tokenClient.callback = (resp) => {
if (resp.error !== undefined) {
console.error(resp);
return;
}
accessToken = resp.access_token;
authButton.textContent = 'Disconnect Google Drive';
};
tokenClient.requestAccessToken({ prompt: 'consent' });
} else {
google.accounts.oauth2.revoke(accessToken, () => {
accessToken = null;
authButton.textContent = 'Connect Google Drive';
});
}
}
imageInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
imagePreview.src = e.target.result;
imagePreview.style.display = 'block';
if (cropper) cropper.destroy();
cropper = new Cropper(imagePreview, {
aspectRatio: NaN,
viewMode: 1
});
cropButton.disabled = false;
};
reader.readAsDataURL(file);
}
});
cropButton.addEventListener('click', () => {
if (cropper) {
const canvas = cropper.getCroppedCanvas();
canvas.toBlob((blob) => {
croppedBlob = blob;
uploadedFileId = null;
driveButton.textContent = 'Upload to Drive';
driveButton.disabled = false;
openDriveButton.style.display = 'none';
openDriveButton.removeAttribute('href');
const link = document.createElement('a');
link.download = 'cropped-image.png';
link.href = canvas.toDataURL();
link.click();
}, 'image/png');
}
});
async function ensureItabuFolder() {
const q = "name='itabu-tools' and mimeType='application/vnd.google-apps.folder' and trashed=false";
const resp = await fetch(`https://www.googleapis.com/drive/v3/files?q=${encodeURIComponent(q)}&fields=files(id)`, {
headers: new Headers({ 'Authorization': 'Bearer ' + accessToken })
});
const data = await resp.json();
if (data.files && data.files.length > 0) {
return data.files[0].id;
}
const createResp = await fetch('https://www.googleapis.com/drive/v3/files', {
method: 'POST',
headers: new Headers({
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
}),
body: JSON.stringify({
name: 'itabu-tools',
mimeType: 'application/vnd.google-apps.folder'
})
});
const createData = await createResp.json();
return createData.id;
}
driveButton.addEventListener('click', async () => {
if (!croppedBlob) return;
if (!accessToken) {
alert('Please connect your Google Drive account first.');
return;
}
driveButton.disabled = true;
if (!uploadedFileId) {
try {
const folderId = await ensureItabuFolder();
const metadata = { name: `cropped-image-${Date.now()}.png`, mimeType: 'image/png', parents: [folderId] };
const form = new FormData();
form.append('metadata', new Blob([JSON.stringify(metadata)], { type: 'application/json' }));
form.append('file', croppedBlob);
const resp = await fetch('https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id', {
method: 'POST',
headers: new Headers({ 'Authorization': 'Bearer ' + accessToken }),
body: form
});
const data = await resp.json();
if (!resp.ok || !data.id) {
throw new Error(data.error?.message || 'Upload failed');
}
uploadedFileId = data.id;
driveButton.textContent = 'Remove from Drive';
openDriveButton.href = `https://drive.google.com/file/d/${uploadedFileId}/view`;
openDriveButton.style.display = 'inline-block';
} catch (err) {
console.error('Upload failed', err);
alert('Failed to upload');
} finally {
driveButton.disabled = false;
}
} else {
try {
const resp = await fetch(`https://www.googleapis.com/drive/v3/files/${uploadedFileId}`, {
method: 'DELETE',
headers: new Headers({ 'Authorization': 'Bearer ' + accessToken })
});
if (!resp.ok) {
throw new Error('Delete failed');
}
uploadedFileId = null;
driveButton.textContent = 'Upload to Drive';
openDriveButton.style.display = 'none';
openDriveButton.removeAttribute('href');
} catch (err) {
console.error('Delete failed', err);
alert('Failed to delete');
} finally {
driveButton.disabled = false;
}
}
});
</script>
<script src="https://apis.google.com/js/api.js" onload="gapiLoaded()" async defer></script>
<script src="https://accounts.google.com/gsi/client" onload="gisLoaded()" async defer></script>
</body>
<footer style="text-align: center; font-size: 0.9em; margin-top: 2em; padding: 1em 0; color: #555;">
<p>
<a href="https://tools.itabu.xyz/" style="margin-right: 1em;">Home</a>
<a href="terms.html" target="_blank" style="margin-right: 1em;">Terms of Service</a>
<a href="privacy.html" target="_blank">Privacy Policy</a>
</p>
<p style="margin-top: 0.5em;">© 2025 i-Tabu Tool</p>
</footer>
</html>