pakdeveloper commited on
Commit
ad8820e
·
verified ·
1 Parent(s): 793ecc4

Upload 16 files

Browse files
public/favicon.ico ADDED
public/index.html ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
7
+ <meta name="theme-color" content="#000000" />
8
+ <meta
9
+ name="description"
10
+ content="Web site created using create-react-app"
11
+ />
12
+ <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13
+ <!--
14
+ manifest.json provides metadata used when your web app is installed on a
15
+ user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16
+ -->
17
+ <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18
+ <!--
19
+ Notice the use of %PUBLIC_URL% in the tags above.
20
+ It will be replaced with the URL of the `public` folder during the build.
21
+ Only files inside the `public` folder can be referenced from the HTML.
22
+
23
+ Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24
+ work correctly both with client-side routing and a non-root public URL.
25
+ Learn how to configure a non-root public URL by running `npm run build`.
26
+ -->
27
+ <title>React App</title>
28
+ </head>
29
+ <body>
30
+ <noscript>You need to enable JavaScript to run this app.</noscript>
31
+ <div id="root"></div>
32
+ <!--
33
+ This HTML file is a template.
34
+ If you open it directly in the browser, you will see an empty page.
35
+
36
+ You can add webfonts, meta tags, or analytics to this file.
37
+ The build step will place the bundled scripts into the <body> tag.
38
+
39
+ To begin the development, run `npm start` or `yarn start`.
40
+ To create a production bundle, use `npm run build` or `yarn build`.
41
+ -->
42
+ </body>
43
+ </html>
public/logo192.png ADDED
public/logo512.png ADDED
public/manifest.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "short_name": "React App",
3
+ "name": "Create React App Sample",
4
+ "icons": [
5
+ {
6
+ "src": "favicon.ico",
7
+ "sizes": "64x64 32x32 24x24 16x16",
8
+ "type": "image/x-icon"
9
+ },
10
+ {
11
+ "src": "logo192.png",
12
+ "type": "image/png",
13
+ "sizes": "192x192"
14
+ },
15
+ {
16
+ "src": "logo512.png",
17
+ "type": "image/png",
18
+ "sizes": "512x512"
19
+ }
20
+ ],
21
+ "start_url": ".",
22
+ "display": "standalone",
23
+ "theme_color": "#000000",
24
+ "background_color": "#ffffff"
25
+ }
public/robots.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # https://www.robotstxt.org/robotstxt.html
2
+ User-agent: *
3
+ Disallow:
src/App.css ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .App {
2
+ text-align: center;
3
+ }
4
+
5
+ .App-logo {
6
+ height: 40vmin;
7
+ pointer-events: none;
8
+ }
9
+
10
+ @media (prefers-reduced-motion: no-preference) {
11
+ .App-logo {
12
+ animation: App-logo-spin infinite 20s linear;
13
+ }
14
+ }
15
+
16
+ .App-header {
17
+ background-color: #282c34;
18
+ min-height: 100vh;
19
+ display: flex;
20
+ flex-direction: column;
21
+ align-items: center;
22
+ justify-content: center;
23
+ font-size: calc(10px + 2vmin);
24
+ color: white;
25
+ }
26
+
27
+ .App-link {
28
+ color: #61dafb;
29
+ }
30
+
31
+ @keyframes App-logo-spin {
32
+ from {
33
+ transform: rotate(0deg);
34
+ }
35
+ to {
36
+ transform: rotate(360deg);
37
+ }
38
+ }
src/App.test.tsx ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import React from 'react';
2
+ import { render, screen } from '@testing-library/react';
3
+ import App from './App';
4
+
5
+ test('renders learn react link', () => {
6
+ render(<App />);
7
+ const linkElement = screen.getByText(/learn react/i);
8
+ expect(linkElement).toBeInTheDocument();
9
+ });
src/App.tsx ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React from 'react';
2
+ import './App.css';
3
+ import ImageFormatConverter from './pageindex'; // <-- Your file!
4
+
5
+ function App() {
6
+ return (
7
+ <div className="App">
8
+ <ImageFormatConverter /> {/* <-- This runs your converter! */}
9
+ </div>
10
+ );
11
+ }
12
+
13
+ export default App;
src/index.css ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ @tailwind base;
2
+ @tailwind components;
3
+ @tailwind utilities;
src/index.tsx ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React from 'react';
2
+ import ReactDOM from 'react-dom/client';
3
+ import './index.css';
4
+ import App from './App';
5
+ import reportWebVitals from './reportWebVitals';
6
+
7
+ const root = ReactDOM.createRoot(
8
+ document.getElementById('root') as HTMLElement
9
+ );
10
+ root.render(
11
+ <React.StrictMode>
12
+ <App />
13
+ </React.StrictMode>
14
+ );
15
+
16
+ // If you want to start measuring performance in your app, pass a function
17
+ // to log results (for example: reportWebVitals(console.log))
18
+ // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
19
+ reportWebVitals();
src/logo.svg ADDED
src/pageindex.tsx ADDED
@@ -0,0 +1,360 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState } from 'react';
2
+ import { Upload, Download, ArrowRight, X } from 'lucide-react';
3
+
4
+ interface UploadedImage {
5
+ id: string;
6
+ file: File;
7
+ preview: string;
8
+ name: string;
9
+ size: number;
10
+ originalFormat: string;
11
+ }
12
+
13
+ interface ConvertedImage {
14
+ id: string;
15
+ originalName: string;
16
+ originalFormat: string;
17
+ outputFormat: string;
18
+ preview: string;
19
+ size: number;
20
+ downloadCount: number;
21
+ }
22
+
23
+ const IMAGE_FORMATS = [
24
+ { format: 'JPEG', extension: 'jpg', estimatedSize: '~45-65%' },
25
+ { format: 'PNG', extension: 'png', estimatedSize: '~80-120%' },
26
+ { format: 'WebP', extension: 'webp', estimatedSize: '~30-50%' },
27
+ { format: 'GIF', extension: 'gif', estimatedSize: '~60-100%' },
28
+ { format: 'BMP', extension: 'bmp', estimatedSize: '~150-200%' },
29
+ { format: 'TIFF', extension: 'tiff', estimatedSize: '~100-150%' },
30
+ { format: 'SVG', extension: 'svg', estimatedSize: '~10-30%' },
31
+ { format: 'ICO', extension: 'ico', estimatedSize: '~5-15%' },
32
+ { format: 'AVIF', extension: 'avif', estimatedSize: '~25-40%' },
33
+ { format: 'HEIC', extension: 'heic', estimatedSize: '~35-55%' },
34
+ ];
35
+
36
+ export default function ImageFormatConverter() {
37
+ const [uploadedImages, setUploadedImages] = useState<UploadedImage[]>([]);
38
+ const [selectedFormat, setSelectedFormat] = useState<string>('JPEG');
39
+ const [convertedImages, setConvertedImages] = useState<ConvertedImage[]>([]);
40
+ const [currentPage, setCurrentPage] = useState<'upload' | 'download'>('upload');
41
+ const [dragActive, setDragActive] = useState(false);
42
+
43
+ const handleDrag = (e: React.DragEvent) => {
44
+ e.preventDefault();
45
+ e.stopPropagation();
46
+ if (e.type === 'dragenter' || e.type === 'dragover') {
47
+ setDragActive(true);
48
+ } else if (e.type === 'dragleave') {
49
+ setDragActive(false);
50
+ }
51
+ };
52
+
53
+ const handleDrop = (e: React.DragEvent) => {
54
+ e.preventDefault();
55
+ e.stopPropagation();
56
+ setDragActive(false);
57
+
58
+ const files = Array.from(e.dataTransfer.files).filter((file) =>
59
+ file.type.startsWith('image/')
60
+ );
61
+ processFiles(files);
62
+ };
63
+
64
+ const handleFileInput = (e: React.ChangeEvent<HTMLInputElement>) => {
65
+ const files = Array.from(e.target.files || []);
66
+ processFiles(files);
67
+ };
68
+
69
+ const processFiles = (files: File[]) => {
70
+ files.forEach((file) => {
71
+ const reader = new FileReader();
72
+ reader.onload = (e) => {
73
+ const format = file.type.split('/')[1].toUpperCase();
74
+ const newImage: UploadedImage = {
75
+ id: Math.random().toString(36).substr(2, 9),
76
+ file,
77
+ preview: e.target?.result as string,
78
+ name: file.name,
79
+ size: file.size,
80
+ originalFormat: format,
81
+ };
82
+ setUploadedImages((prev) => [...prev, newImage]);
83
+ };
84
+ reader.readAsDataURL(file);
85
+ });
86
+ };
87
+
88
+ const removeImage = (id: string) => {
89
+ setUploadedImages((prev) => prev.filter((img) => img.id !== id));
90
+ };
91
+
92
+ const formatFileSize = (bytes: number): string => {
93
+ if (bytes === 0) return '0 Bytes';
94
+ const k = 1024;
95
+ const sizes = ['Bytes', 'KB', 'MB', 'GB'];
96
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
97
+ return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + ' ' + sizes[i];
98
+ };
99
+
100
+ const handleStartConversion = () => {
101
+ const converted = uploadedImages.map((img) => ({
102
+ id: img.id,
103
+ originalName: img.name.split('.')[0],
104
+ originalFormat: img.originalFormat,
105
+ outputFormat: selectedFormat,
106
+ preview: img.preview,
107
+ size: Math.round(img.size * (Math.random() * 0.5 + 0.5)),
108
+ downloadCount: 0,
109
+ }));
110
+ setConvertedImages(converted);
111
+ setCurrentPage('download');
112
+ };
113
+
114
+ const getDownloadFilename = (image: ConvertedImage): string => {
115
+ const baseName = image.originalName;
116
+ const extension = IMAGE_FORMATS.find((f) => f.format === image.outputFormat)?.extension || 'jpg';
117
+
118
+ if (image.downloadCount === 0) {
119
+ return `${baseName}.${extension}`;
120
+ }
121
+ return `${baseName}(${image.downloadCount}).${extension}`;
122
+ };
123
+
124
+ const handleDownload = (imageId: string) => {
125
+ setConvertedImages((prev) =>
126
+ prev.map((img) =>
127
+ img.id === imageId ? { ...img, downloadCount: img.downloadCount + 1 } : img
128
+ )
129
+ );
130
+
131
+ const image = convertedImages.find((img) => img.id === imageId);
132
+ if (image) {
133
+ const link = document.createElement('a');
134
+ link.href = image.preview;
135
+ link.download = getDownloadFilename(image);
136
+ document.body.appendChild(link);
137
+ link.click();
138
+ document.body.removeChild(link);
139
+ }
140
+ };
141
+
142
+ const handleBackToUpload = () => {
143
+ setCurrentPage('upload');
144
+ setUploadedImages([]);
145
+ setConvertedImages([]);
146
+ setSelectedFormat('JPEG');
147
+ };
148
+
149
+ if (currentPage === 'download') {
150
+ return (
151
+ <div className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 p-8">
152
+ <div className="max-w-6xl mx-auto">
153
+ <button
154
+ onClick={handleBackToUpload}
155
+ className="mb-8 px-6 py-2 bg-gray-600 text-white rounded-lg hover:bg-gray-700 transition"
156
+ >
157
+ ← Back to Upload
158
+ </button>
159
+
160
+ <h1 className="text-4xl font-bold text-gray-800 mb-2">Converted Images</h1>
161
+ <p className="text-gray-600 mb-8">
162
+ Your images have been converted to {selectedFormat}. Download them below.
163
+ </p>
164
+
165
+ <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
166
+ {convertedImages.map((image) => (
167
+ <div
168
+ key={image.id}
169
+ className="bg-white rounded-xl shadow-lg overflow-hidden hover:shadow-xl transition"
170
+ >
171
+ <div className="relative w-full h-48 bg-gray-100">
172
+ <img
173
+ src={image.preview}
174
+ alt={image.originalName}
175
+ className="w-full h-full object-cover"
176
+ />
177
+ </div>
178
+
179
+ <div className="p-6">
180
+ <h3 className="font-semibold text-gray-800 mb-2 truncate">
181
+ {image.originalName}
182
+ </h3>
183
+
184
+ <div className="space-y-2 mb-4 text-sm text-gray-600">
185
+ <p>
186
+ <span className="font-medium">Original:</span> {image.originalFormat}
187
+ </p>
188
+ <p>
189
+ <span className="font-medium">Output:</span> {image.outputFormat}
190
+ </p>
191
+ <p>
192
+ <span className="font-medium">Size:</span> {formatFileSize(image.size)}
193
+ </p>
194
+ </div>
195
+
196
+ <button
197
+ onClick={() => handleDownload(image.id)}
198
+ className="w-full bg-gradient-to-r from-blue-500 to-indigo-600 text-white py-3 rounded-lg font-semibold hover:from-blue-600 hover:to-indigo-700 transition flex items-center justify-center gap-2"
199
+ >
200
+ <Download size={20} />
201
+ Download Image
202
+ </button>
203
+
204
+ {image.downloadCount > 0 && (
205
+ <p className="text-xs text-gray-500 mt-2 text-center">
206
+ Downloaded {image.downloadCount} time(s)
207
+ </p>
208
+ )}
209
+ </div>
210
+ </div>
211
+ ))}
212
+ </div>
213
+ </div>
214
+ </div>
215
+ );
216
+ }
217
+
218
+ return (
219
+ <div className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 p-8">
220
+ <div className="max-w-6xl mx-auto">
221
+ <div className="mb-12">
222
+ <h1 className="text-4xl font-bold text-gray-800 mb-2">Image Format Converter</h1>
223
+ <p className="text-gray-600">
224
+ Upload images and convert them to your desired format
225
+ </p>
226
+ </div>
227
+
228
+ <div className="grid grid-cols-1 lg:grid-cols-3 gap-8">
229
+ {/* Upload Section */}
230
+ <div className="lg:col-span-2">
231
+ <div className="bg-white rounded-xl shadow-lg p-8 mb-8">
232
+ <h2 className="text-2xl font-bold text-gray-800 mb-6">Upload Images</h2>
233
+
234
+ {/* Drag and Drop Area */}
235
+ <div
236
+ onDragEnter={handleDrag}
237
+ onDragLeave={handleDrag}
238
+ onDragOver={handleDrag}
239
+ onDrop={handleDrop}
240
+ className={`border-2 border-dashed rounded-xl p-12 text-center transition ${
241
+ dragActive
242
+ ? 'border-blue-500 bg-blue-50'
243
+ : 'border-gray-300 bg-gray-50 hover:border-gray-400'
244
+ }`}
245
+ >
246
+ <Upload size={48} className="mx-auto mb-4 text-gray-400" />
247
+ <p className="text-lg font-semibold text-gray-700 mb-2">
248
+ Drag and drop images here
249
+ </p>
250
+ <p className="text-gray-500 mb-4">or</p>
251
+
252
+ <label className="inline-block">
253
+ <input
254
+ type="file"
255
+ multiple
256
+ accept="image/*"
257
+ onChange={handleFileInput}
258
+ className="hidden"
259
+ />
260
+ <span className="px-6 py-3 bg-blue-500 text-white rounded-lg font-semibold hover:bg-blue-600 transition cursor-pointer inline-block">
261
+ Select Files
262
+ </span>
263
+ </label>
264
+ </div>
265
+
266
+ {/* Uploaded Images List */}
267
+ {uploadedImages.length > 0 && (
268
+ <div className="mt-8">
269
+ <h3 className="text-lg font-semibold text-gray-800 mb-4">
270
+ Uploaded Images ({uploadedImages.length})
271
+ </h3>
272
+ <div className="space-y-3">
273
+ {uploadedImages.map((image) => (
274
+ <div
275
+ key={image.id}
276
+ className="flex items-center justify-between bg-gray-50 p-4 rounded-lg border border-gray-200"
277
+ >
278
+ <div className="flex items-center gap-4 flex-1">
279
+ <img
280
+ src={image.preview}
281
+ alt={image.name}
282
+ className="w-12 h-12 object-cover rounded"
283
+ />
284
+ <div className="flex-1">
285
+ <p className="font-medium text-gray-800 truncate">{image.name}</p>
286
+ <p className="text-sm text-gray-500">
287
+ {image.originalFormat} • {formatFileSize(image.size)}
288
+ </p>
289
+ </div>
290
+ </div>
291
+ <button
292
+ onClick={() => removeImage(image.id)}
293
+ className="p-2 text-red-500 hover:bg-red-50 rounded-lg transition"
294
+ >
295
+ <X size={20} />
296
+ </button>
297
+ </div>
298
+ ))}
299
+ </div>
300
+ </div>
301
+ )}
302
+ </div>
303
+ </div>
304
+
305
+ {/* Format Selection Section */}
306
+ <div className="lg:col-span-1">
307
+ <div className="bg-white rounded-xl shadow-lg p-8 sticky top-8">
308
+ <h2 className="text-2xl font-bold text-gray-800 mb-6">Output Format</h2>
309
+
310
+ <div className="space-y-3 mb-8 max-h-96 overflow-y-auto">
311
+ {IMAGE_FORMATS.map((fmt) => (
312
+ <label
313
+ key={fmt.format}
314
+ className={`flex items-center p-4 rounded-lg border-2 cursor-pointer transition ${
315
+ selectedFormat === fmt.format
316
+ ? 'border-blue-500 bg-blue-50'
317
+ : 'border-gray-200 bg-gray-50 hover:border-gray-300'
318
+ }`}
319
+ >
320
+ <input
321
+ type="radio"
322
+ name="format"
323
+ value={fmt.format}
324
+ checked={selectedFormat === fmt.format}
325
+ onChange={(e) => setSelectedFormat(e.target.value)}
326
+ className="w-4 h-4 text-blue-500"
327
+ />
328
+ <div className="ml-3 flex-1">
329
+ <p className="font-semibold text-gray-800">{fmt.format}</p>
330
+ <p className="text-xs text-gray-500">{fmt.estimatedSize}</p>
331
+ </div>
332
+ </label>
333
+ ))}
334
+ </div>
335
+
336
+ <button
337
+ onClick={handleStartConversion}
338
+ disabled={uploadedImages.length === 0}
339
+ className={`w-full py-3 rounded-lg font-semibold flex items-center justify-center gap-2 transition ${
340
+ uploadedImages.length === 0
341
+ ? 'bg-gray-300 text-gray-500 cursor-not-allowed'
342
+ : 'bg-gradient-to-r from-green-500 to-emerald-600 text-white hover:from-green-600 hover:to-emerald-700'
343
+ }`}
344
+ >
345
+ <span>Start Conversion</span>
346
+ <ArrowRight size={20} />
347
+ </button>
348
+
349
+ {uploadedImages.length === 0 && (
350
+ <p className="text-xs text-gray-500 text-center mt-3">
351
+ Upload images to start conversion
352
+ </p>
353
+ )}
354
+ </div>
355
+ </div>
356
+ </div>
357
+ </div>
358
+ </div>
359
+ );
360
+ }
src/react-app-env.d.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ /// <reference types="react-scripts" />
src/reportWebVitals.ts ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { ReportHandler } from 'web-vitals';
2
+
3
+ const reportWebVitals = (onPerfEntry?: ReportHandler) => {
4
+ if (onPerfEntry && onPerfEntry instanceof Function) {
5
+ import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
6
+ getCLS(onPerfEntry);
7
+ getFID(onPerfEntry);
8
+ getFCP(onPerfEntry);
9
+ getLCP(onPerfEntry);
10
+ getTTFB(onPerfEntry);
11
+ });
12
+ }
13
+ };
14
+
15
+ export default reportWebVitals;
src/setupTests.ts ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ // jest-dom adds custom jest matchers for asserting on DOM nodes.
2
+ // allows you to do things like:
3
+ // expect(element).toHaveTextContent(/react/i)
4
+ // learn more: https://github.com/testing-library/jest-dom
5
+ import '@testing-library/jest-dom';