如何使用php获得图像标题


how to get image caption using php

我使用PHP将给定文件夹中的所有图像放入幻灯片。这在下面的代码中工作得很好:

<?php
//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");
$imgs = array();
// create array
foreach($images as $image){ $imgs[] = "$image"; }
/****** Display Images ******/
foreach ($imgs as $img) {
    //I would like to get the image title here, to put in the echo below    
    echo "<div><img src='$img' border='0' width='"100%'" height='"100%'"/ /></div>"; 
    }
?>

这一切都很容易,但由于我现在还想添加图片的标题作为说明文字,我需要从图像中提取/获取这些信息。

我想我可以得到它沿着功能exif_read_data的线条,但我不太确定如何获得标题,而不是所有的元数据…


在使用stackoverflow的聪明人的一点帮助下,这是最终的和功能的结果,正如我下面的回答所看到的那样,也是由几个答案的零零碎落组成的。

<?php
/*
This script will print the image files in a given folder, along with their image description (title in windows file explorer).
*/
// Set the directory where the files reside
$directory = $GLOBALS['directory'];
//get all image files with a .jpg extension.
$imagenpath = glob("" . $directory . "*.jpg");
$imgs = array();
// create array
foreach($imagenpath as $image){ $imgs[] = "$image"; }
// Print each image file with the ImageDescription (the title/caption) in the ALT field
foreach ($imgs as $img) {
    $exif_data = exif_read_data($img, 'IFD0');
    $exif_description = "";
    if (!empty($exif_data['ImageDescription'])) 
        $exif_description = $exif_data['ImageDescription'];
    echo "<div><img src='$img' alt='$exif_description' border='0' width='"100%'" height='"100%'"/ /></div>";
}
?>

这是可以做的一切,因为大多数图片不支持这个函数,但是,你做错了,因为你得到的错误是因为没有找到文件,当你找到文件时,你得到的错误是这样的:

警告:exif_read_data(File .png): File not supported in/path/to/file/file.php on line x

,这是因为你在

中单引号引用了变量
    $exif_data = exif_read_data($img, 'IFD0');

代码可以是更少的字符,这是我的解决方案:

<?php
/*
This script will print the image files in a given folder, along with their image description (title in windows file explorer).
*/
// Set the directory where the files reside
$directory = $GLOBALS['directory'];
//get all image files with a .jpg extension.
$imgs = glob($directory . "*.jpg");
// Print each image file with the ImageDescription (the title/caption) in the ALT field
foreach ($imgs as $img) {
    $exif_data = exif_read_data($img, 'IFD0');
    if (!empty($exif_data['ImageDescription'])) 
        $exif_description = $exif_data['ImageDescription'];
    echo "<div><img src='"$img'" alt='"$exif_description'" border='"0'" width='"100%'" height='"100%'"/ /></div>";
}
?>

试试这个:-

foreach (glob("*.jpg") as $filename) {
    $name = basename($filename, '.jpg');
    echo "<div><img src='$filename' title='$name' alt='$name' border='0' width='"100%'" height='"100%'"/ /></div>";
}

为了解决这个问题,我基本上使用了Windows文件浏览器,选择图像文件,在文档注释部分输入文本。
然后我按照上面@RamRaider的建议打印了$exif_data,发现这篇文章最终出现在文件的ImageDescription中。

在@Abdallah Samman的帮助下,一旦我确定了脚本中的错误,就不再有太多了!

谢谢大家……

下面的代码很漂亮:

<?php
/*
This script will print the image files in a given folder, along with their image description.
*/
// Set the directory where the files reside
$directory = $GLOBALS['directory'];
//get all image files with a .jpg extension.
$imagenpath = glob("" . $directory . "*.jpg");
$imgs = array();
// create array
foreach($imagenpath as $image){ $imgs[] = "$image"; }
// Print each image file with the ImageDescription (the caption) in the ALT field
foreach ($imgs as $img) {
    $exif_data = exif_read_data($img, 'IFD0');
    $exif_description = "";
    if (!empty($exif_data['ImageDescription'])) 
        $exif_description = $exif_data['ImageDescription'];
    echo "<div><img src='$img' alt='$exif_description' border='0' width='"100%'" height='"100%'"/ /></div>";
}
?>