Skip to content

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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

PNG to JPG/WEBP/ICO Converter

To create a detailed PNG to JPG/WEBP/ICO 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>PNG to JPG/WEBP/ICO 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>PNG to JPG/WEBP/ICO Converter</h1>
    <input type="file" id="pngInput" accept=".png" />
    <select id="format">
        <option value="jpg">JPG</option>
        <option value="webp">WEBP</option>
        <option value="ico">ICO</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('pngInput');
    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 PNG 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);

            // Handle different formats
            if (format === 'ico') {
                convertToICO(canvas, img.width, img.height).then((blob) => {
                    const url = URL.createObjectURL(blob);
                    preview.innerHTML = `<img src="${url}" alt="Converted Image" />`;
                    downloadLink.href = url;
                    downloadLink.download = `converted_image.ico`;
                    downloadLink.style.display = 'block';
                });
            } else {
                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 === 'jpg' ? 'jpeg' : format}`, 1.0);
            }
        };
        img.src = event.target.result;
    };

    reader.readAsDataURL(file);
});

// Function to convert canvas to ICO format
function convertToICO(canvas, width, height) {
    return new Promise((resolve) => {
        const icoSize = 32; // Standard ICO size (32x32)
        const icoCanvas = document.createElement('canvas');
        icoCanvas.width = icoSize;
        icoCanvas.height = icoSize;
        const icoCtx = icoCanvas.getContext('2d');

        // Resize the image to 32x32 for ICO
        icoCtx.drawImage(canvas, 0, 0, width, height, 0, 0, icoSize, icoSize);

        // Convert the resized image to ICO format
        icoCanvas.toBlob(function (blob) {
            resolve(blob);
        }, 'image/x-icon');
    });
}

3. Explanation

  1. File Input: The user selects a PNG file using the file input.
  2. Format Selection: The user selects the desired output format (JPG, WEBP, or ICO).
  3. Conversion:
    • For JPG and WEBP, the script uses canvas.toBlob() to convert the image.
    • For ICO, the script resizes the image to 32x32 pixels (standard ICO size) and converts it using canvas.toBlob() with the image/x-icon MIME type.
  4. Preview and Download: The converted image is displayed in the preview area, and a download link is provided.

4. Limitations

  • ICO Format: The ICO conversion resizes the image to 32x32 pixels, which is a standard size for icons. If you need multiple sizes or higher resolutions, you would need a more complex solution.
  • Browser Support: Ensure that the browser supports the <canvas> element and the selected formats.

5. Libraries for Advanced Features

For more advanced features (e.g., multi-size ICOs, batch conversion), consider using libraries like:

  • image-conversion: A lightweight library for image conversion.
  • icojs: A library specifically for creating ICO files.

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

About

PNG to JPG/WEBP/ICO

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published