简体   繁体   中英

Displaying an image gallery from a folder of images automatically

I am working on an image gallery for a client and have run across some trouble. I don't want the client to have to change code at all so what I'm trying to do is make it so they simply have to put whatever images they want into the folder and the code will automatically add everything to the gallery.

I have come up with a few solutions so far but am stuck with what to do next.

The gallery plugin I want to use is Magnific Popup located here: http://dimsemenov.com/plugins/magnific-popup/

The problem is that I need to pass every image url to the plugin but I can't hard code the image urls because I have no idea what they will be.

What I've tried so far is based off this guide to get the path for all of the image files using php: https://daveismyname.com/creating-an-image-gallery-from-a-folder-of-images-automatically-bp

The main issue is that I don't know how to pass a php array of urls to a js array.

This is my php code so far:

<?php
        $dirname = "images/gallery/";
        $images = scandir($dirname);
        shuffle($images);
        $ignore = Array(".", "..");
        foreach($images as $curimg){
            if(!in_array($curimg, $ignore)) {
                images.push({src: ".$dirname.$curimg"});
            }
        }
    ?>

images is a js array that i defined previously:

 var images = [];

What can I do to pass my php array to js array? Or if this isn't the best method to do this, I would love to learn.

Edit: Here is my code so far since some were asking. As of now I'm just trying to get the images to load. I can add everything else I need and make it look pretty after I get this working.

<!DOCTYPE html>
<html>
<!-- Magnific Popup core CSS file -->
<link rel="stylesheet" href="css/magnific-popup.css">
<head>

    <title></title>
</head>
<body>



    <ul>
        <?php
            $dirname = "images/gallery/";
            $images = scandir($dirname);
            echo json_encode($images);
            /**shuffle($images);
            $ignore = Array(".", "..");
            foreach($images as $curimg){
                if(!in_array($curimg, $ignore)) {
                    images.push({src: ".$dirname.$curimg"});

                }
            }
            */
        ?>


    </ul>

    <script type="text/javascript">
    var imgs = eval('<?php echo json_encode($images) ?>');
    for(var i = 0; i < imgs.length; i++) {

        document.write("<img src=imgs[i]>");
        document.write("imgs[i]");
        console.log(imgs[i]);
    }
    </script>



    <footer>
        <!-- jQuery 1.7.2+ or Zepto.js 1.0+ -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

        <!-- Magnific Popup core JS file -->
        <script src="magnific-popup.js"></script>
    </footer>

</body>
</html>

To pass an array from PHP to JavaScript you should convert it to JSON

PHP

<?php
    $dirname = "images/gallery/";
    $images = scandir($dirname);
    shuffle($images);
    $ignore = Array(".", "..");
    $array = array();
    foreach($images as $curimg){
        if(!in_array($curimg, $ignore)) {
            $array[] = $dirname.$curimg;
        }
    }
?>

JavaScript

   <script>    
    var array = <?php echo json_encode($aray) ?>;
    </script>

You have a number of choices. You should understand that you can put the PHP section in the middle of a script (Javascript) tag as shown below. In the script section in the header below, a new array is created, and then push statements are created for each element.

Please also note that the body has a PHP section that prints the list directly and a script tag that prints using the Javascript variable created in the header.

I used the list of files in the document root of the Apache server because that should work on all web sites. If you load the page on your test site, look at the source code that the browser receives. You don't put the PHP code (material between ) in its own file. The entire file below is sent to the PHP module because the file type is php. The PHP module then replaces the sections between with the material it generates and sends the modified page to your browser.

You can add JSON and AJAX, but I don't think that that is where your problem lies, and it isn't necessary. Try running this page on your server (I used the name array_demo.php for the file.) and see if you can understand it. It runs fine on my development server, so it should run correctly on yours.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PHP demo with arrays</title>
<?php
$root = $_SERVER["DOCUMENT_ROOT"];
$list = scandir($root);
?>
<script>
<?php 
$number = count( $list);
print ("var jlist = new Array();");
foreach ($list as $item) {
if ($item == ".") { continue; }
if ($item == "..") { continue; }
print("jlist.push(\"$item\");");
}
?>
</script>
</head>
<body>
<h1>PHP demo with arrays</h1>
<p>In this example, PHP lists the contents
   of the files contained in the PHP variable $list</p>
<ul>
<?php
foreach($list as $item) {
if ($item == ".") { continue; }
if ($item == "..") { continue; }
print("<li> $item </li>");
}
?>
</ul>
<p>Printing the list via Javascript element created
   in script in header</p>
<ul>
<script>
for (var i = 0; i < jlist.length; i++) {
document.writeln("<li>" + jlist[i] + "</li>");
}
</script>
</ul>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM