File size: 5,836 Bytes
782bb59
c91e80e
 
 
 
 
9fd3768
96dab67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fd3768
c91e80e
 
 
 
 
 
6be31bc
96dab67
c91e80e
 
 
 
9fd3768
 
 
 
 
c91e80e
379f509
9fd3768
 
 
 
c91e80e
 
379f509
 
 
 
 
 
c91e80e
 
 
 
 
 
6be31bc
 
 
 
 
 
 
 
 
 
c91e80e
 
 
379f509
 
c91e80e
 
 
 
 
 
 
 
 
 
4a0bd66
 
379f509
 
 
 
 
 
 
 
96dab67
 
379f509
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c91e80e
 
 
 
782bb59
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Format Converter</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            max-width: 600px;
            margin: 20px auto;
            padding: 20px;
            background-color: #f4f4f4;
            border-radius: 10px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        label, select, button, input {
            display: block;
            margin-bottom: 15px;
        }

        select, input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            background-color: #4caf50;
            color: #fff;
            padding: 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #45a049;
        }

        a {
            color: #2196F3;
            text-decoration: none;
            display: block;
            margin-top: 10px;
        }

        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <h1>Image Format Converter</h1>

    <form id="imageForm">
        <label for="imageInput">Select an Image:</label>
        <input type="file" id="imageInput" accept="image/*" required onchange="detectImageFormat()">

        <p id="formatInfo">Detected Format: <span id="formatDisplay"></span></p>

        <label for="targetFormat">Choose Target Format:</label>
        <select id="targetFormat" required>
            <option value="image/bmp">BMP</option>
            <option value="image/eps">EPS</option>
            <option value="image/gif">GIF</option>
            <option value="image/ico">ICO</option>
            <option value="image/jpeg">JPG</option>
            <option value="image/png">PNG</option>
            <option value="image/psd">PSD</option>
            <option value="image/svg+xml">SVG</option>
            <option value="image/tga">TGA</option>
            <option value="image/tiff">TIFF</option>
            <option value="image/webp">WebP</option>
        </select>

        <label for="quality">Quality (for JPEG and WebP):</label>
        <input type="range" id="quality" min="1" max="100" step="1" value="85">

        <label for="decreaseSize">Decrease Image Size:</label>
        <input type="checkbox" id="decreaseSize">

        <button type="button" onclick="convertImage()">Convert</button>

        <a id="downloadLink" style="display: none" download="converted_image">Download Converted Image</a>
    </form>

    <script>
        function detectImageFormat() {
            const input = document.getElementById('imageInput');
            const formatDisplay = document.getElementById('formatDisplay');

            if (input.files.length > 0) {
                const file = input.files[0];
                formatDisplay.textContent = file.type;
            }
        }

        function convertImage() {
            const input = document.getElementById('imageInput');
            const targetFormat = document.getElementById('targetFormat').value;
            const quality = document.getElementById('quality').value;
            const decreaseSize = document.getElementById('decreaseSize').checked;
            const formatDisplay = document.getElementById('formatDisplay');
            const downloadLink = document.getElementById('downloadLink');

            if (input.files.length > 0) {
                const file = input.files[0];

                // Display detected format
                formatDisplay.textContent = file.type;

                // Convert and create a download link
                const blob = file.slice(0, file.size, targetFormat);
                const url = URL.createObjectURL(blob);

                if (decreaseSize) {
                    // Decrease image size
                    const img = new Image();
                    img.onload = function () {
                        const canvas = document.createElement('canvas');
                        const ctx = canvas.getContext('2d');

                        const maxWidth = 800;
                        const maxHeight = 800;

                        let newWidth = img.width;
                        let newHeight = img.height;

                        if (img.width > maxWidth) {
                            newWidth = maxWidth;
                            newHeight = (img.height * maxWidth) / img.width;
                        }

                        if (img.height > maxHeight) {
                            newHeight = maxHeight;
                            newWidth = (img.width * maxHeight) / img.height;
                        }

                        canvas.width = newWidth;
                        canvas.height = newHeight;
                        ctx.drawImage(img, 0, 0, newWidth, newHeight);

                        canvas.toBlob(function (smallBlob) {
                            const smallUrl = URL.createObjectURL(smallBlob);
                            downloadLink.href = smallUrl;
                            downloadLink.style.display = 'block';
                        }, targetFormat);
                    };

                    img.src = url;
                } else {
                    downloadLink.href = url;
                    downloadLink.style.display = 'block';
                }
            }
        }
    </script>
</body>
</html>