PHP code example of macocci7 / php-photo-gps

1. Go to this page and download the library: Download macocci7/php-photo-gps library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

macocci7 / php-photo-gps example snippets


    

    ci7\PhpPhotoGps\PhotoGps;

    $filename = 'img/with_gps.jpg';    // --\n";

    // Latitude in sexagesimal format
    echo "Latitude: " . $pg->latitudeS() . "\n";
    echo "緯度: " . $pg->lang('ja')->latitudeS() . "\n";

    // Longitude in sexagesimal format
    echo "Longitude: " . $pg->lang('eng')->longitudeS() . "\n";
    echo "経度: " . $pg->lang('ja')->longitudeS() . "\n";

    // Altitude as strings
    echo "Altitude: " . $pg->lang('eng')->altitudeS() . "\n";
    echo "高度: " . $pg->lang('ja')->altitudeS() . "\n";

    // Coord in decimal format ('S' and 'W' results in negative value.)
    echo "Coord: " . $pg->latitudeD() . ", " . $pg->longitudeD() . "\n";
    

    

    ci7\PhpPhotoGps\PhotoGps;

    $filename = 'img/with_gps.jpg';    // --\n";

    // Format: default
    echo "Current format [eng]: " . $pg->lang('eng')->format() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->format() . "\n\n";

    // Latitude in sexagesimal format
    echo "Latitude: " . $pg->lang('eng')->latitudeS() . "\n";
    echo "緯度: " . $pg->lang('ja')->latitudeS() . "\n";

    // Longitude in sexagesimal format
    echo "Longitude: " . $pg->lang('eng')->longitudeS() . "\n";
    echo "経度: " . $pg->lang('ja')->longitudeS() . "\n\n";

    echo "[Chang format:eng]---------------------------------\n";

    // Configure format
    $pg->lang('eng')->format('{ref:u}: {seconds:v}{seconds:u}, {minutes:v}{minutes:u}, {degrees:v}{degrees:u}');

    // Current format
    echo "Current format [eng]: " . $pg->lang('eng')->format() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->format() . "\n\n";

    // Latitude in sexagesimal format
    echo "Latitude: " . $pg->lang('eng')->latitudeS() . "\n";
    echo "緯度: " . $pg->lang('ja')->latitudeS() . "\n";

    // Longitude in sexagesimal format
    echo "Longitude: " . $pg->lang('eng')->longitudeS() . "\n";
    echo "経度: " . $pg->lang('ja')->longitudeS() . "\n\n";

    echo "[Change format:ja]---------------------------------\n";

    // Configure format
    $pg->lang('ja')->format('{seconds:v}{seconds:u}, {minutes:v}{minutes:u}, {degrees:v}{degrees:u} ({ref:u})');

    // Current format
    echo "Current format [eng]: " . $pg->lang('eng')->format() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->format() . "\n\n";

    // Latitude in sexagesimal format
    echo "Latitude: " . $pg->lang('eng')->latitudeS() . "\n";
    echo "緯度: " . $pg->lang('ja')->latitudeS() . "\n";

    // Longitude in sexagesimal format
    echo "Longitude: " . $pg->lang('eng')->longitudeS() . "\n";
    echo "経度: " . $pg->lang('ja')->longitudeS() . "\n\n";

    echo "[Reset format:ja]---------------------------------\n";

    // Reset format
    $pg->lang('ja')->resetFormat();

    // Current format
    echo "Current format [eng]: " . $pg->lang('eng')->format() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->format() . "\n\n";

    // Latitude in sexagesimal format
    echo "Latitude: " . $pg->lang('eng')->latitudeS() . "\n";
    echo "緯度: " . $pg->lang('ja')->latitudeS() . "\n";

    // Longitude in sexagesimal format
    echo "Longitude: " . $pg->lang('eng')->longitudeS() . "\n";
    echo "経度: " . $pg->lang('ja')->longitudeS() . "\n\n";
    

    

    ci7\PhpPhotoGps\PhotoGps;

    $filename = 'img/with_gps.jpg';
    $pg = new PhotoGps($filename);

    echo "[" . $filename . "]--------------------\n";

    // Format: default
    echo "Current format [eng]: " . $pg->lang('eng')->directionFormat() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->directionFormat() . "\n\n";

    // Image Direction: default format
    echo "Image Direction [eng]: " . $pg->lang('eng')->directionS() . "\n";
    echo "Image Direction [ja]: " . $pg->lang('ja')->directionS() . "\n\n";

    // Configure Format: eng
    $pg->lang('eng')->directionFormat('{degrees:v}{degrees:u}({ref})');

    // Current Format
    echo "Current format [eng]: " . $pg->lang('eng')->directionFormat() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->directionFormat() . "\n\n";

    // Image Direction: Current Format
    echo "Image Direction [eng]: " . $pg->lang('eng')->directionS() . "\n";
    echo "Image Direction [ja]: " . $pg->lang('ja')->directionS() . "\n\n";

    // Configure Format: ja
    $pg->lang('ja')->directionFormat('{degrees:v}{degrees:u}');

    // Reset Format: eng
    $pg->lang('eng')->resetDirectionFormat();

    // Current Format
    echo "Current format [eng]: " . $pg->lang('eng')->directionFormat() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->directionFormat() . "\n\n";

    // Image Direction: Current Format
    echo "Image Direction [eng]: " . $pg->lang('eng')->directionS() . "\n";
    echo "Image Direction [ja]: " . $pg->lang('ja')->directionS() . "\n\n";
    

    

    ci7\PhpPhotoGps\PhotoGps;

    $filename = 'img/with_gps.jpg';
    $pg = new PhotoGps($filename);

    echo "[" . $filename . "]--------------------\n";

    // Format: default
    echo "Current format [eng]: " . $pg->lang('eng')->speedFormat() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->speedFormat() . "\n\n";

    // Speed: default format
    echo "Speed [eng]: " . $pg->lang('eng')->speedS() . "\n";
    echo "Speed [ja]: " . $pg->lang('ja')->speedS() . "\n\n";

    // Configure Format: eng
    $pg->lang('eng')->speedFormat('{speed:v}({speed:u})');

    // Current Format
    echo "Current format [eng]: " . $pg->lang('eng')->speedFormat() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->speedFormat() . "\n\n";

    // Speed: Current Format
    echo "Speed [eng]: " . $pg->lang('eng')->speedS() . "\n";
    echo "Speed [ja]: " . $pg->lang('ja')->speedS() . "\n\n";

    // Configure Format: ja
    $pg->lang('ja')->speedFormat('時速{speed:v}マイル');

    // Reset Format: eng
    $pg->lang('eng')->resetSpeedFormat();

    // Current Format
    echo "Current format [eng]: " . $pg->lang('eng')->speedFormat() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->speedFormat() . "\n\n";

    // Speed: Current Format
    echo "Speed [eng]: " . $pg->lang('eng')->speedS() . "\n";
    echo "Speed [ja]: " . $pg->lang('ja')->speedS() . "\n\n";
    

    

    ci7\PhpPhotoGps\PhotoGps;

    $filename = 'img/with_gps.jpg';
    $pg = new PhotoGps($filename);

    echo "[" . $filename . "]--------------------\n";

    // Format: default
    echo "Current format [eng]: " . $pg->lang('eng')->datestampFormat() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->datestampFormat() . "\n\n";

    // Datestamp: default format
    echo "Datestamp [eng]: " . $pg->lang('eng')->datestamp() . "\n";
    echo "Datestamp [ja]: " . $pg->lang('ja')->datestamp() . "\n\n";

    // Configure Format: eng
    $pg->lang('eng')->datestampFormat('l jS \of F Y');

    // Current Format
    echo "Current format [eng]: " . $pg->lang('eng')->datestampFormat() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->datestampFormat() . "\n\n";

    // Datestamp: Current Format
    echo "Datestamp [eng]: " . $pg->lang('eng')->datestamp() . "\n";
    echo "Datestamp [ja]: " . $pg->lang('ja')->datestamp() . "\n\n";

    // Configure Format: ja
    $pg->lang('ja')->datestampFormat('n月j日(\'y)');

    // Reset Format: eng
    $pg->lang('eng')->resetDatestampFormat();

    // Current Format
    echo "Current format [eng]: " . $pg->lang('eng')->datestampFormat() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->datestampFormat() . "\n\n";

    // Datestamp: Current Format
    echo "Datestamp [eng]: " . $pg->lang('eng')->datestamp() . "\n";
    echo "Datestamp [ja]: " . $pg->lang('ja')->datestamp() . "\n\n";
    

    

    ci7\PhpPhotoGps\PhotoGps;

    $filename = 'img/with_gps.jpg';
    $pg = new PhotoGps($filename);

    echo "[" . $filename . "]--------------------\n";

    // Format: default
    echo "Current format [eng]: " . $pg->lang('eng')->timestampFormat() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->timestampFormat() . "\n\n";

    // Timestamp: default format
    echo "Timestamp [eng]: " . $pg->lang('eng')->timestamp() . "\n";
    echo "Timestamp [ja]: " . $pg->lang('ja')->timestamp() . "\n\n";

    // Configure Format: eng
    $pg->lang('eng')->timestampFormat('g:i a');

    // Current Format
    echo "Current format [eng]: " . $pg->lang('eng')->timestampFormat() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->timestampFormat() . "\n\n";

    // Timestamp: Current Format
    echo "Timestamp [eng]: " . $pg->lang('eng')->timestamp() . "\n";
    echo "Timestamp [ja]: " . $pg->lang('ja')->timestamp() . "\n\n";

    // Configure Format: ja
    $pg->lang('ja')->timestampFormat('G時i分s秒');

    // Reset Format: eng
    $pg->lang('eng')->resetTimestampFormat();

    // Current Format
    echo "Current format [eng]: " . $pg->lang('eng')->timestampFormat() . "\n";
    echo "Current format [ja]: " . $pg->lang('ja')->timestampFormat() . "\n\n";

    // Timestamp: Current Format
    echo "Timestamp [eng]: " . $pg->lang('eng')->timestamp() . "\n";
    echo "Timestamp [ja]: " . $pg->lang('ja')->timestamp() . "\n\n";
    

    

    ci7\PhpPhotoGps\PhotoGps;

    $pg = new PhotoGps();

    $images = glob('img/*.{jp*g,JP*G}', GLOB_BRACE);
    sort($images);

    echo "# Photo List: Geo Data\n\n";
    echo "<table>\n";
    echo "<tr><th>Image</th><th>Geo</th><th>Coordinate</th></tr>\n";
    foreach ($images as $file) {
        $link = sprintf("<a href='%s'><img src='%s' width=100 /></a>", $file, $file);
        $pg->load($file);
        $hasGps = $pg->hasGps();
        $hasGeo = $pg->hasGeo();
        $hasAltitude = $pg->hasAltitude();
        echo sprintf("<td>%s</td>", $link);
        echo sprintf("<td>%s</td>", $hasGps ? 'o' : 'x');
        echo "<td>\n";
        if ($hasGeo) {
            echo sprintf(
                "<a href='%s'>%s, %s</a><br />\n",
                sprintf(
                    "https://www.google.com/maps/place/%s+%s/@%.7f,%.7f,17z/?authuser=0&entry=ttu",
                    urlencode($pg->lang('eng')->latitudeS()),
                    urlencode($pg->lang('eng')->longitudeS()),
                    $pg->latitudeD(),
                    $pg->longitudeD()
                ),
                sprintf("%.14f", $pg->latitudeD()),
                sprintf("%.14f", $pg->longitudeD())
            );
            foreach ($pg->langs() as $lang) {
                echo sprintf(
                    "%s, %s<br />\n",
                    $pg->lang($lang)->latitudeS(),
                    $pg->lang($lang)->longitudeS()
                );
            }
        } else {
            echo "No Geo Data\n\n";
        }
        if ($hasAltitude) {
            foreach ($pg->langs() as $lang) {
                echo sprintf("%s\n\n", $pg->lang($lang)->altitudeS());
            }
        } else {
            echo "No Altitude Data\n\n";
        }
        echo "</td></tr>\n";
    }
    echo "</table>\n";
    

    

    ci7\PhpPhotoGps\PhotoGps;
    use Macocci7\PhpPhotoGps\Helpers\Dir;
    use Macocci7\PhpPhotoGps\Helpers\Arrow;
    use Macocci7\PhpPhotoGps\Helpers\Exif;

    $pg = new PhotoGps();
    $images = [
        'File with Fake GPS via HTTP' => 'http://macocci7.net/photo/gps/remote_fake_gps_001.jpg',
        'File with Fake GPS via HTTPS' => 'https://macocci7.net/photo/gps/remote_fake_gps_002.jpg',
        'Local File with GPS' => 'img/with_gps.jpg',
        'No GPS tags' => 'img/without_gps.jpg',
    ];
    $arrowSize = 30;

    Dir::clear('./download/');
    echo "# Exif: GPS Tags\n\n";

    // Loop for images
    foreach ($images as $title => $image) {
        echo "## $title\n\n";
        // Start Table
        echo "<table>\n";
        $style = 'display: flex; align-items: top;';
        echo sprintf("<tr style='%s'>\n<td>\n", $style);

        // Thumbnail
        echo "<img src='$image' alt='$title' width='200'>\n\n";

        // Load GPS Data
        $pg->load($image);

        // Show Attributes Converted from GPS Data
        $style = 'display: flex; justify-content: right; align-items: center;';
        $direction = $pg->direction();
        $speedS = $pg->speedS();
        $track = $pg->track();
        $destBearing = $pg->destBearing();
        $datestamp = $pg->datestamp();
        $timestamp = $pg->timestamp();

        echo "|Attribute|Value|\n";
        echo "|:---|---:|\n";
        echo sprintf("|ExifVersion|%s|\n", Exif::version());

        // Image Direction
        if (!is_null($direction)) {
            $pathArrow = sprintf('img/arrow%.2f.png', $direction);
            Arrow::make($direction)->save($pathArrow);
            echo sprintf(
                "|Image Direction|<div style='%s'><img src='%s' width=%d height=%d />%s</div>|\n",
                $style,
                $pathArrow,
                $arrowSize,
                $arrowSize,
                $pg->directionS()
            );
        }

        // Speed
        if (!is_null($speedS)) {
            echo sprintf("|Speed|%s|\n", $speedS);
        }

        // Track
        if (!is_null($track)) {
            $pathArrow = sprintf('img/arrow%.2f.png', $track);
            Arrow::make($track)->save($pathArrow);
            echo sprintf(
                "|Track|<div style='%s'><img src='%s' width=%d height=%d />%s</div>|\n",
                $style,
                $pathArrow,
                $arrowSize,
                $arrowSize,
                $pg->trackS()
            );
        }

        // Dest Bearing
        if (!is_null($destBearing)) {
            $pathArrow = sprintf('img/arrow%.2f.png', $destBearing);
            Arrow::make($destBearing)->save($pathArrow);
            echo sprintf(
                "|Destination Bearing|<div style='%s'><img src='%s' width=%d height=%d />%s</div>|\n",
                $style,
                $pathArrow,
                $arrowSize,
                $arrowSize,
                $pg->destBearingS()
            );
        }

        // Date Stamp
        echo $datestamp ? sprintf("|Datestamp|%s (UTC)|\n", $datestamp) : '';

        // Time Stamp
        echo $timestamp ? sprintf("|Timestamp|%s (UTC)|\n", $timestamp) : '';

        echo "</td>\n<td>\n\n";

        // Check If GPS Data Exists
        if ($pg->hasGps()) {
            // Show GPS Data
            echo "|Tag|Value|\n";
            echo "|:---|---:|\n";
            foreach ($pg->gps() as $tag => $value) {
                echo sprintf(
                    "|%s|%s|\n",
                    $tag,
                    is_array($value) ? implode('<br />', $value) : $value
                );
            }
        } else {
            echo "No GPS data.\n\n";
        }

        // Close Table
        echo "</td>\n</tr>\n</table>\n\n";
    }
    

    

    ci7\PhpPhotoGps\Helpers\Gps;

    //$exifVersion = "0210";
    //$exifVersion = "0220";
    //$exifVersion = "0221";
    //$exifVersion = "0230";
    //$exifVersion = "0231";
    //$exifVersion = "0232";
    $exifVersion = "0300";
    echo "# Exif" . $exifVersion . ": GPS Attribute Information\n\n";
    echo "|Field Name|Type|Count|Values|Default|Separator|\n";
    echo "|:---|:---|---:|:---|:---:|:---:|\n";
    foreach (Gps::def('exif' . $exifVersion . '.fields') as $key => $value) {
        echo sprintf(
            "|%s|%s|%d|%s|%s|%s|\n",
            $key,
            $value['type'],
            $value['count'],
            isset($value['values'])
            ? implode(
                '<br />',
                array_map(
                    fn ($k, $v) => '* ' . $k . ': ' . $v,
                    array_keys($value['values']),
                    $value['values']
                )
            )
            : '---',
            $value['default'] ?? '---',
            $value['separator'] ?? '---'
        );
    }
    

    [
        'type' => 'ASCII',
        'count' => 2,
        'default' => 'None'
        'values' => [
            'N' => 'North latitude',
            'S' => 'South latitude',
        ],
    ];
    
bash
  (php -m; php -i) | grep gd
  
bash
  (php -m; php -i) | grep exif
  
bash
composer