views:

37

answers:

2

I have the following code to output images from directory im/, how can I tweak this to also output images from another diectory called out/ (for example)? As to echo another img tag under the current one?

<?php
    $imgDir = "im/";

    $images = scandir($imgDir); 
    $ignore = array( ".", ".." ); 

    natsort($images);

    foreach($images as $file)
       {
    if(!in_array($file, $ignore))
       {
    echo "<div id=\"slideWrapper\">\n";
    echo "<img src=\"im/$file\" width=\"1000\" height=\"683\" alt=\"$files\" />\n";
    echo "</div>\n";
    };
    }
?>
A: 

Hi,

i would do it the following way - exchanged $imgDir for an array. The $images array now contains both path and filenames.

<?php
    $imgDirs = array("im/", "out/");
    $images = array();

    // take one of the given directories
    foreach($imgDirs as $imgDir)
    {
       // open a directory reader for this given directory
       $dh = opendir($imgDir);
       // read a single filename of this directory (stop loop if there is no more file)
       while (false !== ($filename = readdir($dh))) 
       {
           // ignore '.' and '..'
           if($filename != '.' && $filename != '..')
           {
              // add the directory and filename to the images array
              // all images, regardless of the folder, are stored in one array :)
              $images[] = $imgDir . $filename;
           }
       } 
       closedir($dh);
    }

    natsort($images);

    // loop for every image
    foreach($images as $file)
    {
       // for every img, echo a div-img-/div-combination
       echo "<div id=\"slideWrapper\">\n";
       echo "<img src=\"$file\" width=\"1000\" height=\"683\" alt=\"$file\" />\n";
       echo "</div>\n";
    }
?>
henchman
if you'd like to retrieve the image dimensions from file, please use: http://php.net/manual/de/function.getimagesize.php.
henchman
Ok that makes sense thanks! But if I was to output osmething like this :<div><img src="im/" /><img src="out/" /></div>As i need to have one image of each directory in two seperate img tags?
Turbodurso
with the above code, you get a div-img-/div-combination for every image, regardless of the folder they are in. i added comments for clarification.do you need to group the images?
henchman
Yep! and thanks for clarifying. I need a div-img(from im/)-img(fom out/)-div. For example for every image named x.jpg in one folder the image names x1.jpg from the other folder is echoed underneath.
Turbodurso
and it is guaranteed that for every x.jpg in folder 1, a x1.jpg in folder 2 exists?
henchman
Yes, could be 1 or t or anything. I know this might not be the best way around it but ultimately i need to output two images that go together (by filename or else) and print them out so that they are in the same div
Turbodurso
A: 

Hi,

believe this would fit your needs.

<?php
    $imgDir = 'im/';
    $imgDir2 = 'out/';
    $images = array();

    // open a directory reader for the first directory
    $dh = opendir($imgDir);
    // read a single filename of this directory (stop loop if there are no more files)
    while (false !== ($filename = readdir($dh))) 
    {
        // ignore '.' and '..'
        if($filename != '.' && $filename != '..')
        {
           // add the directory and filename to the images array
           $images[] = $filename;
        }
    }
    closedir($dh);

    natsort($images);

    // loop for every image
    foreach($images as $image)
    {
       // for every img, echo a div-2-images-div-combination
       echo '<div id="slideWrapper">';
       echo '<img src="'.$imgDir . $image.'" width="1000" height="683" alt="'.$image.'" />';
       echo '<img src="'.$imgDir2 . $image.'" width="1000" height="683" alt="'.$image.'" />';
       echo '</div>';
    }
?>

I would recommend using single quotes, as you don't have to escape normal quotes and it's faster :)

henchman
Thank you for your time henchman, slowly getting a grasp of all of this!
Turbodurso