简体   繁体   English

如何使用php获取图像标题

[英]how to get image caption using php

I'm using PHP to put all the images in a given folder into a slideshow. 我正在使用PHP将给定文件夹中的所有图像放入幻灯片中。 This works fine in the following code: 在以下代码中可以正常工作:

<?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>"; 
    }
?>

This all goes quite easy, but since I would now also like to add the Titles of the pictures as captions, I need to extract/get this information from the image. 这一切都非常容易,但是由于我现在还想添加图片的标题作为标题,因此我需要从图片中提取/获取此信息。

I'm thinking I can get it with something along the lines of the function exif_read_data , but I'm not quite sure how to get the title and not all the meta data . 我想我可以通过函数exif_read_data来获得它,但是我不太确定如何获得标题而不是所有元数据。 . .


With a little help from the smart people using stackoverflow, this is the final and functional result, as seen in my answer below as well and again, made with bits and pieces from several answers. 在使用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>";
}
?>

thats everything that can be done, because most pictures are not supported with this function, though, you are doing it wrong because the error that you are getting is because the file is not found, the error you get when the file is found and is not supported is this: 多数民众赞成在可以做的所有事情,因为此功能不支持大多数图片,但是您做错了,因为得到的错误是因为找不到文件,找到文件时出现的错误是不支持的是:

Warning: exif_read_data(file.png): File not supported in /path/to/file/file.php on line x 警告:exif_read_data(file.png):第x行上的/path/to/file/file.php不支持文件

and its because you single quoted the variable in 这是因为您单引号中的变量

    $exif_data = exif_read_data($img, 'IFD0');

the code could be less characters, this is my solution: 该代码可以减少字符,这是我的解决方案:

<?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>";
}
?>

Try this :- 尝试这个 :-

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>";
}

To solve the problem, I basically used the Windows file browser, selected the image file and entered the text in the document comments section. 为了解决该问题,我基本上使用Windows文件浏览器,选择了图像文件,然后在文档注释部分中输入了文本。
Then I printed the $exif_data as suggested above by @RamRaider and found out this text ends up in the ImageDescription of the file. 然后我按照上面$exif_data建议打印了$exif_data ,发现该文本最终出现在文件的ImageDescription中。

Once I nailed down the errors in my script, along with help from @Abdallah Samman, there wasn't much to it anymore! 一旦我确定了脚本中的错误以及@Abdallah Samman的帮助,就没有多大用处了!

Thank you everyone... 谢谢大家...

The following works beautifully: 以下工作精美:

<?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>";
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM