Open In App

Create a Photo Filters and Effects Project using HTML CSS & JavaScript

In this article, we are going to implement a photo editor that allows users to apply various filters and effects to their images. This project will use HTML, CSS, and JavaScript to provide a user-friendly interface for adjusting brightness, contrast, gray scale, blur, sepia, and hue rotation filters. Users can also reset the filters to their default values and save the edited image.

Preview of final output: Let us have a look at how the final output will look like.



Prerequisites

Approach

Our approach involves building a user interface with input sliders for each filter and applying these filters in real-time to the displayed image. We’ll also provide options to reset the filters and save the edited image.



Example: Write the following code in respective files




// Script.js
const imageInput =
    document.getElementById(
        "imageInput"
    );
const previewImage =
    document.getElementById(
        "previewImage"
    );
const brightnessRange =
    document.getElementById(
        "brightness"
    );
const contrastRange =
    document.getElementById("contrast");
const grayscaleRange =
    document.getElementById(
        "grayscale"
    );
const blurRange =
    document.getElementById("blur");
const sepiaRange =
    document.getElementById("sepia");
const hueRange =
    document.getElementById("hue");
const resetButton =
    document.getElementById("reset");
const saveButton =
    document.getElementById("save");
 
let currentImage = null;
 
imageInput.addEventListener(
    "change",
    (e) => {
        const file = e.target.files[0];
        if (file) {
            const reader =
                new FileReader();
            reader.onload = (event) => {
                currentImage =
                    new Image();
                currentImage.src =
                    event.target.result;
                currentImage.onload =
                    () => {
                        previewImage.src =
                            currentImage.src;
                        resetFilters();
                    };};
            reader.readAsDataURL(file);
        }
    });
 
brightnessRange.addEventListener(
    "input",
    () => {
        applyFilters();
    });
 
contrastRange.addEventListener(
    "input",
    () => {
        applyFilters();
    });
 
grayscaleRange.addEventListener(
    "input",
    () => {
        applyFilters();
    });
 
blurRange.addEventListener(
    "input",
    () => {
        applyFilters();
    });
 
sepiaRange.addEventListener(
    "input",
    () => {
        applyFilters();
    });
 
hueRange.addEventListener(
    "input",
    () => {
        applyFilters();
    });
 
resetButton.addEventListener(
    "click",
    () => {
        resetFilters();
    });
 
saveButton.addEventListener(
    "click",
    () => {
        saveEditedImage();
    });
 
function applyFilters() {
    if (currentImage) {
        const brightnessValue =
            brightnessRange.value;
        const contrastValue =
            contrastRange.value;
        const grayscaleValue =
            grayscaleRange.value;
        const blurValue =
            blurRange.value;
        const sepiaValue =
            sepiaRange.value;
        const hueValue = hueRange.value;
 
        const filterValue = `brightness(${brightnessValue}%)
                             contrast(${contrastValue}%)
                             grayscale(${grayscaleValue}%)
                             blur(${blurValue}px)
                             sepia(${sepiaValue}%)
                             hue-rotate(${hueValue}deg)`;
        previewImage.style.filter =
            filterValue;
    }}
 
function resetFilters() {
    if (currentImage) {
        brightnessRange.value = 100;
        contrastRange.value = 100;
        grayscaleRange.value = 0;
        blurRange.value = 0;
        sepiaRange.value = 0;
        hueRange.value = 0;
        previewImage.style.filter =
            "none";
    }}
 
function saveEditedImage() {
    if (currentImage) {
        const canvas =
            document.createElement(
                "canvas"
            );
        canvas.width =
            currentImage.width;
        canvas.height =
            currentImage.height;
        const context =
            canvas.getContext("2d");
        context.filter =
            previewImage.style.filter;
        context.drawImage(
            currentImage,
            0,
            0,
            canvas.width,
            canvas.height
        );
 
        const link =
            document.createElement("a");
        link.href = canvas.toDataURL(
            "image/jpeg"
        );
        link.download =
            "edited_image.jpg";
        link.click();
    }}




<!-- Index.html  -->
<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8" />
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0" />
    <title>Photo Editor</title>
    <link rel="stylesheet" href="style.css" />
</head>
 
<body>
    <div class="container">
        <h1>Photo Editor</h1>
        <input type="file" id="imageInput"
                           accept="image/*" />
        <div class="image-container">
            <img src="placeholder.jpg"
                 alt="Image" id="previewImage" />
        </div>
        <div class="controls">
            <label for="brightness">
                Brightness:
            </label>
            <input type="range" id="brightness"
                   min="0" max="200" value="100" />
            <br/>
            <label for="contrast">
                Contrast:
            </label>
            <input type="range" id="contrast"
                   min="0" max="200" value="100" />
            <br />
            <label for="grayscale">
                Grayscale:
            </label>
            <input type="range" id="grayscale"
                   min="0" max="100" value="0" />
            <br />
            <label for="blur">
                Blur:
            </label>
            <input type="range" id="blur"
                   min="0" max="10" value="0" />
            <br />
            <label for="sepia">
                Sepia:
            </label>
            <input type="range" id="sepia"
                   min="0" max="100" value="0" />
            <br />
            <label for="hue">
                Hue Rotation:
            </label>
            <input type="range" id="hue"
                   min="0" max="360" value="0" />
            <br />
            <button id="reset">Reset</button>
            <button id="save">Save</button>
        </div>
    </div>
    <script src="script.js"></script>
</body>
 
</html>




/* Style.css */
body {
    font-family: Arial, sans-serif;
    background-color: #f0f0f0;
    text-align: center;
    margin: 0;
    padding: 0;
}
 
.container {
    max-width: 600px;
    margin: 0 auto;
    padding: 20px;
    background-color: #fff;
    box-shadow: 0 0 10px
        rgba(0, 0, 0, 0.1);
    border-radius: 5px;
}
 
h1 {
    font-size: 24px;
    margin-bottom: 20px;
}
 
.image-container {
    margin-bottom: 20px;
}
 
img {
    max-width: 100%;
    height: auto;
    border: 1px solid #ddd;
    border-radius: 5px;
}
 
.controls {
    text-align: left;
}
 
label {
    font-weight: bold;
}
 
input[type="range"] {
    width: 100%;
}
 
button {
    background-color: #007bff;
    color: #fff;
    border: none;
    padding: 10px 20px;
    margin-top: 10px;
    border-radius: 5px;
    cursor: pointer;
}
 
button:hover {
    background-color: #0056b3;
}

Output

photo editor


Article Tags :