【问题标题】:Take image sizes and generate gallery from folder获取图像大小并从文件夹生成图库
【发布时间】:2017-11-21 01:01:49
【问题描述】:

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

文件夹中的图片编号为 0-14,它显示图像,但我不知道如何从画廊 / 中获取每个图像的大小并将其放入: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"。

【问题讨论】:

    标签: php html photoswipe


    【解决方案1】:

    例如,您想使用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>';
      }
    ?>
    

    【讨论】:

    • 添加后:if (!file_exists($dir.$i.'.jpg')) continue; list($width, $height, $type, $attr) = getimagesize($dir.$i.'.jpg'); 图片没有显示,所以我删除了:if (!file_exists($dir.$i.'.jpg')) continue; 我有错误:Warning: getimagesize(gallery/0.jpg): failed to open stream: No such file or directory in C:\wamp64\www\gallery.php on line 13 图片正在显示,所以目录是有效的,但如果它想获取图片大小,打不开这个。
    • @Rafaucau 这就是我添加它的原因,以便在尝试显示之前检查图像是否存在。检查您的路径是否正确。
    • 要使用这样的路径,图像必须在C:\wamp64\www\gallery\*.jpg 中,并且您还应该在其中有一个名为 thumbs 的文件夹。如果没有,则修复路径。
    • 在 .htaccess 中添加斜杠会导致问题。 HTML 已经看到了 PHP 之外的另一条道路。这就是显示图像的原因,但在获取尺寸时出现错误。所以我设置了不同的路径来获取图像大小并显示它。现在它起作用了。非常感谢您的帮助。
    猜你喜欢
    • 2019-07-28
    • 2013-12-19
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    相关资源
    最近更新 更多