File size: 11,654 Bytes
7a7d90f
 
 
 
 
 
 
188efea
7a7d90f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f63f523
7a7d90f
 
 
 
f63f523
7a7d90f
f63f523
 
7a7d90f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7b3974
 
 
 
 
 
 
 
 
7a7d90f
 
 
 
 
 
 
 
 
 
669a5ef
7a7d90f
 
c5ec00d
 
7a7d90f
 
 
 
 
c5ec00d
7a7d90f
 
c5ec00d
7a7d90f
 
 
 
 
 
 
 
188efea
7a7d90f
 
 
 
 
 
 
c5ec00d
 
 
 
 
 
 
 
 
7a7d90f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
669a5ef
7a7d90f
 
 
 
 
 
 
 
 
 
 
 
669a5ef
7a7d90f
 
 
 
 
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Gallery</title>
    <style>
        /* CSS remains unchanged */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            min-height: 100vh;
            background: #FAFAFA;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 2rem;
        }
        h1 {
            font-size: 1.875rem;
            font-weight: bold;
            color: #262626;
            margin-bottom: 2rem;
        }
        .upload-zone {
            background: white;
            border: 2px dashed #e2e8f0;
            border-radius: 0.5rem;
            padding: 2rem;
            margin-bottom: 2rem;
            cursor: pointer;
            transition: all 0.2s;
        }
        .upload-zone:hover,
        .upload-zone.dragging {
            border-color: #405DE6;
            background: #f8fafc;
        }
        .upload-zone-content {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 1rem;
        }
        .upload-icon {
            width: 3rem;
            height: 3rem;
            color: #405DE6;
        }
        .upload-text {
            font-size: 1.125rem;
            font-weight: 600;
        }
        .upload-subtext {
            font-size: 0.875rem;
            color: #64748b;
        }
        .select-button {
            margin-top: 1rem;
            padding: 0.5rem 1rem;
            background: white;
            border: 1px solid #e2e8f0;
            border-radius: 0.375rem;
            font-size: 0.875rem;
            cursor: pointer;
            transition: all 0.2s;
        }
        .select-button:hover {
            background: #405DE6;
            color: white;
        }
        .gallery {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 1rem;
        }
        .image-card {
            position: relative;
            background: white;
            border-radius: 0.5rem;
            overflow: hidden;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .image-card img {
            width: 100%;
            aspect-ratio: 1;
            object-fit: cover;
            cursor: pointer;
        }
        .image-overlay {
            position: absolute;
            inset: 0;
            background: rgba(0,0,0,0.4);
            opacity: 0;
            transition: opacity 0.2s;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .image-card:hover .image-overlay {
            opacity: 1;
        }
        .delete-button {
            background: white;
            border: none;
            border-radius: 9999px;
            width: 2.5rem;
            height: 2.5rem;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.2s;
        }
        .delete-button:hover {
            background: #ef4444;
            color: white;
        }
        .modal {
            position: fixed;
            inset: 0;
            background: rgba(0,0,0,0.9);
            display: none;
            align-items: center;
            justify-content: center;
            z-index: 50;
            padding: 20px;
        }
        .modal.active {
            display: flex;
        }
        .modal img {
            max-width: 95%;
            max-height: 95vh;
            object-fit: contain;
        }
        .toast {
            position: fixed;
            bottom: 1rem;
            right: 1rem;
            padding: 1rem;
            background: #262626;
            color: white;
            border-radius: 0.375rem;
            display: none;
            animation: slideIn 0.2s ease-out;
        }
        .url-input-container {
            margin: 20px 0;
            display: flex;
            gap: 10px;
            width: 100%;
            max-width: 600px;
        }
        .url-input {
            flex: 1;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 14px;
        }
        .url-button {
            padding: 8px 16px;
            background: #405DE6;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .url-button:hover {
            background: #304BD6;
        }
        .url-button:disabled {
            background: #ccc;
            cursor: not-allowed;
        }
        @keyframes slideIn {
            from {
                transform: translateY(100%);
                opacity: 0;
            }
            to {
                transform: translateY(0);
                opacity: 1;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Image Gallery</h1>
        <div class="upload-zone" id="uploadZone">
            <div class="upload-zone-content">
                <svg class="upload-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12"/>
                </svg>
                <div class="upload-text">Click or drag and drop your images here</div>
                <div class="upload-subtext">Supports: JPG, PNG, GIF</div>
                <button class="select-button">Select Image</button>
            </div>
            <input type="file" id="fileInput" accept="image/*" multiple hidden>
        </div>
        <div class="url-input-container">
            <input type="text" id="urlInput" placeholder="Or paste image URL here" class="url-input">
            <button class="url-button" onclick="addImageFromUrl()">Add URL</button>
        </div>
        <div class="gallery" id="gallery"></div>
    </div>
    <div class="modal" id="modal">
        <img id="modalImage" src="" alt="Full size image">
    </div>
    <div class="toast" id="toast"></div>

    <script>
        const uploadZone = document.getElementById('uploadZone');
        const fileInput = document.getElementById('fileInput');
        const gallery = document.getElementById('gallery');
        const modal = document.getElementById('modal');
        const modalImage = document.getElementById('modalImage');
        const toast = document.getElementById('toast');
        let images = [];

        function showToast(message) {
            toast.textContent = message;
            toast.style.display = 'block';
            setTimeout(() => {
                toast.style.display = 'none';
            }, 3000);
        }

        function handleDrag(e) {
            e.preventDefault();
            e.stopPropagation();

            if (e.type === "dragenter" || e.type === "dragover") {
                uploadZone.classList.add('dragging');
            } else if (e.type === "dragleave" || e.type === "drop") {
                uploadZone.classList.remove('dragging');
            }
        }

        uploadZone.addEventListener('click', () => fileInput.click());
        uploadZone.addEventListener('dragenter', handleDrag);
        uploadZone.addEventListener('dragover', handleDrag);
        uploadZone.addEventListener('dragleave', handleDrag);
        uploadZone.addEventListener('drop', (e) => {
            handleDrag(e);
            const files = Array.from(e.dataTransfer.files);
            handleFiles(files);
        });

        fileInput.addEventListener('change', (e) => {
            const files = Array.from(e.target.files || []);
            handleFiles(files);
            fileInput.value = '';
        });

        function handleFiles(files) {
            files.forEach(file => {
                if (!file.type.startsWith('image/')) {
                    showToast('Unsupported file format');
                    return;
                }
                if (file.size > 5 * 1024 * 1024) {
                    showToast('File size exceeds 5MB limit');
                    return;
                }
                const reader = new FileReader();
                reader.onload = (e) => {
                    addImage({
                        id: Date.now(),
                        url: e.target.result
                    });
                    showToast('Image uploaded successfully');
                };
                reader.readAsDataURL(file);
            });
        }

        async function addImageFromUrl() {
            const urlInput = document.getElementById('urlInput');
            const url = urlInput.value.trim();

            if (!url) {
                showToast('Please enter an image URL');
                return;
            }

            try {
                // Validate the URL format
                new URL(url);

                const addButton = document.querySelector('.url-button');
                addButton.disabled = true;
                addButton.textContent = 'Loading...';

                // Validate the image URL by loading it into an <img> element
                await validateImageUrl(url);

                // Add the image to the gallery
                addImage({
                    id: Date.now(),
                    url: url
                });

                urlInput.value = '';
                showToast('Image added successfully');
            } catch (error) {
                showToast(error.message || 'Failed to load image. Please check the URL.');
            } finally {
                const addButton = document.querySelector('.url-button');
                addButton.disabled = false;
                addButton.textContent = 'Add URL';
            }
        }

        function validateImageUrl(url) {
            return new Promise((resolve, reject) => {
                const img = new Image();
                img.onload = () => resolve(true);
                img.onerror = () => reject(new Error('Failed to load image. Please check the URL.'));
                img.src = url;
            });
        }

        function addImage(image) {
            images.unshift(image);
            renderGallery();
        }

        function deleteImage(id) {
            images = images.filter(img => img.id !== id);
            renderGallery();
            showToast('Image deleted successfully');
        }

        function showModal(url) {
            modalImage.src = url;
            modal.classList.add('active');
        }

        modal.addEventListener('click', () => {
            modal.classList.remove('active');
        });

        function renderGallery() {
            gallery.innerHTML = images.map(img => `
                <div class="image-card">
                    <img src="${img.url}" alt="Gallery image" onclick="showModal('${img.url}')">
                    <div class="image-overlay">
                        <button class="delete-button" onclick="deleteImage(${img.id})">
                            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <path d="M18 6L6 18M6 6l12 12"/>
                            </svg>
                        </button>
                    </div>
                </div>
            `).join('');
        }
    </script>
</body>
</html>