read picture folders, sort them into sets and output them with a loop

how can i do it ever takes the right picture sets

here the picture directory (img):

  • example_start.jpg

  • example-A_small.jpg

  • example-A_small_2x.jpg

  • example-A_medium.jpg

  • example-A_medium_2x.jpg

  • example-A_large.jpg

  • example-A_large_2x.jpg

  • example-B_small.jpg

  • example-B_small_2x.jpg

  • example-B_medium.jpg

  • example-B_medium_2x.jpg

  • example-B_large.jpg

  • example-B_large_2x.jpg

     // Get Pictures
     $imgDir = 'img';
     $imgs = glob($imgDir.'/*{[!_start*].jpg}', GLOB_BRACE); // without _start image!
     asort($imgs); // sort: ABC…
    

caption.php file:

$imgCaption = array('example-A is nice','example-B is blue');



require_once 'caption.php'; // picture caption data
    foreach($imgs as $files => $img){
        if(!isset($imgCaption[$files])){$imgCaption[$files] = '';}
             echo'
                  <img
                    src="'.$_SERVER['REQUEST_URI'].$img.'"
                    srcset="
                      '.$_SERVER['REQUEST_URI'].$img.' 734w,
                      '.$_SERVER['REQUEST_URI'].$img.' 1472w,
                      '.$_SERVER['REQUEST_URI'].$img.' 1068w,
                      '.$_SERVER['REQUEST_URI'].$img.' 2136w,
                      '.$_SERVER['REQUEST_URI'].$img.' 2016w,
                      '.$_SERVER['REQUEST_URI'].$img.' 4032w," 
                    sizes="(min-width: 640px) 50vw, 100vw" width="100%" height="100%"   
                    alt="'.$imgCaption[$files].'">
                ';

expected Output

 <img
   src="img/example_A-small.jpg"
   srcset="
     img/example-A_small.jpg 734w,
     img/example-A_small_2x.jpg 1472w,
     img/example-A_medium.jpg 1068w,
     img/example-A_medium_2x.jpg 2136w,
     img/example-A_large.jpg 2016w,
     img/example-A_large_2x.jpg 4032w," 
   sizes="(min-width: 640px) 50vw, 100vw" width="100%" height="100%" alt="Example A">
 <img
   src="img/example_B-small.jpg"
   srcset="
     img/example-B_small.jpg 734w,
     img/example-B_small_2x.jpg 1472w,
     img/example-B_medium.jpg 1068w,
     img/example-B_medium_2x.jpg 2136w,
     img/example-B_large.jpg 2016w,
     img/example-B_large_2x.jpg 4032w," 
   sizes="(min-width: 640px) 50vw, 100vw" width="100%" height="100%" alt="Example A">

I find it easier just to have separated directories for the images and do something like the following:

function grabFilenames($dir) {
    if (is_dir($dir)) {
        $handle = opendir($dir);
        while (false !== ($file = readdir($handle))) {
            if (is_file($dir . '/' . $file) && is_readable($dir . '/' . $file)) {
                $fileNames[] = $file;
            }
        }
        closedir($handle);
    } else {
        echo "<p>There is an directory read issue</p>";
    }
    return $fileNames;
}

$images = grabFilenames('assets/large');
$thumbs = grabFilenames('assets/thumbnails');

Oops I modified the template just to read the info from a database table…me bad
Anyways here’s two different ways of doing it…

<div id="gallery" class="picture-box" data-total="{$journal|count}">
    <div class="frame">
        {counter start=-1 skip=1 print=false}
        {$x = 1}
        {foreach $journal as $cms}
            <a id="package{$x}" class="myLightBox" href="{$cms.image_path}" title="{$cms.heading}" data-picture="{counter}" 
               data-exif="{if $cms.Model}{$cms.Model}   ---   {$cms.FocalLength}    {$cms.Aperture}    {$cms.ISO}    {$cms.ExposureTime}{/if}">
                <img class="box" src="{$cms.thumb_path}" alt="Picture for Journal Entry">
            </a>
            {$x = $x+1}
        {/foreach}
    </div>
</div>

I use a smarty templated system, but I think you will be able to get the drift.
Here’s a link to the actual page: https://www.miniaturephotographer.com/gallery.php

HTH John, but it probably didn’t … me bad again. :pensive:

Me again if the jpegs have an EXIF file you can get the data from it in php.

Here’s a test script that I did:

<?php

$file_name = 'assets/large/img-photos-1554676248.jpg';
//$file_name = 'assets/large/img-photos-1554676248.jpg';
$exif_data = exif_read_data($file_name);
echo "<pre>" . print_r($exif_data, 1) . "</pre>";
if ($exif_data['Model']) {
    $photos[] = [
        'FileName' => $exif_data['FileName'],
        'Model' => "Sony " . $exif_data['Model'],
        'ExposureTime' => $exif_data['ExposureTime'] . "s",
        'Aperture' => "f/" . intval($exif_data['FNumber']),
        'ISO' => $exif_data['ISOSpeedRatings'],
        'FocalLength' => $exif_data['FocalLengthIn35mmFilm'] . "mm",
    ];
}

echo "<pre>" . print_r($photos, 1) . "</pre?";

Here’s a process file of my website, this is where I upload my images:

<?php

require_once '../private/initialize.php';

use Library\ProcessImage\ProcessImage as Process;
use Library\Resize\Resize;
use Library\Journal\Journal;
use Library\CMS\CMS;
use Library\Users\Users;
use Library\Read\Read;

$result = false;
$fetchUsername = new Users;
//echo $_SESSION['username'] . "<br>";
$username = $fetchUsername->username();

$journal = new Journal();
/*
 * If user is updating blog or home page.
 */
$update = filter_input(INPUT_POST, 'action', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
if (isset($update) && $update === 'update') {
    $data['id'] = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
    $data['author'] = $username;
    $data['page_name'] = filter_input(INPUT_POST, 'page_name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    $data['heading'] = filter_input(INPUT_POST, 'heading', FILTER_DEFAULT);
    $data['content'] = filter_input(INPUT_POST, 'content', FILTER_DEFAULT);
    $data['rating'] = filter_input(INPUT_POST, 'rating', FILTER_SANITIZE_NUMBER_INT);
    $data['category'] = filter_input(INPUT_POST, 'category', FILTER_SANITIZE_FULL_SPECIAL_CHARS);

    $result = $journal->update($data);
    if ($result) {
        header("Location: " . $result);
        exit();
    }
}
$upload = filter_input(INPUT_POST, 'action', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$data['user_id'] = filter_input(INPUT_POST, 'user_id', FILTER_SANITIZE_NUMBER_INT);
$data['author'] = $username;
$data['page_name'] = filter_input(INPUT_POST, 'page_name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$data['heading'] = filter_input(INPUT_POST, 'heading', FILTER_DEFAULT);
$data['content'] = filter_input(INPUT_POST, 'content', FILTER_DEFAULT);
$data['rating'] = filter_input(INPUT_POST, 'rating', FILTER_SANITIZE_NUMBER_INT);
$data['category'] = filter_input(INPUT_POST, 'category', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
//echo "<pre>" . print_r($data, 1). "</pre>";

if ($_FILES['file']['error'] !== 4) {
    $file = $_FILES['file']; // Assign image data to $file array:
    $thumbImage = $_FILES['file'];

    $imgObject = new Process($file, 'photos-');
    $check['image_status'] = $imgObject->processImage();
    $check['file_type'] = $imgObject->checkFileType();
    $check['file_ext'] = $imgObject->checkFileExt();


    $exif_data = exif_read_data($imgObject->saveIMG());
    if ($exif_data['Model']) {
        $data['Model'] = "Sony " . $exif_data['Model'];
        $data['ExposureTime'] = $exif_data['ExposureTime'] . "s";
        $data['Aperture'] = $exif_data['COMPUTED']['ApertureFNumber'];
        $data['ISO'] = "ISO " . $exif_data['ISOSpeedRatings'];
        $data['FocalLength'] = $exif_data['FocalLengthIn35mmFilm'] . "mm";
    }

    //echo "<pre>" . print_r($data, 1) . "</pre?";


    //echo "<pre>" . print_r($check, 1) . "</pre>\n";
    if (in_array(TRUE, $check)) {
        $errMsg = "There's something wrong with the image file!<b>";
    } else {
        $data['image_path'] = $imgObject->saveIMG();

        // *** 1)  Create a new instance of class Resize:
        $resizePic = new Resize($data['image_path']);
        // *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
        $resizePic->resizeImage(2048, 1365, 'exact');
        // *** 3) Save image to directory:
        $resizePic->saveImage($data['image_path'], 100);
    }

    $thumbObject = new Process($thumbImage, 'photos-', false);
    $check['image_status'] = $thumbObject->processImage();
    $check['file_type'] = $thumbObject->checkFileType();
    $check['file_ext'] = $thumbObject->checkFileExt();
    //echo "<pre>" . print_r($check, 1) . "</pre>\n";
    if (in_array(TRUE, $check)) {
        $errMsg = "There's something wrong with the image file!<b>";
    } else {
        $data['thumb_path'] = $thumbObject->saveIMG();
        copy($data['image_path'], $data['thumb_path']);
        // *** 1)  Create a new instance of class Resize:
        $resizePic = new Resize($data['thumb_path']);
        // *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
        $resizePic->resizeImage(600, 400, 'exact');
        // *** 3) Save image to directory:
        $resizePic->saveImage($data['thumb_path'], 100);
        /*
         * Save all the data from the form to the database table: cms
         */
        $result = $journal->create($data);
        if ($result) {
            header("Location: " . $result);
            exit();
        }
    }
}
Sponsor our Newsletter | Privacy Policy | Terms of Service