1

I want to create Gallery using Photoswipe plugin but I want to show thumbs and images automatically form folder. Photoswipe requires the size of the image, so I want the script to take the size of each image.

Pictures in folder are numbered from 0-14 and it showing images but I don't know how to take size of each image from gallery/ and put it to: 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>';
  }
?>

This should look like this: data-size="1920x1080".

Rafaucau
  • 323
  • 2
  • 20

1 Answers1

2

You want to make use of getimagesize(), for example.

<?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>';
  }
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • After added this : `if (!file_exists($dir.$i.'.jpg')) continue; list($width, $height, $type, $attr) = getimagesize($dir.$i.'.jpg');` Images are not showing, so i removed: `if (!file_exists($dir.$i.'.jpg')) continue;` And i have error: `Warning: getimagesize(gallery/0.jpg): failed to open stream: No such file or directory in C:\wamp64\www\gallery.php on line 13` Images are showing so directory is valid but if it want to get image size, it can't open this. – Rafaucau Nov 21 '17 at 01:21
  • @Rafaucau That's why I added it, so as to check the image exists before trying to display it. Check your path is correct. – Lawrence Cherone Nov 21 '17 at 01:29
  • To use that path like that, images must be in `C:\wamp64\www\gallery\*.jpg` and you should have a folder called thumbs in there also. If not then fix the paths. – Lawrence Cherone Nov 21 '17 at 01:31
  • Adding trailing slash in .htaccess caused the problem. HTML has seen another path than PHP. That's why the images were displayed, but there was error with taking size. So I set different paths to get the image size and to display it. Now it works. Thank You very much for your help. – Rafaucau Nov 21 '17 at 01:49