Tips for improving the quality of your images

How can I adjust image quality/dpi without changing pixel size?

I have an image with a specific pixel size that I need to reduce in quality before saving. If I want to decrease the quality to 87%, how do I achieve this using the following functions?

function defineNewImgFile(image) {

    let imgBlob = base64ImageToBlob(image);

    let newFile = new File([imgBlob], image, {
        type: typeOfImg
    });
    return newFile;
}

//changes base64 format
let typeOfImg;

function base64ImageToBlob(str) {
    let pos = str.indexOf(';base64,');
    let type = str.substring(5, pos);
    typeOfImg = type;
    let b64 = str.substr(pos + 8);

    let imageContent = atob(b64);

    let buffer = new ArrayBuffer(imageContent.length);
    let view = new Uint8Array(buffer);

    for (let n = 0; n < imageContent.length; n++) {
        view[n] = imageContent.charCodeAt(n);
    }

    let blob = new Blob([view], {
        type: type
    });

    return blob;
}

Answer №1

If you're looking to compress images, consider utilizing sharp

Answer №2

Give this a shot:

Code for Javascript Function

var image = document.getElementById('imageId');
if(image && image.style) {
    image.style.height = '100px';
    image.style.width = '200px';
}

Implementing the Front End Code:

<img src="path/to/your/image.jpg" id="imageId" alt="remember your alt tags!"/>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on keeping a floating sidebar afloat as ajax content loads

I'm quite the beginner in JavaScript, so here's my question. I have a floating sidebar that stops floating when it reaches the footer. Here is the JavaScript code: $(window).load(function(){ $(function() { var top = $('#sidebar ...

Retrieve the value from a promise using the $q.all function

I am attempting to retrieve the returned values of promises using the $q.all() method. This is what I have implemented so far. var promise1 = function(time){ var defered = $q.defer(); var promise = defered.promise; def ...

What strategies does NPM/WebPack employ to handle duplicate dependencies across different version ranges?

I am working on an application that relies on various packages, each with its own set of dependencies. For instance, my app may require package@^1.0.0, while another package it uses may demand package@^1.5.1. When I build the app for production, will both ...

How can I attach a jQuery plugin to a textbox with a changing class name?

I have a function that converts a regular text field into a datepicker: <input type="text" class="datepicker form-control" name="text-150" > var DatePicker = function () { if ($(".datepicker").length === 0) { return; } $(".datepic ...

The $digest method is refreshing the main $rootScope parent

I'm having trouble grasping the concept of how $digest functions. I came across a response on Angular $scope.$digest vs $scope.$apply that explains it as follows: " $digest() will update the current scope and any child scopes. $apply() will update ev ...

AWS Lambda, where the billing time exceeds the actual processing time

While working on my Lambda function in Node.js, I noticed a significant difference in the time measured from the start to the end of the function compared to the billed duration provided by Lambda. The function itself takes around 1400 milliseconds to exec ...

Troubleshooting a non-functional Express server demonstration

On my remote server, I currently have node v7.4.0 installed. Recently, I updated to the latest version of express (4.14.0) and set up a file named index.js in my public_html directory. This index.js file is an exact copy of the official online test code sn ...

I encountered an error in JavaScript where the function .val() is not recognized on the selected answer, throwing

I'm trying to verify if the selected answer is correct and then add +1 to my user score. However, I'm struggling with why my code isn't functioning as expected. Can someone please point out where the bug is or if there's something else ...

Expanding video frame to full-screen in jPlayer with the use of an onClick event

Enhanced Video Functionality: Users can easily access the video page from the main menu. The video will start playing automatically, initially not in full screen mode and without control buttons for aesthetic purposes. Upon clicking on the video, it expa ...

Using ASP.NET MVC and jQuery Ajax to close and refresh a parent table from a modal dialog

I am relatively new to both MVC and jQuery, and I'm struggling to make them work together. I've managed to put together a modal dialog form with an ajax postback, but the UI is presenting challenges for me. Despite looking for examples of MVC and ...

The Three.js raycaster fails to intersect with objects once they have been displaced from their original position

I am encountering an issue with the raycaster: When I place an object at the origin (0, 0, 0), the raycaster can detect it. However, if I move the object to a different position, like (0, 300, 0), the raycaster no longer hits the object. I have double-ch ...

Is there a way for me to write on a website and have my content instantly show up on a different hosting

Similar Question: Web based text chat? I am looking to develop a website that enables me to type in a textbox and have the content displayed on another user's screen. Can anyone assist me with this task? Thank you! Amen ...

prioritizing the loading of the header in an Angular application before proceeding to load the main content

My website has a basic layout shown below: |-------------------| | HEADER | |___________________| |------||-----------| | side || Main | | bar || Content | | || | |------||------------ For routing and states, I am using ...

Issue with accessing Protractor spec file location

As a newcomer to Protractor, I followed a tutorial on GitHub to set everything up. The tutorial was successful, but when I tried to apply it to my actual code, I encountered a problem. I found that I can only call the spec.js file if it's located in t ...

Using an if statement to run a script in Npm

Is there a way to configure an npm run script to use different AWS accounts based on the environment? { "config": { "acc": if ({npm_config_env} == "dev") "account1" else "account_2" }, "scr ...

Encountering a 'JSON parsing error' while transmitting data from Ajax to Django REST Framework

I've been encountering an issue while trying to send a JSON to my REST API built with Django Rest Framework. Every time I make this request, I receive an error message, regardless of the view I'm accessing. I suspect the problem lies in the AJAX ...

In order for Javascript to continue, it must first wait for a request to be completed

I have a beginner question that's been on my mind. I'm currently working on creating a wrapper for an API and I need to authenticate to receive the access token for further requests (please note, the API doesn't use OAuth). Here is a simpli ...

Determine whether the elements in the master array match the elements in the child array

Checking for data presence in arrays: [ { "productDisplay": "ZXP 105", "productNumber": "WZDR 112" }, { "productDisplay": "ZXP 106", "productNumber": "WZDR 113" } ] ChildArray [{productDisplay:"ZXP 105", ...

Displaying a div on click using Jquery will only impact one div at a time

I am encountering an issue with my WordPress setup. Within my loop, I have a clickable link that toggles a div to show or hide content. However, since each item in the loop uses the same class, clicking on one link activates all of them simultaneously. &l ...

Improving efficiency by saving and loading the form value using best practices

One of the challenges I'm facing is how to populate dropdown selections on a webpage without resorting to hard-coded procedures. The code snippet below illustrates the current setup: <td> <ui-select ng-model="user_profile.gender" require ...