Home » Php » php – group large set of images

php – group large set of images

Posted by: admin July 12, 2020 Leave a comment


Is there any algorithm for arranging a lot of small images with different dimensions and group them into a larger one?
Anyone have any idea from where should i start investigating?

Basically i want to make something like this http://www.google.ro/images/srpr/nav_logo27.png in PHP. I’m not really expecting such complexity.
I’m gonna use the image as a CSS sprite, if it makes any difference.

How to&Answers:

Using an optimization algorithm could be an overkill for your problem. Coding an optimization algorithm will require a lot of pre-study. Even implementing a ready library could be a big problem to tackle.

You can devise a heuristic, by yourself:
Use the large image’s width and height as your input.
Think of it as if you fill up each row and continue to the next row once the row is filled up.
And start inserting images one by one.

If you use an algorithm the final image will be more optimized. Smaller in size. But the difference would not be much different if your images do not vary a lot in dimensions.

If your image dimensions do vary a little, order them according to their most varying dimension (either width or height). Then start inserting the images in that order.


Generally it is hard computational problem, but approximate solutions exist.

Edit: Related question
Packing rectangular image data into a square texture


I think this is called mosaic, so you can start by looking this up