Js code to improve image quality

 JavaScript alone cannot significantly improve the quality of an image that has been uploaded to a website and appears pixelated or of low quality. Image quality is primarily determined by the resolution and clarity of the original image. However, you can use JavaScript to perform some basic image manipulations, such as resizing, cropping, or applying filters, to enhance the presentation of the image on your website.


Here's an example of JavaScript code that can be used to resize an uploaded image to a specified size:


```javascript

// Function to resize an image

function resizeImage(input, maxWidth, maxHeight, callback) {

    const reader = new FileReader();

    reader.onload = function (e) {

        const img = new Image();

        img.src = e.target.result;

        img.onload = function () {

            let width = img.width;

            let height = img.height;


            // Calculate new dimensions while maintaining aspect ratio

            if (width > height) {

                if (width > maxWidth) {

                    height *= maxWidth / width;

                    width = maxWidth;

                }

            } else {

                if (height > maxHeight) {

                    width *= maxHeight / height;

                    height = maxHeight;

                }

            }


            // Create a canvas to draw the resized image

            const canvas = document.createElement("canvas");

            canvas.width = width;

            canvas.height = height;

            const ctx = canvas.getContext("2d");

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


            // Convert the canvas back to a Blob

            canvas.toBlob(function (blob) {

                callback(blob);

            }, "image/jpeg", 0.9); // You can adjust the quality here

        };

    };

    reader.readAsDataURL(input.files[0]);

}


// Example usage

const fileInput = document.getElementById("fileInput"); // Replace with your file input element

const maxWidth = 800; // Adjust as needed

const maxHeight = 600; // Adjust as needed


fileInput.addEventListener("change", function () {

    resizeImage(this, maxWidth, maxHeight, function (resizedBlob) {

        // You can now use the resizedBlob for display or upload

        const resizedImage = new Image();

        resizedImage.src = URL.createObjectURL(resizedBlob);


        // Display the resized image on your website

        document.getElementById("imageContainer").appendChild(resizedImage);

    });

});

```


In this code:

- The `resizeImage` function takes an input file, maximum width, maximum height, and a callback function.

- It reads the uploaded image, calculates new dimensions to fit within the specified width and height while maintaining the aspect ratio, and then resizes the image.

- The resized image is converted back to a Blob with a specified quality (you can adjust the quality value as needed).

- Finally, the resized image is displayed on your website.


Remember that this code resizes the image to fit within the specified dimensions but does not magically improve the quality of a low-resolution or pixelated image.

Previous
Next Post »