duanrong5167 2013-02-18 03:31
浏览 86
已采纳

如何获得灯箱图片的标题

I am using a php lightbox (this one - http://www.fatbellyman.com/webstuff/lightbox_gallery/index.php) to automatically display thumbnails and images. I would like to have the titles and image descriptions display on the larger lightbox images, pulled from a separate list (text file?). If it is possible to do this without using a database that would be best.

This is on my gallery page:

<?php include 'lightbox_display_function.php'; ?>
<?php lightbox_display('apparel/soiled/icarus/', 'lightbox[icarus]'); ?>

And this is the lightbox_display_function.php:

<?php function lightbox_display($dir_to_search, $rel){
        $image_dir = $dir_to_search;
        $dir_to_search = scandir($dir_to_search);
        $image_exts = array('gif', 'jpg', 'jpeg', 'png');
        $excluded_filename = '_t';
            foreach ($dir_to_search as $image_file){
            $dot = strrpos($image_file, '.');
            $filename = substr($image_file, 0, $dot);
            $filetype = substr($image_file, $dot+1);
            $thumbnail_file = strrpos($filename, $excluded_filename);
                if ((!$thumbnail_file) and array_search($filetype, $image_exts) !== false){
echo "<a href='".$image_dir.$image_file."' rel='".$rel."'>
<img src='".$image_dir.$filename."_t.".$filetype."' alt='".$filename."' width='70' height='70' title='$filename'/>
</a>"."
";
                }
            }
    }
  • 写回答

1条回答 默认 最新

  • dongne1560 2013-02-18 14:11
    关注

    Do you have some sample code of how you have the page structured at the moment, off the top of my head could you not hard code them into an associative array of some sort?

    Have the image file name be the key, so that you can pair up titles with the necessary file?

    If you post what you have so far, we could have a look and hopefully make suggestions.

    -- Update --

    Hardcore the array for the page before you call it, and include the array as a parameter so the elements in it can be paired up with the appropriate images in the function.

    <?php include 'lightbox_display_function.php'; ?>
    <?php
    $titleArray=array();
    $titleArray['RedCar.jpg']="Picture of red car";
    $titleArray['BlueCar.jpg']="Picture of blue car";
    $titleArray['GreenCar.jpg']="Picture of green car";
    lightbox_display('apparel/soiled/icarus/', 'lightbox[icarus]',$titleArray); 
    ?>
    

    So now in the function we have the information to work with

        <?php function lightbox_display($dir_to_search, $rel,$titleArray){
                $image_dir = $dir_to_search;
                $dir_to_search = scandir($dir_to_search);
                $image_exts = array('gif', 'jpg', 'jpeg', 'png');
                $excluded_filename = '_t';
                    foreach ($dir_to_search as $image_file){
                    $dot = strrpos($image_file, '.');
                    $filename = substr($image_file, 0, $dot);
                    $filetype = substr($image_file, $dot+1);
                    $thumbnail_file = strrpos($filename, $excluded_filename);
    
                    $title="";
                    if (array_key_exists($filename, $titleArray)) { //check if the filename matches a key in the array
                       $title = $titleArray[$filename]; //match, so set $title to equal the title set for that image name in the array
                    }
    
                    if ((!$thumbnail_file) and array_search($filetype, $image_exts) !== false){
                         //At this point $title can be included in the echo or used in some fashion, put is paired up with the appropriate image.
                         echo "<a href='".$image_dir.$image_file."' rel='".$rel."'>
                         <img src='".$image_dir.$filename."_t.".$filetype."' alt='".$filename."' width='70' height='70' title='$filename'/>
                         </a>"."
    ";
                        }
                    }
            }
    

    I haven't tested that but it should give you something to work with, theory if nothing else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c