简体   繁体   English

使用可变大小缩略图制作照片库的最简单方法是什么?

[英]What is the easiest way of doing a photo gallery with variable size thumbnails?

I would like to create a gallery like Google+ "Photos From Your Circles" in which the thumbnails looks like a collage. 我想创建一个类似Google+“来自你的圈子的照片”的图库,其中缩略图看起来像拼贴画。

  • 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 请以屏幕截图为例: 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. 我想学习如何使用css / javascript / jquery以编程方式在客户端设置这样的图库缩略图。

Thank you for answers! 谢谢你的回答!

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. 一般来说,Google拥有庞大的图片库,因此找到合适的宽度组合对他们来说更容易。

For you I would suggest having limited set of aspect ratios (ex. 16) which creates finite number of combinations (256). 对于你,我建议使用有限的宽高比(例如16)来创建有限数量的组合(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. 我会将所有图片连成一行,并将间距作为白色叠加在图片上。

You may find this write-up helpful: Building a Google Plus Inspired Image Gallery 您可能会发现这篇文章很有用: 构建Google Plus灵感图片库

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. 基本上,一旦你知道你的缩略图行的宽度W,继续放置缩略图直到总宽度超过W.假设你超过W 40 xx。 Now, crop each thumbnail in the row (via css) to remove 40 pixels total. 现在,裁剪行中的每个缩略图(通过css)以删除总共40个像素。

If, say, you want to crop 10px from an image, you can do it with something like this: 例如,如果您想从图像中裁剪10px,可以使用以下内容执行此操作:

<div style="width:[cropped width]; height:[height]; margin-left:-5px; overflow:hidden;">
  <img style="width:[true width]; height:[height];" src="path/to/thumbnail.jpg" />
</div>

The overflow:hidden crops the image, and the negative margin centers it horizontally, basically. 溢出:隐藏作物图像,负边缘基本上水平居中。

I'm going to simplify what I see (eg the numbers I provide won't be 100%, down-to-the-pixel accurate), but it will give you one method for accomplishing this. 我将简化我看到的内容(例如,我提供的数字不是100%,像素精确到准),但它会为您提供一种方法来实现这一目标。

When I look at the screenshot, what I see is a list of images that are approximately 4x3 aspect ratio (or 3x4 in reverse). 当我看截图时,我看到的是大约4x3宽高比(或反向3x4)的图像列表。 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. 然而,总体效果是G +可以从大量图像中提取,因此,可以选择满足各个纵横比(给定图像的横向或纵向)的需求的图像组合,以及包含矩形的整体宽高比。

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: 从截图中我们看到三个这样的例子:

  1. 1st row = 4 landscape photos 第1行= 4张风景照片
  2. 2nd row = 2 landscape photos, 2 portrait photos, and 1 square photo 第2行= 2张风景照片,2张肖像照片和1张方形照片
  3. 3rd row = 3 landscape photos, 1 portrait photo, and 1 square photo 第3行= 3张风景照片,1张肖像照片和1张方形照片

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: 所以,我认为解决这个特殊问题基本上是解决4个子问题:

  1. Compute the aspect ratios of all photos in the available "photo pool" 计算可用“照片池”中所有照片的宽高比
  2. Make a list of all combinations of photos' aspect ratios that result in the desired width (to make a single row) 列出所有照片宽高比的组合,以产生所需的宽度(制作单行)
  3. From the pool of photos available, figure out which photos you can combine into which valid combinations, resulting in a single, composed row of images 从可用的照片池中,找出哪些照片可以组合成哪些有效组合,从而产生一组合成的图像
  4. Finally, steps 1-3 create a single row of images. 最后,步骤1-3创建单行图像。 In order to get the overall rectangle, you just use steps 1-3 to create as many rows of images as you like, and then stack them all on top of one another. 为了获得整个矩形,您只需使用步骤1-3创建任意数量的图像行,然后将它们全部叠加在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM