Saved the Day - Handling Large Images in the Browser" />
A client uploaded 28 images, each around 5800 x 9500 pixels and 28 MB in size. When attempting to display these images in the browser, the entire tab froze - even refusing to close - for a solid 10 minutes. And this was on a high-end machine with a Ryzen 9 CPU and an RTX 4080 GPU, not a tiny laptop.
In our CMS (Grace Web), when an admin uploads a file - be it a picture or a video - we display it immediately after the upload finishes. It's great for UX - it confirms the upload was successful, they uploaded what they intended to, and it shows responsiveness of the UI for better feel.
But with these massive images, the browser could barely display them and froze up.
It's important to note that the issue wasn't with the upload itself but with displaying these large images. Rendering 28 huge images simultaneously and one by one was just too much. Even on a blank page, it's just... nope.
Anytime you display a picture in an tag, the browser goes through an entire process that employs the CPU, RAM, GPU, and sometimes the drive - sometimes even cycling through this process multiple times before the picture is displayed during image loading and layout recalculations. It goes through calculating pixels, transferring data among hardware, interpolating algorithms, etc. For a browser, it’s a significant process.
We considered several options:
Neither of these solutions felt usable. We didn't want to limit our admins' ability to upload whatever they needed, nor did we want to compromise on the user experience.
I recalled that the
We decided to replace the elements with
const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); const img = new Image(); // img.src = URL.createObjectURL(file); // Use this if you're working with uploaded files img.src = filesroot + data.file_path; // Our existing file path img.onload = function() { // Resize canvas to desired size (for preview) canvas.width = 80 * (img.width / img.height); // Adjust scaling as needed canvas.height = 80; // Draw the image onto the canvas, resizing it in the process ctx.drawImage(img, 0, 0, canvas.width, canvas.height); file_div.innerHTML = ''; // Prepared <div> in UI to display the thumbnail file_div.appendChild(canvas); };
The improvement was immediate and dramatic! Not only did the browser handle the 28 large images without any hiccups, but we also pushed it further by loading 120 MB images measuring 28,000 x 17,000 pixels, and it still worked as if it was a tiny icon.
Using the
By resizing the image within the
Given this success, we're now considering replacing with
We'll be conducting testing and benchmarking to measure any performance gains. I'll be sure to share our findings in a future article.
While using
For public websites, it's best to properly resize images on the server side before serving them to users. If you're interested in how to automate image resizing and optimization, I wrote an article on that topic you might find useful.
I love optimization! Even though we are moving towards local high-performance hardware each day, by optimizing - even when "it's good, but can be better" - we could prevent issues like this from the beginning.
This approach is not standard. And I don’t care for standards very much. They are often treated as hard rules, but they are just guidelines. This solution fits. We do not have clients with ancient devices without solid
Do you have some similar experiences? How did you handle it? Feel free to share your experiences or ask questions in the comments below!
The above is the detailed content of How