【问题标题】:Filename image as Automatic caption文件名图像作为自动标题
【发布时间】:2012-02-13 22:15:57
【问题描述】:

我正在使用this script 显示文件夹中的随机图像。我有数百张具有重要文件名的图像,因此我正在寻找一种方法将每个图像文件名显示为图像标题。这可能吗?

这是我使用的脚本:

<?php
/*
By Matt Mullenweg > http://photomatt.net
Inspired by Dan Benjamin > http://hiveware.com/imagerotator.php
Latest version always at:

http://photomatt.net/scripts/randomimage

*/// Make this the relative path to the images, like "../img" or "random/images/".
// If the images are in the same directory, leave it blank.
$folder = '';

// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';

$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';

$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along

header('Location: '.$folder.$files[$rand]); // Voila!
?>

【问题讨论】:

    标签: php random rotator captions


    【解决方案1】:

    如果您只想继续显示单个随机图像,则删除(或注释掉,以便以后可以恢复它)标题行并在文件末尾添加以下行:

    //header('Location: '.$folder.$files[$rand]); // Voila!
    echo $files[$rand];
    ?>
    <br />
    <img src="<?php echo $folder.$files[$rand] ?>" />
    

    您可能想要添加一些 css 样式来设置图像的显示大小,并设置其上方的标题。

    【讨论】:

    • 嗨,这在浏览器中运行良好。十分感谢。但是如果我们像普通图片一样调用脚本就不行了,例如:,这是正常的在我的网站中显示随机图片。
    • 好的,我明白了。带有 img 标签的页面是 php 脚本吗?如果不是,那么不使用 iframe 而不仅仅是 img 标签,您将无法同时显示图像及其标题
    • 我在 Drupal 块中使用它,我可以将输入格式设置为 PHP 或 HTML,但由于未知原因,直接在 PHP 块中使用脚本会导致我的网站崩溃;所以我会尝试 iframe 解决方案。
    猜你喜欢
    • 2012-12-24
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    相关资源
    最近更新 更多