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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.
