制作具有可变大小缩略图的照片库的最简单方法是什么?
我想创建一个像 Google+“来自你的圈子的照片”这样的图库,其中缩略图看起来像拼贴画。
- 缩略图保持照片的纵横比。
- 画廊行的高度接近(不相等但非常接近)。
- 缩略图之间的间距到处都是相等的。
- 画廊填充一个矩形。
- 照片未裁剪。它们只是调整大小以填充空间。
请参阅屏幕截图作为示例: http://ansonalex.com/wp-content /uploads/2011/07/google-plus-gallery-large.jpg
我想了解如何使用 css/javascript/jquery 在客户端设置这样的画廊缩略图样式以编程方式。
谢谢各位的解答!
I would like to create a gallery like Google+ "Photos From Your Circles" in which the thumbnails looks like a collage.
- Thumbnails keeps the aspect ratio of the photo.
- Gallery rows are close in height (not equal but very close).
- Spacing between thumbnails is equal everywhere.
- The gallery fills in a rectangle.
- Photos are not cropped. They're just resized to fill in the space.
Please see the screenshot as example:
http://ansonalex.com/wp-content/uploads/2011/07/google-plus-gallery-large.jpg
i would like to learn how can i use css/javascript/jquery to style gallery thumbnails like this at the client side programatically.
Thank you for answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能会发现这篇文章很有帮助:构建一个受 Google Plus 启发的图片库
他使用了一种不需要对图像进行排序的技术,而且非常简单。基本上,一旦您知道缩略图行的宽度 W,请继续放置缩略图,直到总宽度超过 W。假设您超出了 W 40 像素。现在,裁剪行中的每个缩略图(通过 css)以总共删除 40 个像素。
比如说,如果您想从图像中裁剪 10 像素,则可以使用以下方法来实现:
overflow:hidden 裁剪图像,负边距基本上将其水平居中。
You may find this write-up helpful: Building a Google Plus Inspired Image Gallery
He uses a technique that does not require sorting the images, and it's pretty simple. Basically, once you know the width W of your rows of thumbnails, keep placing thumbnails until the total width exceeds W. Let's say you exceed W by 40px. Now, crop each thumbnail in the row (via css) to remove 40 pixels total.
If, say, you want to crop 10px from an image, you can do it with something like this:
The overflow:hidden crops the image, and the negative margin centers it horizontally, basically.
这是典型的切削库存问题。您可以使用分支定界法或动态编程。
一般来说,谷歌拥有庞大的图片库,因此对他们来说找到适当的宽度组合更容易。
对于您来说,我建议使用有限的长宽比集(例如 16),这会创建有限数量的组合(256)。转换前的每张图片都应缩放或裁剪为最接近的宽高比。
请记住,可能有非常宽的图片需要一些解决方法:裁剪或它们的宽度应该是行的宽度和高度变量。间距也需要以某种方式解决。我会将所有图片排成一排,并将间距作为白色叠加在图片上。
This is classic cutting stock problem. You can solve it using branch and bound method or dynamic programming.
Generally Google has huge base of pictures thus finding proper combination of widths is easier for them.
For you I would suggest having limited set of aspect ratios (ex. 16) which creates finite number of combinations (256). Every picture before conversion should be scaled or cropped to closest aspect ratio.
Keep in mind that there may be very wide pictures which need some workaround: cropped or width of them should be width of row and height variable. Also spacing needs to be solved somehow. I would join all pictures in one row and put spacings as white overlays over pictures.
我将简化我所看到的内容(例如,我提供的数字不会 100%、精确到像素),但它将为您提供一种实现此目的的方法。
当我查看屏幕截图时,我看到的是一个长宽比约为 4x3(或相反为 3x4)的图像列表。这个纵横比是布局的核心。填充的整个矩形可以是任何纵横比,但它应该是某些宽度和高度的倍数。例如,您会看到每行都包含一些肖像照片和一些风景照片。然而,总体效果是 G+ 可以从大量图像中提取数据,因此可以选择满足单个宽高比(给定图像的横向或纵向)以及包含矩形的整体纵横比。
我会拍摄池中可用的图像,并计算它们的纵横比(简单的宽度除以高度)。然后按纵横比对图像进行分组。
最后,布局中棘手的部分是,您必须弄清楚哪些宽高比组合会导致一行从左到右完全填满。从屏幕截图中我们看到三个这样的例子:
结果是,由于所有缩略图都具有相同的高度,因此这些特定组合中的组合宽度将为您提供布局矩形所需的最终宽度。
因此,我认为解决这个特定问题基本上是解决 4 个子问题:
I'm going to simplify what I see (e.g. the numbers I provide won't be 100%, down-to-the-pixel accurate), but it will give you one method for accomplishing this.
When I look at the screenshot, what I see is a list of images that are approximately 4x3 aspect ratio (or 3x4 in reverse). This aspect ratio is at the heart of the layout. The overall rectangle that is filled can be any aspect ratio, but it should be a multiple of some widths and heights. For example, you'll see that each row contains some portrait, and some landscape photos. The overall effect, however, is that G+ can pull from a large pool of images, and therefore, can choose a combination of images that meet the needs of the individual aspect ratio (landscape or portrait aspect of the given image), as well as overall aspect ratio of the containing rectangle.
I would take images that are available in the pool, and calculate their aspect ratio (a simple width divided by height). Then group the images by their aspect ratio.
Finally, the part that's tricky about the layout is that you have to figure out which combinations of aspect ratios will result in a row that is completely full, from left to right. From the screenshot we see three such examples:
The result is that, since all thumbnails have the same height, their combined widths in these particular combinations give you the desired resulting width for the layout rectangle.
So, I think solving this particular problem is basically a matter of solving 4 sub-problems: