【问题标题】:Trying to think of a way to dynamically add text for each image想办法为每张图片动态添加文字
【发布时间】:2013-02-05 15:35:01
【问题描述】:

我正在想办法实现这一点。

我使用 jQuery 幻灯片来显示图像,并使用一个简单的 php 脚本根据用户选择的画廊从文件夹中提取图像:

    $handle = opendir(dirname(realpath(__FILE__)).'/../images/gallery/'.$gallery);
while($file = readdir($handle)){
    if($file !== '.' && $file !== '..'){
        $pictures .= '<img src="images/gallery/'.$gallery.'/'.$file.'" alt="" />';
    }
}

我这样做是因为有 6 个不同的画廊,每个画廊的照片数量各不相同,大约 25 到 40 张图片。如果我静态地做这个网站,或者有一个数据库运行它就没有问题,但我想不出用 PHP 的方式来做这件事。

基本上这是我希望做的一个例子:I hard coded all the images and text

但是在this page我想不出一个好的方法

【问题讨论】:

  • ALT 文本从何而来?
  • 这不是说“替代文字...”它更像是关于图像特定图像的“绒毛文字”...而且并非每张图像都会有文字。

标签: php jquery


【解决方案1】:

您可以将描述添加到每个文件夹文件中,例如 info.txt。在加载时检查 file_exists() 并从文件中获取数据。

如果你想为每张照片设置 desc,你可以这样做:

对于文件:

  • 花.jpg
  • cloud.jpg
  • girl.jpg

info.txt:

flower.jpg::This is flower
cloud.jpg::This is cloud
girl.jpg::This is girl

所以你阅读 info.txt,按行分解,最后分解行。结果,您将获得包含描述和文件名的数组。

【讨论】:

  • 对此的一种变体是每个图像都有一个文本文件。因此,如果您有flower.jpg,您可以有flower.jpg.txt,其中包含该单个图像的描述。 (而不是一个目录中所有图像的一个文本文件)。
  • 所以基本上做一些类似的事情:"data-caption="#___ID__"" 进入图像标签?
  • 有这样的工作示例,所以我可以看看它是如何工作的?
【解决方案2】:

可能不是最好的方法,但这是我完成它的解决方法:` 我重命名了图像: 01-一只鸟,02-一只猫

$handle = opendir(dirname(realpath(__FILE__)).'/../images/gallery/'.$gallery);
while($file = readdir($handle)){
    if($file !== '.' && $file !== '..'){

        $alt = substr($file, 3, -4); //Removes the "##-" and the ".jpg"
        $id = substr($file, 0, 2); // Removes EVERYTHING after the "##"

    // The image
        $pictures .= '<img src="images/gallery/'.$gallery.'/'.$file.'" alt="'.$alt.'" data-caption="#cap'.$id.'" />';
    // The text
        $span .= '<span class="caption" id="cap'.$id.'">'.$alt.'</span>';

    }
}`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多