获取图像大小并从文件夹生成图库

Take image sizes and generate gallery from folder

我想使用 Photoswipe 插件创建画廊,但我想自动显示缩略图和文件夹中的图像。 Photoswipe 需要图像的大小,所以我希望脚本获取每张图像的大小。

文件夹中的图片从 0-14 编号,它显示图片,但我不知道如何从 gallery/ 中获取每张图片的大小并将其放入:data-size="'.$imageSize.' ":

<?php
  $dir="gallery/";
  $thumbsDir="gallery/thumbs/";
  for($i=0; $i<=14; $i++)
  {
    echo 
      '<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
        <a href="'.$dir.$i.'.jpg" itemprop="contentUrl" data-size="'.$imageSize.'" data-index="'.$i.'">
          <img src="'.$thumbsDir.$i.'.jpg" width="412px" itemprop="thumbnail" class="img-responsive img-thumbnail">
        </a>
      </figure>';
  }
?>

这应该是这样的:data-size="1920x1080".

您想使用 getimagesize(),例如。

<?php
  $dir="gallery/";
  $thumbsDir="gallery/thumbs/";
  for($i=0; $i<=14; $i++)
  {
    if (!file_exists($dir.$i.'.jpg')) continue;
    list($width, $height, $type, $attr) = getimagesize($dir.$i.'.jpg');

    echo 
      '<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
        <a href="'.$dir.$i.'.jpg" itemprop="contentUrl" data-size="'.$width.'x'.$height.'" data-index="'.$i.'">
          <img src="'.$thumbsDir.$i.'.jpg" width="412px" itemprop="thumbnail" class="img-responsive img-thumbnail">
        </a>
      </figure>';
  }
?>