How to Create Image Compressor By Using HTML, CSS & JS.

 

How to Create Image Compressor By Using HTML, CSS & JS.





This tutorial will guide you through creating a Image Compressor By Using HTML, CSS & JavaScript. Follow all the given step :

Step 1 : Create Index.html and add given html code on it.
Step 2 : Create style.css and add given css code on it.
Step 3 : Create index.js and add given JavaScript code on it.

HTML


HTML Code for Image Compressor


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Image Compressor</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>Image Compressor</h1>

    <div class="file-input-wrapper">
        <input type="file" id="fileInput" class="file-input" accept="image/*">
        <label for="fileInput" class="file-input-label">
          <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-cloud-arrow-up-fill" viewBox="0 0 16 16">
            <path d="M8 2a5.53 5.53 0 0 0-3.594 1.342c-.766.66-1.321 1.52-1.464 2.383C1.266 6.095 0 7.555 0 9.318 0 11.366 1.708 13 3.781 13h8.906C14.502 13 16 11.57 16 9.773c0-1.636-1.242-2.969-2.834-3.194C12.923 3.999 10.69 2 8 2m2.354 5.146a.5.5 0 0 1-.708.708L8.5 6.707V10.5a.5.5 0 0 1-1 0V6.707L6.354 7.854a.5.5 0 1 1-.708-.708l2-2a.5.5 0 0 1 .708 0z"/>
        </svg>Choose Image
        </label>
    </div>

    <br><br>

    <button onclick="compressImage()" class="compress-btn">
        Compress Image
    </button>

    <br><br>

    <div id="output"></div>

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


CSS


CSS Code for Image Compressor

    
*{
    padding: 0;
    margin: 0;
}
body{
    font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans';
    text-align: center;
    padding: 10px;
    background: rgb(255, 255, 255);
}
h1{
    margin-bottom: 20px;
    margin-top: 30px;
}
.file-input-wrapper{
    position: relative;
    overflow: hidden;
    display: inline-block;
    border-radius: 4px;
    background: #f0f0f0;
    height: 50px;
    min-width: 25%;
    padding: 10px;
    color: #000000;
    font-size: 16px;
    cursor: pointer;
    margin-top: 25px;
    box-shadow: 0px 0px 5px 0px #919191;
    transition: background-color 0.3s ease;
}
.file-input-wrapper:hover{
    background-color: #e0e0e0;
}
.file-input{
    position: absolute;
    left: 0;
    top: 0;
    cursor: pointer;
    opacity: 0;
}
.file-input-label{
    display: inline-block;
    padding: 0px 20px;
    font-weight: bold;
}
.file-input-label svg{
    margin-right: 10px;
    position: relative;
    top: 8px;
    height: 30px;
    width: 30px;
}
button{
    padding: 12px 32px;
    position: relative;
    background: #000;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    overflow: hidden;
    color: white;
}
button::before{
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    background-color: rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    transform: translate(-50%, -50%);
    animation: ripple 1s linear infinite;
}
button:active{
    transform: scale(.96);
}
@keyframes ripple{
    0%{
        width: 0;
        height: 0;
        opacity: 1;
    }
    100%{
        width: 300px;
        height: 300px;
        opacity: 0;
    }
}
#output img{
    max-width: 100%;
    margin-top: 10px;
}
a{
    text-decoration: none;
    font-size: 13px;
    position: relative;
    top: 20px;
    padding: 13px 20px;
    background: #000000;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    color: white;
    box-shadow: inset 1px 1px 10px 3px #ffffff;
}
    


JavaScript


JavaScript code for Image Compressor

            
function compressImage() {
    const fileInput = document.getElementById('fileInput');
    const output = document.getElementById('output');

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

    reader.onload = function(event) {
        const img = new Image();
        img.src = event.target.result;

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

            const maxWidth = 800;
            const maxHeight = 600;
            let width = img.width;
            let height = img.height;

            if (width > height) {
                if (width > maxWidth) {
                    height *= maxWidth / width;
                    width = maxWidth;
                }
            } else {
                if (height > maxHeight) {
                    width *= maxHeight / height;
                    height = maxHeight;
                }
            }

            canvas.width = width;
            canvas.height = height;

            ctx.drawImage(img, 0, 0, width, height);

            const compressedDataURL = canvas.toDataURL('image/jpeg', 0.7);

            const compressedImage = new Image();
            compressedImage.src = compressedDataURL;
            output.innerHTML = '';
            output.appendChild(compressedImage);

            const downloadLink = document.createElement('a');
            downloadLink.href = compressedDataURL;
            downloadLink.download = 'compressed_image.jpg';
            downloadLink.textContent = 'Download Compressed Image';
            output.appendChild(document.createElement('br'));
            output.appendChild(downloadLink);
        };
    };

    if (file) {
        reader.readAsDataURL(file);
    }
}
    






What is HTML

HTML, or HyperText Markup Language, is the standard markup language used to create and design documents on the World Wide Web. It's essentially the backbone of web pages, providing the structure and content that web browsers interpret and display.

HTML consists of a series of elements, which are enclosed by tags to define their purpose and function on a webpage. These elements can include headings, paragraphs, images, links, lists, forms, and more. By using a combination of HTML elements, web developers can organize and present information in a structured and visually appealing manner.

HTML is not a programming language but rather a markup language. This means it doesn't have the capability to perform calculations or execute complex logic like programming languages do. Instead, HTML focuses on describing the structure and content of a document, leaving the presentation and functionality to other technologies like CSS (Cascading Style Sheets) for styling and JavaScript for interactivity.

In summary, HTML serves as the foundation for building web pages, providing the structure and content necessary for browsers to render and display information to users on the internet.

What is CSS

CSS, or Cascading Style Sheets, is a language used to style the presentation of web documents written in HTML and XML. It provides a way to control the layout, colors, fonts, and other visual aspects of a webpage, enabling developers to create attractive and responsive designs. By separating content from presentation, CSS promotes cleaner code, easier maintenance, and greater flexibility in website design. With CSS, developers can achieve consistent branding, improved user experience, and better accessibility across various devices and screen sizes.

What is JavaScript

JavaScript is a versatile programming language used for creating dynamic and interactive web content. It enables developers to manipulate HTML elements, respond to user actions, and build seamless web applications. With its extensive ecosystem of libraries and frameworks, JavaScript powers both client-side and server-side development, making it an essential tool for modern web development.