Skip to content

convert-apps/jpg-to-png-webp-jpeg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

jpg to png/webp/jpeg Converter

To create a detailed JPG to PNG/WEBP/JPEG converter using JavaScript and HTML, you can use the <canvas> element to manipulate and convert images. Below is a step-by-step guide to building a simple web-based converter.


1. HTML Structure

Create a basic HTML structure with file input, format selection, and a button to trigger the conversion.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JPG to PNG/WEBP/JPEG Converter</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        #preview {
            max-width: 100%;
            margin-top: 20px;
        }
        #downloadLink {
            display: none;
            margin-top: 10px;
            color: blue;
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <h1>JPG to PNG/WEBP/JPEG Converter</h1>
    <input type="file" id="jpgInput" accept=".jpg,.jpeg" />
    <select id="format">
        <option value="png">PNG</option>
        <option value="webp">WEBP</option>
        <option value="jpeg">JPEG</option>
    </select>
    <button id="convertBtn">Convert</button>
    <div id="preview"></div>
    <a id="downloadLink" href="#">Download</a>

    <script src="converter.js"></script>
</body>
</html>

2. JavaScript Logic

Create a converter.js file to handle the conversion logic.

document.getElementById('convertBtn').addEventListener('click', function () {
    const fileInput = document.getElementById('jpgInput');
    const format = document.getElementById('format').value;
    const preview = document.getElementById('preview');
    const downloadLink = document.getElementById('downloadLink');

    if (fileInput.files.length === 0) {
        alert('Please select a JPG file.');
        return;
    }

    const file = fileInput.files[0];
    const reader = new FileReader();

    reader.onload = function (event) {
        const img = new Image();
        img.onload = function () {
            const canvas = document.createElement('canvas');
            canvas.width = img.width;
            canvas.height = img.height;
            const ctx = canvas.getContext('2d');

            // Draw the image onto the canvas
            ctx.drawImage(img, 0, 0);

            // Convert the canvas to the selected format
            canvas.toBlob(function (blob) {
                const url = URL.createObjectURL(blob);
                preview.innerHTML = `<img src="${url}" alt="Converted Image" />`;
                downloadLink.href = url;
                downloadLink.download = `converted_image.${format}`;
                downloadLink.style.display = 'block';
            }, `image/${format === 'jpeg' ? 'jpeg' : format}`, 1.0);
        };
        img.src = event.target.result;
    };

    reader.readAsDataURL(file);
});

3. Explanation

  1. File Input: The user selects a JPG file using the file input.
  2. Format Selection: The user selects the desired output format (PNG, WEBP, or JPEG).
  3. Conversion:
    • The script reads the JPG file, draws it onto a canvas, and then converts it to the selected format using canvas.toBlob().
    • The image/jpeg MIME type is used for JPEG conversion, while image/png and image/webp are used for PNG and WEBP, respectively.
  4. Preview and Download: The converted image is displayed in the preview area, and a download link is provided.

4. Limitations

  • Quality: The quality of the converted image depends on the canvas.toBlob() parameters. You can adjust the quality (e.g., 0.8 for 80% quality) for JPEG and WEBP formats.
  • Browser Support: Ensure that the browser supports the <canvas> element and the selected formats.

5. Example Output

  • If the user selects a JPG file and chooses PNG as the output format, the script will convert the JPG to PNG and provide a download link.
  • Similarly, the script will handle conversions to WEBP and JPEG.

6. Libraries for Advanced Features

For more advanced features (e.g., batch conversion, compression), consider using libraries like:

  • browser-image-compression: A library for compressing images in the browser.
  • sharp.js: A high-performance image processing library (requires Node.js backend).

This basic converter should work for most JPG images and provide a simple way to convert them to PNG, WEBP, or JPEG formats directly in the browser.