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.

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

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏