How I managed to render million small images on a webpage
I am building 10MPage.com which captures the state of the internet in 2025. Every internet user is allowed to upload a small image of 64x64 pixels and contribute to this archive.
As the name suggests it needs to be able to handle 10 million of these small images. When I first came up with this concept I was concerned on how to render these efficiently. In this article I will talk about my first tries of doing is and the final solution.
Before you continue, take a look at 10MPage.com and see if you can figure out how it's done. And if you've reached 10MPage, why not claim a tile for yourself? :)
Image tags versus canvas
The first choice I had to make was if I wanted to use HTML elements or a full screen canvas.
Seperate image tags
I initially tested with seperate
<div> <p>With this CSS:<br> </p> <pre class="brush:php;toolbar:false"> <style> body { margin: 0; padding: 0; overflow: auto; /* Enable scrolling */ } .grid { display: block; position: relative; width: 100%; /* The grid will take the full width */ } .row { display: flex; /* Each row is a flex container */ } .tile { width: 64px; height: 64px; box-sizing: border-box; border: 1px solid #ccc; /* Visual separation between tiles */ } .tile img { width: 64px; height: 64px; object-fit: cover; } </style> </p> <p>This is what it looks like:</p> <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173668501716928.jpg" class="lazy" alt="How I managed to render million small images on a webpage"></p> <p>Which is fine but there are a few points that could be an issue:</p> <ul> <li>Browser scroll</li> <li>Large DOM</li> <li>Lazy loading</li> </ul> <h3> Canvas </h3> <p>The next approach was via a canvas, for simplicity I decided to just draw a checkerboard. Adding scrolling was easy too, this is what that looked like:<br> </p> <pre class="brush:php;toolbar:false"><body> <canvas> <p>Screenshot:<br> <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173668501927013.jpg" class="lazy" alt="How I managed to render million small images on a webpage"></p> <p>This approach is nice because it allows me to render everything via code which will make more advanced features easier.</p> <h3> Deciding on image tags or canvas </h3> <p>Ultimately I decided on using canvas because it is more flexible than divs. This is because of things like loading animations, smooth scrolling, lazy loading and the fact that it is enterly rendered via code which gives a lot of control. </p> <p>But loading a lot of small images causes a lot of overhead, to minimize that I want to bundle the small images in larger blocks.</p> <h2> Optimize tile delivery </h2> <p>Loading each image separately adds a lot of network request. Let's quickly calculate on a 1080p screen. The width is 1920 pixels which would be 1920 / 64 = 30 tiles. With a height of 1080 pixels that would become 1080 / 64 = ~17 tiles. So to render a full screen of tiles on a fullHD display it would require rendering 30*17 = 510 small images. </p><p>But we need to be able to scroll! And when scrolling I do not want to show a lot of loading icons before rendering. So that means that we have to pre load the surrounding images too. If we want to pre load around it we would need to add eight times the tiles. Imagine the black rectangle is the display:</p> <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173668502115888.jpg" class="lazy" alt="How I managed to render million small images on a webpage"></p> <p>*<em>That would become 510 * 8 = 4080 images! *</em></p> <p>It is not realistic to render that many images so fast. The solution? Combine the individual tiles in larger blocks.</p> <p>Using PHP I wrote a controller that generates an image that contains contains 16*16 tiles. Each block is 64 * 16 = 1024 pixels in width and height. You can see this when you go to 10MPage and look in your network tab of your console.</p> <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173668502382486.jpg" class="lazy" alt="How I managed to render million small images on a webpage"></p> <p>The script will add a question mark for unfilled spots.</p> <p>So instead of 4080 images for 1920 * 3 = 5760 pixels by 1080 * 3 = 3240 pixels we now only need 24 images: 5760 / 1024 = ~6, 3240 / 1024 = ~4 (both rounded up), 6*4 = 24. Which is doable!</p> <h2> Hiding the blocks </h2> <p>I've implemented a few things to 'hide' that the tiles are loaded in larger blocks.</p> <h3> Loading screen always has 64x64 tiles </h3> <p>Loading screen<br> <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173668502441571.jpg" class="lazy" alt="How I managed to render million small images on a webpage"></p> <h3> Full grid is always rendered square </h3> <p>In order to hide large gaps on the bottom or right of the grid the grid will never load blocks if it is not square. You will not see a block at the bottom and then an empty block to the left or right of that.</p> <p>Thank you for reading this artile, I hope you've learned something. <br> If you did, why not add your favorite programming language, crypto coin or your pet to the 10MPage? It is free!</p>
The above is the detailed content of How I managed to render million small images on a webpage. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

This article explores effective use of Java's Collections Framework. It emphasizes choosing appropriate collections (List, Set, Map, Queue) based on data structure, performance needs, and thread safety. Optimizing collection usage through efficient

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion
