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? :)
The first choice I had to make was if I wanted to use HTML elements or a full screen canvas.
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="https://img.php.cn/upload/article/000/000/000/173668501716928.jpg" 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="https://img.php.cn/upload/article/000/000/000/173668501927013.jpg" 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="https://img.php.cn/upload/article/000/000/000/173668502115888.jpg" 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="https://img.php.cn/upload/article/000/000/000/173668502382486.jpg" 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="https://img.php.cn/upload/article/000/000/000/173668502441571.jpg" 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!