doushao6874 2012-12-12 12:48
浏览 62
已采纳

将图像均匀分布到图库中的列

Brief Explanation

I am currently building a gallery that has three columns, each column contains images all of which have the same width, but can vary in height.

The images for the columns are gathered from a directory and placed into an array using the PHP glob() function. This is the easy part...

An example of what the gallery looks like:

The basic design of the gallery

The Issue

As these images are being loaded and placed into the columns dynamically, there is a chance that the columns may differ in height hugely.

For instance, if two portrait photos were placed in column 1, and two landscape photos were placed in column 2, the columns would be very uneven like so:

The issue with this design

It is extremely unlikely that the columns will ever match in height, but I would like them to be as close as possible with the given images and therefore would like to form an algorithm that will look at the images retrieved and place them in the columns to return three columns that are as close in height as they can be.

So, for example, the script would correct the above problem by re-sorting the images and placing them like so:

The result of the image sorting

I am capable of writing this if I have the correct algorithm, I just cannot think of the best steps to do this. Can anyone suggest any steps?

Possible Solution

One of the methods I thought of (I think there will be better ways as I think this is flawed):

  1. Add up the height of all of the images combined and divide by the number of columns (3). This will give us the height to aim for
  2. Distribute the images to the column array and when it exceeds the height of the column, overflow onto the next column.
  3. Place any left over images into the first column, then second column etc...

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dsqnonh2763 2012-12-12 13:12
    关注

    I'd suggest something along the following:

    1. Order your images into an array from tallest to shortest (assuming your width is constant)
    2. Add image to first column array
    3. Add images to next column array until total height is equal or greater than previous column array
    4. Repeat steps 2 & 3 until all images are used (you could even alternate populating the columns in ascending and descending order to help even things out)
    5. Shuffle image arrays to make gridlines appear random before adding images to columns
    6. Adjust vertical margins on images for the shorter columns to match total height of tallest column (a vertical justified align, of sorts)

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿