gpt4 book ai didi

php - 使用 PHP 提取 EXIF 数据

转载 作者:搜寻专家 更新时间:2023-10-31 21:09:02 24 4
gpt4 key购买 nike

我正在为需要照片库的客户建立一个网站,我打算使用文件名作为 alt 标签,但他希望我使用他在 EXIF 数据中输入的关键字 - 正如我没有摄影师 我真的不明白这的技术方面,但是,到目前为止我有一个脚本可以获取文件名,我希望它会像更改几行代码一样简单以获取 EXIF文件名。这是我的代码:

<?php
//The directory to your images folder, with trailing slash
$dir = "cms/gallery/photo/";

//Set the extensions you want to load, seperate by a comma.
$extensions = "jpeg,jpg";

//Set the number of images you want to display per page
$imagesPerPage = 3;

//Set the $page variable
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}

//Load all images into an array
$images = glob($dir."*.{".$extensions."}", GLOB_BRACE);

//Count the number of images
$totalImages = count($images);

//Get the total pages
$totalPages = ceil($totalImages / $imagesPerPage);

//Make sure the page you are on is not greater then the total pages available.
if($page > $totalPages){
//Set the currnet page to the total pages.
$page = $totalPages;
}

//Now find where to start the loading from
$from = ($page * $imagesPerPage) - $imagesPerPage;

//Now start looping
for($i = $from; $i < ($from + $imagesPerPage); $i++){
//We need to make sure that its within the range of totalImages.
if($i < $totalImages){
$filename = explode('.', basename($images[$i])); // GET EXIF DESCRIPTION AS $FILENAME
//Now we can display the image!
echo "

<div class='galleryCellHolder'>
<div class='galleryCell'>
<a class='fancybox' rel='group' href='{$images[$i]}'><img class='galleryPhoto' src='{$images[$i]}' alt='" . $filename[0] . "'></a>
</div>
</div>

";
}
}

//Now to display the page numbers!
for($p = 1; $p <= $totalPages; $p++){
if($p == $page){
$tmp_pages[] = "<a class='noPagination'>{$p}</a>";
}else{
$tmp_pages[] = "<a class='pagination' href='?page={$p}'>{$p}</a>";
}
}
?>
<div class="clearLeft"></div>
<div id="pagination">
<?php

//Now display pages, seperated by a hyphon.
echo "<br />" . implode("", $tmp_pages);

?>
</div>

最佳答案

这样做:

$filedata = exif_read_data($images[$i]);
if(is_array($filedata) && isset($filedata['ImageDescription'])){
$filename = $filedata['ImageDescription'];
} else{
$filename = explode('.', basename($images[$i]));
$filename = $filename[0];
}

如果 FileName 不在 exif 数据中,$filename 将包含路径中的文件名。

正确的名称可能在与 $filedata['ImageDescription'] 不同的变量中。例如,它也可能位于 $filedata['FileName']$filedata['Title'] 中。自己看看哪个有用

关于php - 使用 PHP 提取 EXIF 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25727488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com