要获取照片的详细信息,需要使用PHP中的Exif扩展库。以下是一个示例代码,它将获取照片的文件名、拍摄时间、拍摄设备、GPS信息等详细信息:

<?php
$filename = 'photo.jpg'; // 照片文件名

if (file_exists($filename)) {
    $exif = exif_read_data($filename, 0, true); // 读取Exif信息

    // 获取照片的拍摄时间
    $datetime = isset($exif['EXIF']['DateTimeOriginal']) ? $exif['EXIF']['DateTimeOriginal'] : '';

    // 获取照片的拍摄设备
    $make = isset($exif['IFD0']['Make']) ? $exif['IFD0']['Make'] : '';
    $model = isset($exif['IFD0']['Model']) ? $exif['IFD0']['Model'] : '';
    $device = $make . ' ' . $model;

    // 获取照片的GPS信息
    $latitude_ref = isset($exif['GPS']['GPSLatitudeRef']) ? $exif['GPS']['GPSLatitudeRef'] : '';
    $latitude = isset($exif['GPS']['GPSLatitude']) ? $exif['GPS']['GPSLatitude'] : '';
    $longitude_ref = isset($exif['GPS']['GPSLongitudeRef']) ? $exif['GPS']['GPSLongitudeRef'] : '';
    $longitude = isset($exif['GPS']['GPSLongitude']) ? $exif['GPS']['GPSLongitude'] : '';

    // 将经纬度转换为度分秒格式
    if (!empty($latitude) && !empty($longitude)) {
        $lat_degrees = count($latitude) > 0 ? gps2Num($latitude[0]) : 0;
        $lat_minutes = count($latitude) > 1 ? gps2Num($latitude[1]) : 0;
        $lat_seconds = count($latitude) > 2 ? gps2Num($latitude[2]) : 0;
        $lat_direction = ($latitude_ref == 'N') ? '北' : '南';

        $lon_degrees = count($longitude) > 0 ? gps2Num($longitude[0]) : 0;
        $lon_minutes = count($longitude) > 1 ? gps2Num($longitude[1]) : 0;
        $lon_seconds = count($longitude) > 2 ? gps2Num($longitude[2]) : 0;
        $lon_direction = ($longitude_ref == 'E') ? '东' : '西';

        $latitude = $lat_degrees . '°' . $lat_minutes . '′' . $lat_seconds . '″' . $lat_direction;
        $longitude = $lon_degrees . '°' . $lon_minutes . '′' . $lon_seconds . '″' . $lon_direction;
    } else {
        $latitude = '';
        $longitude = '';
    }

    // 输出照片的详细信息
    echo '文件名:' . $filename . '<br>';
    echo '拍摄时间:' . $datetime . '<br>';
    echo '拍摄设备:' . $device . '<br>';
    echo 'GPS信息:' . $latitude . ' ' . $longitude . '<br>';
} else {
    echo '照片不存在!';
}

// 将GPS坐标转换为度分秒格式
function gps2Num($coordPart) {
    $parts = explode('/', $coordPart);
    if (count($parts) <= 0)
        return 0;
    if (count($parts) == 1)
        return $parts[0];
    return floatval($parts[0]) / floatval($parts[1]);
}
?>

注意:Exif扩展库需要在PHP中开启才能使用。可以在php.ini文件中设置extension=php_exif.dll,或者在运行时使用phpinfo()函数查看是否开启了Exif扩展库。

写一个PHP获取照片详细信息

原文地址: http://www.cveoy.top/t/topic/bSjQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录