-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgps.php
223 lines (195 loc) · 6.88 KB
/
gps.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* PEL: PHP Exif Library.
* A library with support for reading and
* writing all Exif headers in JPEG and TIFF images using PHP.
*
* Copyright (C) 2007 Martin Geisler.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in the file COPYING; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/*
* Contributed by Andac Aydin ([email protected]).
* This example shows how one can add GPS information to a JPEG image.
* Any Exif information in the image will be overwritten by the new
* information.
* This example includes two functions:
* - convertDecimalTpDMS() converts decimal GPS coordinates (how you use them
* in Google Maps for example) to the conventional coordinate-system
* used in Exif data.
* - addGpsInfo() adds several Exif tags to your JPEG file.
*/
require_once "./pel/autoload.php";
use lsolesen\pel\PelJpeg;
use lsolesen\pel\PelTiff;
use lsolesen\pel\PelIfd;
use lsolesen\pel\PelExif;
use lsolesen\pel\PelEntryUserComment;
use lsolesen\pel\PelEntryAscii;
use lsolesen\pel\PelTag;
use lsolesen\pel\PelEntryByte;
use lsolesen\pel\PelEntryRational;
/**
* Convert a decimal degree into degrees, minutes, and seconds.
*
* @param
* int the degree in the form 123.456. Must be in the interval
* [-180, 180].
*
* @return array a triple with the degrees, minutes, and seconds. Each
* value is an array itself, suitable for passing to a
* PelEntryRational. If the degree is outside the allowed interval,
* null is returned instead.
*/
function convertDecimalToDMS($degree)
{
if ($degree > 180 || $degree < - 180) {
return null;
}
$degree = abs($degree); // make sure number is positive
// (no distinction here for N/S
// or W/E).
$seconds = $degree * 3600; // Total number of seconds.
$degrees = floor($degree); // Number of whole degrees.
$seconds -= $degrees * 3600; // Subtract the number of seconds
// taken by the degrees.
$minutes = floor($seconds / 60); // Number of whole minutes.
$seconds -= $minutes * 60; // Subtract the number of seconds
// taken by the minutes.
$seconds = round($seconds * 100, 0); // Round seconds with a 1/100th
// second precision.
return array(
array(
$degrees,
1
),
array(
$minutes,
1
),
array(
$seconds,
100
)
);
}
/**
* Add GPS information to an image basic metadata.
* Any old Exif data
* is discarded.
*
* @param
* string the input filename.
*
* @param
* string the output filename. An updated copy of the input
* image is saved here.
*
* @param
* string image description.
*
* @param
* string user comment.
*
* @param
* string camera model.
*
* @param
* float longitude expressed as a fractional number of degrees,
* e.g. 12.345�. Negative values denotes degrees west of Greenwich.
*
* @param
* float latitude expressed as for longitude. Negative values
* denote degrees south of equator.
*
* @param
* float the altitude, negative values express an altitude
* below sea level.
*
* @param
* string the date and time.
*/
function addGpsInfo($input, $output, $description, $comment, $model, $longitude, $latitude, $altitude, $date_time)
{
/* Load the given image into a PelJpeg object */
$jpeg = new PelJpeg($input);
/*
* Create and add empty Exif data to the image (this throws away any
* old Exif data in the image).
*/
$exif = new PelExif();
$jpeg->setExif($exif);
/*
* Create and add TIFF data to the Exif data (Exif data is actually
* stored in a TIFF format).
*/
$tiff = new PelTiff();
$exif->setTiff($tiff);
/*
* Create first Image File Directory and associate it with the TIFF
* data.
*/
$ifd0 = new PelIfd(PelIfd::IFD0);
$tiff->setIfd($ifd0);
/*
* Create a sub-IFD for holding GPS information. GPS data must be
* below the first IFD.
*/
$gps_ifd = new PelIfd(PelIfd::GPS);
$ifd0->addSubIfd($gps_ifd);
/*
* The USER_COMMENT tag must be put in a Exif sub-IFD under the
* first IFD.
*/
$exif_ifd = new PelIfd(PelIfd::EXIF);
$exif_ifd->addEntry(new PelEntryUserComment($comment));
$ifd0->addSubIfd($exif_ifd);
$inter_ifd = new PelIfd(PelIfd::INTEROPERABILITY);
$ifd0->addSubIfd($inter_ifd);
$ifd0->addEntry(new PelEntryAscii(PelTag::MODEL, $model));
$ifd0->addEntry(new PelEntryAscii(PelTag::DATE_TIME, $date_time));
$ifd0->addEntry(new PelEntryAscii(PelTag::IMAGE_DESCRIPTION, $description));
$gps_ifd->addEntry(new PelEntryByte(PelTag::GPS_VERSION_ID, 2, 2, 0, 0));
/*
* Use the convertDecimalToDMS function to convert the latitude from
* something like 12.34� to 12� 20' 42"
*/
list ($hours, $minutes, $seconds) = convertDecimalToDMS($latitude);
/* We interpret a negative latitude as being south. */
$latitude_ref = ($latitude < 0) ? 'S' : 'N';
$gps_ifd->addEntry(new PelEntryAscii(PelTag::GPS_LATITUDE_REF, $latitude_ref));
$gps_ifd->addEntry(new PelEntryRational(PelTag::GPS_LATITUDE, $hours, $minutes, $seconds));
/* The longitude works like the latitude. */
list ($hours, $minutes, $seconds) = convertDecimalToDMS($longitude);
$longitude_ref = ($longitude < 0) ? 'W' : 'E';
$gps_ifd->addEntry(new PelEntryAscii(PelTag::GPS_LONGITUDE_REF, $longitude_ref));
$gps_ifd->addEntry(new PelEntryRational(PelTag::GPS_LONGITUDE, $hours, $minutes, $seconds));
/*
* Add the altitude. The absolute value is stored here, the sign is
* stored in the GPS_ALTITUDE_REF tag below.
*/
$gps_ifd->addEntry(new PelEntryRational(PelTag::GPS_ALTITUDE, array(
abs($altitude),
1
)));
/*
* The reference is set to 1 (true) if the altitude is below sea
* level, or 0 (false) otherwise.
*/
$gps_ifd->addEntry(new PelEntryByte(PelTag::GPS_ALTITUDE_REF, (int) ($altitude < 0)));
/* Finally we store the data in the output file. */
file_put_contents($output, $jpeg->getBytes());
}