0

I have an image with a size of around 12MB and it takes time to get loaded. Is there any way to reduce the size of the image to make it load faster? Thanks

New Update: I want to reduce the image size as it gets loaded from an input type file.

11
  • 2
    If you dont mind the manual step, you can just use this: tiny-img.com Commented Jul 4, 2022 at 8:54
  • 2
    Cannot resize what you have not loaded, so unless you're planning to resize on the backend, the answer is no. Commented Jul 4, 2022 at 8:55
  • 1
    Or use something like sharp to do it programmatically. Commented Jul 4, 2022 at 8:55
  • 1
    Is there a way to upload only every 16th pixel only from a file? That would function as resizing. Commented Jul 4, 2022 at 9:17
  • 1
    Well, your frontend JavaScript will not be able to resize the image to reduce load. If you do not run NodeJS in your backend, then the question should probably be asked with another tag. Commented Jul 4, 2022 at 9:32

1 Answer 1

0

I would suggest you to load images using Image() js method so the remaning content of the page load properly, and use loading = 'lazy' property on the images to make the browser load them only when they are visible in the viewport or when you scroll near to them.

function loadImg(){
var img = new Image(400, 400);
img.src = 'https://pbs.twimg.com/profile_images/1220067947798024192/30eZhfxx_400x400.png';
img.setAttribute("loading", "lazy")
document.body.appendChild(img);
}
#btn{
position: fixed;
top: 5px
}
<input type="button" onclick="loadImg()" value="Load image" id="btn">

Sign up to request clarification or add additional context in comments.

3 Comments

it keeps creating new images every time I click on the load image button
thats the purpose of the snippet above, its just an exemple, try use loading = 'lazy' in your code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.