将文件夹中的图像放入列表中并预览


Place images from a folder into list with preview

我需要为Joomla构建一个动态图片库插件!这将进入一个特定的文件夹,并从该文件夹中拉出所有图像,并首先将它们显示为大型预览图像,其余的显示在列表中。之后,如果单击,我需要在灯箱中打开预览图像,而在灯箱中,我还需要具有所列图像的小缩略图。

但是知道我只需要 php 转到该文件夹并从指定的文件夹中拉出所有图像。我已经用谷歌搜索了一些并找到了一些解决方案,但由于我不明白的原因,这不起作用。有人可以告诉我,代码有什么问题吗?

谢谢!

<div id="images">
        <?php 
        $images_dir = 'images/';
        $scan = scandir($images_dir);
        echo '<img src="' . $images_dir . $scan[2] . '"alt="image" />';
        ?>
        <ul id="smallimages">
        <?php
        for($i=0; $i<count($scan); $i++){
            $ext = substr($scan[$i], strpos($scan[$i], '.'), strlen($scan[$i]-1));
        $filetypes = array('.jpg', '.JPEG', '.jpeg');
        if(in_array($ext, $filetypes)){
            echo '<li><a href="' . $images_dir . $scan[$i] . '"><img src="' . $images_dir . $scan[$i] . '" alt="' . $scan[$i] . '"></a></li>';}         }?></ul>
</div>

我认为这是因为您没有正确定义路径。尝试使用以下方法:

$images_dir = JUri::root() . 'plugins/content/plg_new/images';

JUri::root()是Joomla网站的根,因此请根据图像所在的位置更改路径

希望这有帮助

这对你有用吗?

<?php
$image_directory="hrevert/images/";
$pictures = glob("$image_directory*.{gif,jpg,png}", GLOB_BRACE); 
//displays the first image
$first_img=reset($pictures);
echo "<img src='"".$first_img."'" width='20px' />"; 

//loops through other images and prints them
echo "<ul>";
foreach($pictures as $picture)  {
    echo "<a href='$picture'><img src='"".$picture."'" width='20px' /></a>"; 
}
echo "</ul>"
?>