File size: 5,744 Bytes
782bb59
c91e80e
 
 
 
 
9fd3768
31ac590
53f6048
 
 
 
 
 
 
9a82139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31ac590
 
 
 
 
 
 
 
9a82139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!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>
        /* Your existing styles... */
    </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="detectImageProperties()">

        <p id="formatInfo">Detected Format: <span id="formatDisplay"></span></p>
        <p id="sizeInfo">Image Size: <span id="sizeDisplay"></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="customWidth">Custom Width (leave empty for original):</label>
        <input type="text" id="customWidth">

        <label for="customHeight">Custom Height (leave empty for original):</label>
        <input type="text" id="customHeight">

        <input type="checkbox" id="keepAspectRatio" checked>
        <label for="keepAspectRatio">Keep Aspect Ratio</label>

        <input type="checkbox" id="compressImage">
        <label for="compressImage">Compress Image</label>

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

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

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

            if (input.files.length > 0) {
                const file = input.files[0];
                const img = new Image();
                img.src = URL.createObjectURL(file);

                img.onload = function () {
                    formatDisplay.textContent = file.type;
                    sizeDisplay.textContent = `${img.width} x ${img.height} pixels`;
                };
            }
        }

        function convertImage() {
            const input = document.getElementById('imageInput');
            const targetFormat = document.getElementById('targetFormat').value;
            const customWidth = document.getElementById('customWidth').value;
            const customHeight = document.getElementById('customHeight').value;
            const keepAspectRatio = document.getElementById('keepAspectRatio').checked;
            const compressImage = document.getElementById('compressImage').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;

                // Check if the target format is supported
                const supportedFormats = ['image/bmp', 'image/eps', 'image/gif', 'image/ico', 'image/jpeg', 'image/png', 'image/psd', 'image/svg+xml', 'image/tga', 'image/tiff', 'image/webp'];

                if (!supportedFormats.includes(targetFormat)) {
                    alert('Unsupported target format. Please choose a supported format.');
                    return;
                }

                // Convert and create a download link
                const img = new Image();
                img.src = URL.createObjectURL(file);

                img.onload = function () {
                    const canvas = document.createElement('canvas');
                    const ctx = canvas.getContext('2d');

                    // Apply custom dimensions
                    let newWidth, newHeight;

                    if (customWidth && customHeight) {
                        newWidth = parseInt(customWidth, 10);
                        newHeight = parseInt(customHeight, 10);

                        if (keepAspectRatio) {
                            // Calculate new height to maintain aspect ratio
                            newHeight = (img.height * newWidth) / img.width;
                        }
                    } else {
                        // If custom dimensions are not provided, use original dimensions
                        newWidth = img.width;
                        newHeight = img.height;
                    }

                    canvas.width = newWidth;
                    canvas.height = newHeight;

                    // Draw the image on the canvas
                    ctx.drawImage(img, 0, 0, newWidth, newHeight);

                    // Convert and create a download link
                    canvas.toBlob(function (blob) {
                        const url = URL.createObjectURL(blob);
                        downloadLink.href = url;
                        downloadLink.style.display = 'block';
                    }, targetFormat, compressImage ? 0.85 : 1.0); // Adjust compression quality if needed
                };
            }
        }
    </script>
</body>
</html>