summaryrefslogtreecommitdiff
path: root/includes/jpeg_metadata_tk/EXIF.php
blob: 8751dcda051985349eb96392af6920e0769d2525 (plain)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
<?

/******************************************************************************
*
* Filename:     EXIF.php
*
* Description:  Provides functions for reading and writing EXIF Information
*               to/from an APP1 segment of a JPEG file
*               Unfortunately, because EXIF data may be distributed anywhere
*               throughout an image file, rather than just being in one block,
*               it is impossible to pass just a string containing only the EXIF
*               information. Hence it is neccessary to be able to seek to
*               any point in the file. This causes the HTTP and FTP wrappers
*               not to work - i.e. the EXIF functions will only work with local
*               files.
*               To work on an internet file, copy it locally to start with:
*
*               $newfilename = tempnam ( $dir, "tmpexif" );
*               copy ( "http://whatever.com", $newfilename );
*
*
* Author:       Evan Hunter
*
* Date:         30/7/2004
*
* Project:      PHP JPEG Metadata Toolkit
*
* Revision:     1.11
*
* Changes:      1.00 -> 1.10 : added function get_EXIF_TIFF to allow extracting EXIF from a TIFF file
*               1.10 -> 1.11 : added functionality to allow decoding of XMP and Photoshop IRB information
*                              embedded within the EXIF data
*                              added checks for http and ftp wrappers, as these are not supported
*                              changed interpret_IFD to allow thumbnail links to work when
*                              toolkit is portable across directories
*
*
* URL:          http://electronics.ozhiker.com
*
* Copyright:    Copyright Evan Hunter 2004
*
* License:      This file is part of the PHP JPEG Metadata Toolkit.
*
*               The PHP JPEG Metadata Toolkit 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.
*
*               The PHP JPEG Metadata Toolkit 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 the PHP JPEG Metadata Toolkit; if not,
*               write to the Free Software Foundation, Inc., 59 Temple
*               Place, Suite 330, Boston, MA  02111-1307  USA
*
*               If you require a different license for commercial or other
*               purposes, please contact the author: evan@ozhiker.com
*
******************************************************************************/


// TODO : Thoroughly test the functions for writing EXIF segments
// TODO : Figure out a way to allow EXIF to function normally with HTTP and FTP wrappers
// TODO : Implement EXIF decoding of Device Setting Description field
// TODO : Implement EXIF decoding of SpatialFrequencyResponse field
// TODO : Implement EXIF decoding of OECF field
// TODO : Implement EXIF decoding of SubjectArea field
// TODO : Add a put_EXIF_TIFF function

/******************************************************************************
*
* Initialisation
*
******************************************************************************/


if ( !isset( $GLOBALS['HIDE_UNKNOWN_TAGS'] ) )     $GLOBALS['HIDE_UNKNOWN_TAGS']= FALSE;
if ( !isset( $GLOBALS['SHOW_BINARY_DATA_HEX'] ) )  $GLOBALS['SHOW_BINARY_DATA_HEX'] = FALSE;
if ( !isset( $GLOBALS['SHOW_BINARY_DATA_TEXT'] ) ) $GLOBALS['SHOW_BINARY_DATA_TEXT'] = FALSE;


include_once 'EXIF_Tags.php';
include_once 'EXIF_Makernote.php';
include_once 'PIM.php';
include_once 'Unicode.php';
include_once 'JPEG.php';
include_once 'IPTC.php';
include_once 'Photoshop_IRB.php';       // Change: as of version 1.11  - Required for TIFF with embedded IRB
include_once 'XMP.php';                 // Change: as of version 1.11  - Required for TIFF with embedded XMP
include_once 'pjmt_utils.php';          // Change: as of version 1.11  - Required for directory portability








/******************************************************************************
*
* Function:     get_EXIF_JPEG
*
* Description:  Retrieves information from a Exchangeable Image File Format (EXIF)
*               APP1 segment and returns it in an array.
*
* Parameters:   filename - the filename of the JPEG image to process
*
* Returns:      OutputArray - Array of EXIF records
*               FALSE - If an error occured in decoding
*
******************************************************************************/

function get_EXIF_JPEG( $filename )
{
        // Change: Added as of version 1.11
        // Check if a wrapper is being used - these are not currently supported (see notes at top of file)
        if ( ( stristr ( $filename, "http://" ) != FALSE ) || ( stristr ( $filename, "ftp://" ) != FALSE ) )
        {
                // A HTTP or FTP wrapper is being used - show a warning and abort
                error_log( "HTTP and FTP wrappers are currently not supported with EXIF - See EXIF functionality documentation - a local file must be specified<br>
	                To work on an internet file, copy it locally to start with:<br><br>\n
    	            $newfilename = tempnam ( \$dir, \"tmpexif\" );<br>\n
        	        copy ( \"http://whatever.com\", \$newfilename );<br><br>\n" );
                return FALSE;
        }

        // get the JPEG headers
        $jpeg_header_data = get_jpeg_header_data( $filename );


        // Flag that an EXIF segment has not been found yet
        $EXIF_Location = -1;

        //Cycle through the header segments
        for( $i = 0; $i < count( $jpeg_header_data ); $i++ )
        {
                // If we find an APP1 header,
                if ( strcmp ( $jpeg_header_data[$i]['SegName'], "APP1" ) == 0 )
                {
                        // And if it has the EXIF label,
                        if ( ( strncmp ( $jpeg_header_data[$i]['SegData'], "Exif\x00\x00", 6) == 0 ) ||
                             ( strncmp ( $jpeg_header_data[$i]['SegData'], "Exif\x00\xFF", 6) == 0 ) )          // For some reason, some files have a faulty EXIF name which has a 0xFF in it
                        {
                                // Save the location of the EXIF segment
                                $EXIF_Location = $i;
                        }
                }

        }

        // Check if an EXIF segment was found
        if ( $EXIF_Location == -1 )
        {
                // Couldn't find any EXIF block to decode
                return FALSE;
        }

        $filehnd = @fopen($filename, 'rb');

        // Check if the file opened successfully
        if ( ! $filehnd  )
        {
                // Could't open the file - exit
                error_log( "Could not open file $filename" );
                return FALSE;
        }

        fseek( $filehnd, $jpeg_header_data[$EXIF_Location]['SegDataStart'] + 6  );

        // Decode the Exif segment into an array and return it
        $exif_data = process_TIFF_Header( $filehnd, "TIFF" );



        // Close File
        fclose($filehnd);
        return $exif_data;
}

/******************************************************************************
* End of Function:     get_EXIF_JPEG
******************************************************************************/



/******************************************************************************
*
* Function:     put_EXIF_JPEG
*
* Description:  Stores information into a Exchangeable Image File Format (EXIF)
*               APP1 segment from an EXIF array.
*
*               WARNING: Because the EXIF standard allows pointers to data
*               outside the APP1 segment, if there are any such pointers in
*               a makernote, this function will DAMAGE them since it will not
*               be aware that there is an external pointer. This will often
*               happen with Makernotes that include an embedded thumbnail.
*               This damage could be prevented where makernotes can be decoded,
*               but currently this is not implemented.
*
*
* Parameters:   exif_data - The array of EXIF data to insert into the JPEG header
*               jpeg_header_data - The JPEG header into which the EXIF data
*                                  should be stored, as from get_jpeg_header_data
*
* Returns:      jpeg_header_data - JPEG header array with the EXIF segment inserted
*               FALSE - If an error occured
*
******************************************************************************/

function put_EXIF_JPEG( $exif_data, $jpeg_header_data )
{
        // pack the EXIF data into its proper format for a JPEG file
        $packed_data = get_TIFF_Packed_Data( $exif_data );
        if ( $packed_data === FALSE )
        {
                return $jpeg_header_data;
        }

        $packed_data = "Exif\x00\x00$packed_data";

        //Cycle through the header segments
        for( $i = 0; $i < count( $jpeg_header_data ); $i++ )
        {
                // If we find an APP1 header,
                if ( strcmp ( $jpeg_header_data[$i]['SegName'], "APP1" ) == 0 )
                {
                        // And if it has the EXIF label,
                        if ( ( strncmp ( $jpeg_header_data[$i]['SegData'], "Exif\x00\x00", 6) == 0 ) ||
                             ( strncmp ( $jpeg_header_data[$i]['SegData'], "Exif\x00\xFF", 6) == 0 ) )          // For some reason, some files have a faulty EXIF name which has a 0xFF in it
                        {
                                // Found a preexisting EXIF block - Replace it with the new one and return.
                                $jpeg_header_data[$i]['SegData'] = $packed_data;
                                return $jpeg_header_data;
                        }
                }
        }

        // No preexisting segment segment found, insert a new one at the start of the header data.

        // Determine highest position of an APP segment at or below APP3, so we can put the
        // new APP3 at this position


        $highest_APP = -1;

        //Cycle through the header segments
        for( $i = 0; $i < count( $jpeg_header_data ); $i++ )
        {
                // Check if we have found an APP segment at or below APP3,
                if ( ( $jpeg_header_data[$i]['SegType'] >= 0xE0 ) && ( $jpeg_header_data[$i]['SegType'] <= 0xE3 ) )
                {
                        // Found an APP segment at or below APP12
                        $highest_APP = $i;
                }
        }

        // No preexisting EXIF block found, insert a new one at the start of the header data.
        array_splice($jpeg_header_data, $highest_APP + 1 , 0, array( array(   "SegType" => 0xE1,
                                                                              "SegName" => "APP1",
                                                                              "SegDesc" => $GLOBALS[ "JPEG_Segment_Descriptions" ][ 0xE1 ],
                                                                              "SegData" => $packed_data ) ) );
        return $jpeg_header_data;

}

/******************************************************************************
* End of Function:     put_EXIF_JPEG
******************************************************************************/




/******************************************************************************
*
* Function:     get_Meta_JPEG
*
* Description:  Retrieves information from a Meta APP3 segment and returns it
*               in an array. Uses information supplied by the
*               get_jpeg_header_data function.
*               The Meta segment has the same format as an EXIF segment, but
*               uses different tags
*
* Parameters:   filename - the filename of the JPEG image to process
*
* Returns:      OutputArray - Array of Meta records
*               FALSE - If an error occured in decoding
*
******************************************************************************/

function get_Meta_JPEG( $filename )
{
        // Change: Added as of version 1.11
        // Check if a wrapper is being used - these are not currently supported (see notes at top of file)
        if ( ( stristr ( $filename, "http://" ) != FALSE ) || ( stristr ( $filename, "ftp://" ) != FALSE ) )
        {
                // A HTTP or FTP wrapper is being used - show a warning and abort
                echo "HTTP and FTP wrappers are currently not supported with Meta - See EXIF/Meta functionality documentation - a local file must be specified<br>";
                echo "To work on an internet file, copy it locally to start with:<br><br>\n";
                echo "\$newfilename = tempnam ( \$dir, \"tmpmeta\" );<br>\n";
                echo "copy ( \"http://whatever.com\", \$newfilename );<br><br>\n";
                return FALSE;
        }

        // get the JPEG headers
        $jpeg_header_data = get_jpeg_header_data( $filename );


        // Flag that an Meta segment has not been found yet
        $Meta_Location = -1;

        //Cycle through the header segments
        for( $i = 0; $i < count( $jpeg_header_data ); $i++ )
        {
                // If we find an APP3 header,
                if  ( strcmp ( $jpeg_header_data[$i]['SegName'], "APP3" ) == 0 )
                {
                        // And if it has the Meta label,
                        if ( ( strncmp ( $jpeg_header_data[$i]['SegData'], "Meta\x00\x00", 6) == 0 ) ||
                             ( strncmp ( $jpeg_header_data[$i]['SegData'], "META\x00\x00", 6) == 0 ) )
                        {
                                // Save the location of the Meta segment
                                $Meta_Location = $i;
                        }
                }
        }

        // Check if an EXIF segment was found
        if ( $Meta_Location == -1 )
        {
                // Couldn't find any Meta block to decode
                return FALSE;
        }


        $filehnd = @fopen($filename, 'rb');

        // Check if the file opened successfully
        if ( ! $filehnd  )
        {
                // Could't open the file - exit
                error_log( "Could not open file $filename" );
                return FALSE;
        }

        fseek( $filehnd, $jpeg_header_data[$Meta_Location]['SegDataStart'] + 6 );

        // Decode the Meta segment into an array and return it
        $meta = process_TIFF_Header( $filehnd, "Meta" );

         // Close File
        fclose($filehnd);

        return $meta;
}

/******************************************************************************
* End of Function:     get_Meta
******************************************************************************/







/******************************************************************************
*
* Function:     put_Meta_JPEG
*
* Description:  Stores information into a Meta APP3 segment from a Meta array.
*
*
*               WARNING: Because the Meta (EXIF) standard allows pointers to data
*               outside the APP1 segment, if there are any such pointers in
*               a makernote, this function will DAMAGE them since it will not
*               be aware that there is an external pointer. This will often
*               happen with Makernotes that include an embedded thumbnail.
*               This damage could be prevented where makernotes can be decoded,
*               but currently this is not implemented.
*
*
* Parameters:   meta_data - The array of Meta data to insert into the JPEG header
*               jpeg_header_data - The JPEG header into which the Meta data
*                                  should be stored, as from get_jpeg_header_data
*
* Returns:      jpeg_header_data - JPEG header array with the Meta segment inserted
*               FALSE - If an error occured
*
******************************************************************************/

function put_Meta_JPEG( $meta_data, $jpeg_header_data )
{
        // pack the Meta data into its proper format for a JPEG file
        $packed_data = get_TIFF_Packed_Data( $meta_data );
        if ( $packed_data === FALSE )
        {
                return $jpeg_header_data;
        }

        $packed_data = "Meta\x00\x00$packed_data";

        //Cycle through the header segments
        for( $i = 0; $i < count( $jpeg_header_data ); $i++ )
        {
                // If we find an APP1 header,
                if ( strcmp ( $jpeg_header_data[$i]['SegName'], "APP3" ) == 0 )
                {
                        // And if it has the Meta label,
                        if ( ( strncmp ( $jpeg_header_data[$i]['SegData'], "Meta\x00\x00", 6) == 0 ) ||
                             ( strncmp ( $jpeg_header_data[$i]['SegData'], "META\x00\x00", 6) == 0 ) )
                        {
                                // Found a preexisting Meta block - Replace it with the new one and return.
                                $jpeg_header_data[$i]['SegData'] = $packed_data;
                                return $jpeg_header_data;
                        }
                }
        }
        // No preexisting segment segment found, insert a new one at the start of the header data.

        // Determine highest position of an APP segment at or below APP3, so we can put the
        // new APP3 at this position


        $highest_APP = -1;

        //Cycle through the header segments
        for( $i = 0; $i < count( $jpeg_header_data ); $i++ )
        {
                // Check if we have found an APP segment at or below APP3,
                if ( ( $jpeg_header_data[$i]['SegType'] >= 0xE0 ) && ( $jpeg_header_data[$i]['SegType'] <= 0xE3 ) )
                {
                        // Found an APP segment at or below APP12
                        $highest_APP = $i;
                }
        }

        // No preexisting Meta block found, insert a new one at the start of the header data.
        array_splice($jpeg_header_data, $highest_APP + 1 , 0, array( array(     "SegType" => 0xE3,
                                                                                "SegName" => "APP3",
                                                                                "SegDesc" => $GLOBALS[ "JPEG_Segment_Descriptions" ][ 0xE1 ],
                                                                                "SegData" => $packed_data ) ) );
        return $jpeg_header_data;

}

/******************************************************************************
* End of Function:     put_Meta_JPEG
******************************************************************************/



/******************************************************************************
*
* Function:     get_EXIF_TIFF
*
* Description:  Retrieves information from a Exchangeable Image File Format (EXIF)
*               within a TIFF file and returns it in an array.
*
* Parameters:   filename - the filename of the TIFF image to process
*
* Returns:      OutputArray - Array of EXIF records
*               FALSE - If an error occured in decoding
*
******************************************************************************/

function get_EXIF_TIFF( $filename )
{
        // Change: Added as of version 1.11
        // Check if a wrapper is being used - these are not currently supported (see notes at top of file)
        if ( ( stristr ( $filename, "http://" ) != FALSE ) || ( stristr ( $filename, "ftp://" ) != FALSE ) )
        {
                // A HTTP or FTP wrapper is being used - show a warning and abort
                echo "HTTP and FTP wrappers are currently not supported with TIFF - See EXIF/TIFF functionality documentation - a local file must be specified<br>";
                echo "To work on an internet file, copy it locally to start with:<br><br>\n";
                echo "\$newfilename = tempnam ( \$dir, \"tmptiff\" );<br>\n";
                echo "copy ( \"http://whatever.com\", \$newfilename );<br><br>\n";
                return FALSE;
        }


        $filehnd = @fopen($filename, 'rb');

        // Check if the file opened successfully
        if ( ! $filehnd  )
        {
                // Could't open the file - exit
                error_log( "Could not open file $filename" );
                return FALSE;
        }

        // Decode the Exif segment into an array and return it
        $exif_data = process_TIFF_Header( $filehnd, "TIFF" );

        // Close File
        fclose($filehnd);
        return $exif_data;
}

/******************************************************************************
* End of Function:     get_EXIF_TIFF
******************************************************************************/




/******************************************************************************
*
* Function:     Interpret_EXIF_to_HTML
*
* Description:  Generates html detailing the contents an APP1 EXIF array
*               which was retrieved with a get_EXIF_.... function.
*               Can also be used for APP3 Meta arrays.
*
* Parameters:   Exif_array - the EXIF array,as read from get_EXIF_....
*               filename - the name of the Image file being processed ( used
*                          by scripts which displays EXIF thumbnails)
*
* Returns:      output_str - A string containing the HTML
*
******************************************************************************/

function Interpret_EXIF_to_HTML( $Exif_array, $filename )
{
        // Create the string to receive the html output
        $output_str = "";

        // Check if the array to process is valid
        if ( $Exif_array === FALSE )
        {
                // Exif Array is not valid - abort processing
                return $output_str;
        }

        // Ouput the heading according to what type of tags were used in processing
        if ( $Exif_array[ 'Tags Name' ] == "TIFF" )
        {
                $output_str .= "<h2 class=\"EXIF_Main_Heading\">Contains Exchangeable Image File Format (EXIF) Information</h2>\n";
        }
        else if ( $Exif_array[ 'Tags Name' ] == "Meta" )
        {
                $output_str .= "<h2 class=\"EXIF_Main_Heading\">Contains META Information (APP3)</h2>\n";
        }
        else
        {
                $output_str .= "<h2 class=\"EXIF_Main_Heading\">Contains " . $Exif_array[ 'Tags Name' ] . " Information</h2>\n";
        }


        // Check that there are actually items to process in the array
        if ( count( $Exif_array ) < 1 )
        {
                // No items to process in array - abort processing
                return $output_str;
        }

        // Output secondary heading
        $output_str .= "<h3 class=\"EXIF_Secondary_Heading\">Main Image Information</h2>\n";

        // Interpret the zeroth IFD to html
        $output_str .= interpret_IFD( $Exif_array[0], $filename, $Exif_array['Byte_Align'] );

        // Check if there is a first IFD to process
        if ( array_key_exists( 1, $Exif_array ) )
        {
                // There is a first IFD for a thumbnail
                // Add a heading for it to the output
                $output_str .= "<h3 class=\"EXIF_Secondary_Heading\">Thumbnail Information</h2>\n";

                // Interpret the IFD to html and add it to the output
                $output_str .= interpret_IFD( $Exif_array[1], $filename, $Exif_array['Byte_Align'] );
        }

        // Cycle through any other IFD's
        $i = 2;
        while ( array_key_exists( $i, $Exif_array ) )
        {
                // Add a heading for the IFD
                $output_str .= "<h3  class=\"EXIF_Secondary_Heading\">Image File Directory (IFD) $i Information</h2>\n";

                // Interpret the IFD to html and add it to the output
                $output_str .= interpret_IFD( $Exif_array[$i], $filename, $Exif_array['Byte_Align'] );
                $i++;
        }

        // Return the resulting HTML
        return $output_str;
}

/******************************************************************************
* End of Function:     Interpret_EXIF_to_HTML
******************************************************************************/
















/******************************************************************************
*
*         INTERNAL FUNCTIONS
*
******************************************************************************/











/******************************************************************************
*
* Internal Function:     get_TIFF_Packed_Data
*
* Description:  Packs TIFF IFD data from EXIF or Meta into a form ready for
*               either a JPEG EXIF/Meta segment or a TIFF file
*               This function attempts to protect the contents of an EXIF makernote,
*               by ensuring that it remains in the same position relative to the
*               TIFF header
*
* Parameters:   tiff_data - the EXIF array,as read from get_EXIF_JPEG or get_Meta_JPEG
*
* Returns:      packed_data - A string containing packed segment
*
******************************************************************************/

function get_TIFF_Packed_Data( $tiff_data )
{
        // Check that the segment is valid
        if ( $tiff_data === FALSE )
        {
                return FALSE;
        }

        // Get the byte alignment
        $Byte_Align = $tiff_data['Byte_Align'];

        // Add the Byte Alignment to the Packed data
        $packed_data = $Byte_Align;

        // Add the TIFF ID to the Packed Data
        $packed_data .= put_IFD_Data_Type( 42, 3, $Byte_Align );

        // Create a string for the makernote
        $makernote = "";

        // Check if the makernote exists
        if ( $tiff_data[ 'Makernote_Tag' ] !== FALSE )
        {
                // A makernote exists - We need to ensure that it stays in the same position as it was
                // Put the Makernote before any of the IFD's by padding zeros to the correct offset
                $makernote .= str_repeat("\x00",( $tiff_data[ 'Makernote_Tag' ][ 'Offset' ] - 8 ) );
                $makernote .= $tiff_data[ 'Makernote_Tag' ]['Data'];
        }

        // Calculage where the zeroth ifd will be
        $ifd_offset = strlen( $makernote ) + 8;

        // Add the Zeroth IFD pointer to the packed data
        $packed_data .= put_IFD_Data_Type( $ifd_offset, 4, $Byte_Align );

        // Add the makernote to the packed data (if there was one)
        $packed_data .= $makernote;

        //Add the IFD's to the packed data
        $packed_data .= get_IFD_Array_Packed_Data( $tiff_data, $ifd_offset, $Byte_Align );

        // Return the result
        return $packed_data;
}

/******************************************************************************
* End of Function:     get_TIFF_Packed_Data
******************************************************************************/




/******************************************************************************
*
* Internal Function:     get_IFD_Array_Packed_Data
*
* Description:  Packs a chain of IFD's from EXIF or Meta segments into a form
*               ready for either a JPEG EXIF/Meta segment or a TIFF file
*
* Parameters:   ifd_data - the IFD chain array, as read from get_EXIF_JPEG or get_Meta_JPEG
*               Zero_IFD_offset - The offset to the first IFD from the start of the TIFF header
*               Byte_Align - the Byte alignment to use - "MM" or "II"
*
* Returns:      packed_data - A string containing packed IFD's
*
******************************************************************************/

function get_IFD_Array_Packed_Data( $ifd_data, $Zero_IFD_offset, $Byte_Align )
{
        // Create a string to receive the packed output
        $packed_data = "";

        // Count the IFDs
        $ifd_count = 0;
        foreach( $ifd_data as $key => $IFD )
        {
                // Make sure we only count the IFD's, not other information keys
                if ( is_numeric( $key ) )
                {
                        $ifd_count++;
                }
        }


        // Cycle through each IFD,
        for ( $ifdno = 0; $ifdno < $ifd_count; $ifdno++ )
        {
                // Check if this IFD is the last one
                if ( $ifdno == $ifd_count - 1 )
                {
                        // This IFD is the last one, get it's packed data
                        $packed_data .= get_IFD_Packed_Data( $ifd_data[ $ifdno ], $Zero_IFD_offset +strlen($packed_data), $Byte_Align, FALSE );
                }
                else
                {
                        // This IFD is NOT the last one, get it's packed data
                        $packed_data .= get_IFD_Packed_Data( $ifd_data[ $ifdno ], $Zero_IFD_offset +strlen($packed_data), $Byte_Align, TRUE );
                }

        }

        // Return the packed output
        return $packed_data;
}

/******************************************************************************
* End of Function:     get_IFD_Array_Packed_Data
******************************************************************************/



/******************************************************************************
*
* Internal Function:     get_IFD_Packed_Data
*
* Description:  Packs an IFD from EXIF or Meta segments into a form
*               ready for either a JPEG EXIF/Meta segment or a TIFF file
*
* Parameters:   ifd_data - the IFD chain array, as read from get_EXIF_JPEG or get_Meta_JPEG
*               IFD_offset - The offset to the IFD from the start of the TIFF header
*               Byte_Align - the Byte alignment to use - "MM" or "II"
*               Another_IFD - boolean - false if this is the last IFD in the chain
*                                     - true if it is not the last
*
* Returns:      packed_data - A string containing packed IFD's
*
******************************************************************************/

function get_IFD_Packed_Data( $ifd_data, $IFD_offset, $Byte_Align, $Another_IFD )
{

        $ifd_body_str = "";
        $ifd_data_str = "";

        $Tag_Definitions_Name = $ifd_data[ 'Tags Name' ];


        // Count the Tags in this IFD
        $tag_count = 0;
        foreach( $ifd_data as $key => $tag )
        {
                // Make sure we only count the Tags, not other information keys
                if ( is_numeric( $key ) )
                {
                        $tag_count++;
                }
        }

        // Add the Tag count to the packed data
        $packed_data = put_IFD_Data_Type( $tag_count, 3, $Byte_Align );

        // Calculate the total length of the IFD (without the offset data)
        $IFD_len = 2 + $tag_count * 12 + 4;


        // Cycle through each tag
        foreach( $ifd_data as $key => $tag )
        {
                // Make sure this is a tag, not another information key
                if ( is_numeric( $key ) )
                {

                        // Add the tag number to the packed data
                        $ifd_body_str .= put_IFD_Data_Type( $tag[ 'Tag Number' ], 3, $Byte_Align );

                        // Add the Data type to the packed data
                        $ifd_body_str .= put_IFD_Data_Type( $tag['Data Type'], 3, $Byte_Align );

                        // Check if this is a Print Image Matching entry
                        if ( $tag['Type'] == "PIM" )
                        {
                                // This is a Print Image Matching entry,
                                // encode it
                                $data = Encode_PIM( $tag, $Byte_Align );
                        }
                                // Check if this is a IPTC/NAA Record within the EXIF IFD
                        else if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) &&
                                  ( $tag[ 'Tag Number' ] == 33723 ) )
                        {
                                // This is a IPTC/NAA Record, encode it
                                $data = put_IPTC( $tag['Data'] );
                        }
                                // Change: Check for embedded XMP as of version 1.11
                                // Check if this is a XMP Record within the EXIF IFD
                        else if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) &&
                                  ( $tag[ 'Tag Number' ] == 700 ) )
                        {
                                // This is a XMP Record, encode it
                                $data = write_XMP_array_to_text( $tag['Data'] );
                        }
                                // Change: Check for embedded IRB as of version 1.11
                                // Check if this is a Photoshop IRB Record within the EXIF IFD
                        else if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) &&
                                  ( $tag[ 'Tag Number' ] == 34377 ) )
                        {
                                // This is a Photoshop IRB Record, encode it
                                $data = pack_Photoshop_IRB_Data( $tag['Data'] );
                        }
                                // Exif Thumbnail Offset
                        else if ( ( $tag[ 'Tag Number' ] == 513 ) && ( $Tag_Definitions_Name == "TIFF" ) )
                        {
                                        // The Exif Thumbnail Offset is a pointer but of type Long, not Unknown
                                        // Hence we need to put the data into the packed string separately
                                        // Calculate the thumbnail offset
                                        $data_offset = $IFD_offset + $IFD_len + strlen($ifd_data_str);

                                        // Create the Offset for the IFD
                                        $data = put_IFD_Data_Type( $data_offset, 4, $Byte_Align );

                                        // Store the thumbnail
                                        $ifd_data_str .= $tag['Data'];
                        }
                                // Exif Thumbnail Length
                        else if ( ( $tag[ 'Tag Number' ] == 514 ) && ( $Tag_Definitions_Name == "TIFF" ) )
                        {
                                        // Encode the Thumbnail Length
                                        $data = put_IFD_Data_Type( strlen($ifd_data[513]['Data']), 4, $Byte_Align );
                        }
                                // Sub-IFD
                        else if ( $tag['Type'] == "SubIFD" )
                        {
                                        // This is a Sub-IFD
                                        // Calculate the offset to the start of the Sub-IFD
                                        $data_offset = $IFD_offset + $IFD_len + strlen($ifd_data_str);
                                        // Get the packed data for the IFD chain as the data for this tag
                                        $data = get_IFD_Array_Packed_Data( $tag['Data'], $data_offset, $Byte_Align );
                        }
                        else
                        {
                                // Not a special tag

                                // Create a string to receive the data
                                $data = "";

                                // Check if this is a type Unknown tag
                                if ( $tag['Data Type'] != 7 )
                                {
                                        // NOT type Unknown
                                        // Cycle through each data value and add it to the data string
                                        foreach( $tag[ 'Data' ] as $data_val )
                                        {
                                                $data .= put_IFD_Data_Type( $data_val, $tag['Data Type'], $Byte_Align );
                                        }
                                }
                                else
                                {
                                        // This is a type Unknown - just add the data as is to the data string
                                        $data .= $tag[ 'Data' ];
                                }
                        }

                        // Pad the data string out to at least 4 bytes
                        $data = str_pad ( $data, 4, "\x00" );


                        // Check if the data type is an ASCII String or type Unknown
                        if ( ( $tag['Data Type'] == 2 ) || ( $tag['Data Type'] == 7 ) )
                        {
                                // This is an ASCII String or type Unknown
                                // Add the Length of the string to the packed data as the Count
                                $ifd_body_str .= put_IFD_Data_Type( strlen($data), 4, $Byte_Align );
                        }
                        else
                        {
                                // Add the array count to the packed data as the Count
                                $ifd_body_str .= put_IFD_Data_Type( count($tag[ 'Data' ]), 4, $Byte_Align );
                        }


                        // Check if the data is over 4 bytes long
                        if ( strlen( $data ) > 4 )
                        {
                                // Data is longer than 4 bytes - it needs to be offset
                                // Check if this entry is the Maker Note
                                if ( ( $Tag_Definitions_Name == "EXIF" ) && ( $tag[ 'Tag Number' ] == 37500 ) )
                                {
                                        // This is the makernote - It will have already been stored
                                        // at its original offset to help preserve it
                                        // all we need to do is add the Offset to the IFD packed data
                                        $data_offset = $tag[ 'Offset' ];

                                        $ifd_body_str .= put_IFD_Data_Type( $data_offset, 4, $Byte_Align );
                                }
                                else
                                {
                                        // This is NOT the makernote
                                        // Calculate the data offset
                                        $data_offset = $IFD_offset + $IFD_len + strlen($ifd_data_str);

                                        // Add the offset to the IFD packed data
                                        $ifd_body_str .= put_IFD_Data_Type( $data_offset, 4, $Byte_Align );

                                        // Add the data to the offset packed data
                                        $ifd_data_str .= $data;
                                }
                        }
                        else
                        {
                                // Data is less than or equal to 4 bytes - Add it to the packed IFD data as is
                                $ifd_body_str .= $data;
                        }

                }
        }

        // Assemble the IFD body onto the packed data
        $packed_data .= $ifd_body_str;

        // Check if there is another IFD after this one
        if( $Another_IFD === TRUE )
        {
                // There is another IFD after this
                // Calculate the Next-IFD offset so that it goes immediately after this IFD
                $next_ifd_offset = $IFD_offset + $IFD_len + strlen($ifd_data_str);
        }
        else
        {
                // There is NO IFD after this - indicate with offset=0
                $next_ifd_offset = 0;
        }

        // Add the Next-IFD offset to the packed data
        $packed_data .= put_IFD_Data_Type( $next_ifd_offset, 4, $Byte_Align );

        // Add the offset data to the packed data
        $packed_data .= $ifd_data_str;

        // Return the resulting packed data
        return $packed_data;
}

/******************************************************************************
* End of Function:     get_IFD_Packed_Data
******************************************************************************/





/******************************************************************************
*
* Internal Function:     process_TIFF_Header
*
* Description:  Decodes the information stored in a TIFF header and it's
*               Image File Directories (IFD's). This information is returned
*               in an array
*
* Parameters:   filehnd - The handle of a open image file, positioned at the
*                          start of the TIFF header
*               Tag_Definitions_Name - The name of the Tag Definitions group
*                                      within the global array IFD_Tag_Definitions
*
*
* Returns:      OutputArray - Array of IFD records
*               FALSE - If an error occured in decoding
*
******************************************************************************/

function process_TIFF_Header( $filehnd, $Tag_Definitions_Name )
{


        // Save the file position where the TIFF header starts, as offsets are relative to this position
        $Tiff_start_pos = ftell( $filehnd );



        // Read the eight bytes of the TIFF header
        $DataStr = network_safe_fread( $filehnd, 8 );

        // Check that we did get all eight bytes
        if ( strlen( $DataStr ) != 8 )
        {
                return FALSE;   // Couldn't read the TIFF header properly
        }

        $pos = 0;
        // First two bytes indicate the byte alignment - should be 'II' or 'MM'
        // II = Intel (LSB first, MSB last - Little Endian)
        // MM = Motorola (MSB first, LSB last - Big Endian)
        $Byte_Align = substr( $DataStr, $pos, 2 );



        // Check the Byte Align Characters for validity
        if ( ( $Byte_Align != "II" ) && ( $Byte_Align != "MM" ) )
        {
                // Byte align field is invalid - we won't be able to decode file
                return FALSE;
        }

        // Skip over the Byte Align field which was just read
        $pos += 2;

        // Next two bytes are TIFF ID - should be value 42 with the appropriate byte alignment
        $TIFF_ID = substr( $DataStr, $pos, 2 );

        if ( get_IFD_Data_Type( $TIFF_ID, 3, $Byte_Align ) != 42 )
        {
                // TIFF header ID not found
                return FALSE;
        }

        // Skip over the TIFF ID field which was just read
        $pos += 2;


        // Next four bytes are the offset to the first IFD
        $offset_str = substr( $DataStr, $pos, 4 );
        $offset = get_IFD_Data_Type( $offset_str, 4, $Byte_Align );

        // Done reading TIFF Header


        // Move to first IFD

        if ( fseek( $filehnd, $Tiff_start_pos + $offset ) !== 0 )
        {
                // Error seeking to position of first IFD
                return FALSE;
        }



        // Flag that a makernote has not been found yet
        $GLOBALS[ "Maker_Note_Tag" ] = FALSE;

        // Read the IFD chain into an array
        $Output_Array = read_Multiple_IFDs( $filehnd, $Tiff_start_pos, $Byte_Align, $Tag_Definitions_Name );

        // Check if a makernote was found
        if ( $GLOBALS[ "Maker_Note_Tag" ] != FALSE )
        {
                // Makernote was found - Process it
                // The makernote needs to be processed after all other
                // tags as it may require some of the other tags in order
                // to be processed properly
                $GLOBALS[ "Maker_Note_Tag" ] = Read_Makernote_Tag( $GLOBALS[ "Maker_Note_Tag" ], $Output_Array, $filehnd );

        }

        $Output_Array[ 'Makernote_Tag' ] = $GLOBALS[ "Maker_Note_Tag" ];

        // Save the Name of the Tags used in the output array
        $Output_Array[ 'Tags Name' ] = $Tag_Definitions_Name;



        // Save the Byte alignment
        $Output_Array['Byte_Align'] = $Byte_Align;


        // Return the output array
        return $Output_Array ;
}

/******************************************************************************
* End of Function:     process_TIFF_Header
******************************************************************************/






/******************************************************************************
*
* Internal Function:     read_Multiple_IFDs
*
* Description:  Reads and interprets a chain of standard Image File Directories (IFD's),
*               and returns the entries in an array. This chain is made up from IFD's
*               which have a pointer to the next IFD. IFD's are read until the next
*               pointer indicates there are no more
*
* Parameters:   filehnd - a handle for the image file being read, positioned at the
*                         start of the IFD chain
*               Tiff_offset - The offset of the TIFF header from the start of the file
*               Byte_Align - either "MM" or "II" indicating Motorola or Intel Byte alignment
*               Tag_Definitions_Name - The name of the Tag Definitions group within the global array IFD_Tag_Definitions
*               local_offsets - True indicates that offset data should be interpreted as being relative to the start of the currrent entry
*                               False (normal) indicates offests are relative to start of Tiff header as per IFD standard
*               read_next_ptr - True (normal) indicates that a pointer to the next IFD should be read at the end of the IFD
*                               False indicates that no pointer follows the IFD
*
*
* Returns:      OutputArray - Array of IFD entries
*
******************************************************************************/

function read_Multiple_IFDs( $filehnd, $Tiff_offset, $Byte_Align, $Tag_Definitions_Name, $local_offsets = FALSE, $read_next_ptr = TRUE )
{
        // Start at the offset of the first IFD
        $Next_Offset = 0;

        do
        {
                // Read an IFD
                list($IFD_Array , $Next_Offset) = read_IFD_universal( $filehnd, $Tiff_offset, $Byte_Align, $Tag_Definitions_Name, $local_offsets, $read_next_ptr );

                // Move to the position of the next IFD
                if ( fseek( $filehnd, $Tiff_offset + $Next_Offset ) !== 0 )
                {
                        // Error seeking to position of next IFD
                        error_log( "Error: Corrupted EXIF" );
                        return FALSE;
                }

                $Output_Array[] = $IFD_Array;


        } while ( $Next_Offset != 0 );      // Until the Next IFD Offset is zero


        // return resulting array

        return $Output_Array ;
}

/******************************************************************************
* End of Function:     read_Multiple_IFDs
******************************************************************************/







/******************************************************************************
*
* Internal Function:     read_IFD_universal
*
* Description:  Reads and interprets a standard or Non-standard Image File
*               Directory (IFD), and returns the entries in an array
*
* Parameters:   filehnd - a handle for the image file being read, positioned at the start
*                         of the IFD
*               Tiff_offset - The offset of the TIFF header from the start of the file
*               Byte_Align - either "MM" or "II" indicating Motorola or Intel Byte alignment
*               Tag_Definitions_Name - The name of the Tag Definitions group within the global array IFD_Tag_Definitions
*               local_offsets - True indicates that offset data should be interpreted as being relative to the start of the currrent entry
*                               False (normal) indicates offests are relative to start of Tiff header as per IFD standard
*               read_next_ptr - True (normal) indicates that a pointer to the next IFD should be read at the end of the IFD
*                               False indicates that no pointer follows the IFD
*
* Returns:      OutputArray - Array of IFD entries
*               Next_Offset - Offset to next IFD (zero = no next IFD)
*
******************************************************************************/

function read_IFD_universal( $filehnd, $Tiff_offset, $Byte_Align, $Tag_Definitions_Name, $local_offsets = FALSE, $read_next_ptr = TRUE )
{
        if ( ( $filehnd == NULL ) || ( feof( $filehnd ) ) )
        {
                return array (FALSE , 0);
        }

        // Record the Name of the Tag Group used for this IFD in the output array
        $OutputArray[ 'Tags Name' ] = $Tag_Definitions_Name;

        // Record the offset of the TIFF header in the output array
        $OutputArray[ 'Tiff Offset' ] = $Tiff_offset;

        // First 2 bytes of IFD are number of entries in the IFD
        $No_Entries_str = network_safe_fread( $filehnd, 2 );
        $No_Entries = get_IFD_Data_Type( $No_Entries_str, 3, $Byte_Align );


        // If the data is corrupt, the number of entries may be huge, which will cause errors
        // This is often caused by a lack of a Next-IFD pointer
        if ( $No_Entries> 10000 )
        {
                // Huge number of entries - abort
                // error_log( "Error: huge number of EXIF entries - EXIF is probably Corrupted" );

                return array ( FALSE , 0);
        }

        // If the data is corrupt or just stupid, the number of entries may zero,
        // Indicate this by returning false
        if ( $No_Entries === 0 )
        {
                // No entries - abort
                return array ( FALSE , 0);
        }

        // Save the file position where first IFD record starts as non-standard offsets
        // need to know this to calculate an absolute offset
        $IFD_first_rec_pos = ftell( $filehnd );


        // Read in the IFD structure
        $IFD_Data = network_safe_fread( $filehnd, 12 * $No_Entries );

        // Check if the entire IFD was able to be read
        if ( strlen( $IFD_Data ) != (12 * $No_Entries) )
        {
                // Couldn't read the IFD Data properly, Some Casio files have no Next IFD pointer, hence cause this error
                error_log( "Error: EXIF Corrupted" );
                return array(FALSE, 0);
        }


        // Last 4 bytes of a standard IFD are the offset to the next IFD
        // Some NON-Standard IFD implementations do not have this, hence causing problems if it is read

        // If the Next IFD pointer has been requested to be read,
        if ( $read_next_ptr )
        {
                // Read the pointer to the next IFD

                $Next_Offset_str = network_safe_fread( $filehnd, 4 );
                $Next_Offset = get_IFD_Data_Type( $Next_Offset_str, 4, $Byte_Align );
        }
        else
        {
                // Otherwise set the pointer to zero ( no next IFD )
                $Next_Offset = 0;
        }



        // Initialise current position to the start
        $pos = 0;


        // Loop for reading IFD entries

        for ( $i = 0; $i < $No_Entries; $i++ )
        {
                // First 2 bytes of IFD entry are the tag number ( Unsigned Short )
                $Tag_No_str = substr( $IFD_Data, $pos, 2 );
                $Tag_No = get_IFD_Data_Type( $Tag_No_str, 3, $Byte_Align );
                $pos += 2;

                // Next 2 bytes of IFD entry are the data format ( Unsigned Short )
                $Data_Type_str = substr( $IFD_Data, $pos, 2 );
                $Data_Type = get_IFD_Data_Type( $Data_Type_str, 3, $Byte_Align );
                $pos += 2;

                // If Datatype is not between 1 and 12, then skip this entry, it is probably corrupted or custom
                if (( $Data_Type > 12 ) || ( $Data_Type < 1 ) )
                {
                        $pos += 8;
                        continue 1;  // Stop trying to process the tag any further and skip to the next one
                }

                // Next 4 bytes of IFD entry are the data count ( Unsigned Long )
                $Data_Count_str = substr( $IFD_Data, $pos, 4 );
                $Data_Count = get_IFD_Data_Type( $Data_Count_str, 4, $Byte_Align );
                $pos += 4;

                if ( $Data_Count > 100000 )
                {
                        // error_log( "Error: huge EXIF data count - EXIF is probably Corrupted" );

                        // Some Casio files have no Next IFD pointer, hence cause errors

                        return array ( FALSE , 0);
                }

                // Total Data size is the Data Count multiplied by the size of the Data Type
                $Total_Data_Size = $GLOBALS['IFD_Data_Sizes'][ $Data_Type ] * $Data_Count;

                $Data_Start_pos = -1;

                // If the total data size is larger than 4 bytes, then the data part is the offset to the real data
                if ( $Total_Data_Size > 4 )
                {
                        // Not enough room for data - offset provided instead
                        $Data_Offset_str = substr( $IFD_Data, $pos, 4 );
                        $Data_Start_pos = get_IFD_Data_Type( $Data_Offset_str, 4, $Byte_Align );


                        // In some NON-STANDARD makernotes, the offset is relative to the start of the current IFD entry
                        if ( $local_offsets )
                        {
                                // This is a NON-Standard IFD, seek relative to the start of the current tag
                                fseek( $filehnd, $IFD_first_rec_pos +  $pos - 8 + $Data_Start_pos );
                        }
                        else
                        {
                                // This is a normal IFD, seek relative to the start of the TIFF header
                                fseek( $filehnd, $Tiff_offset + $Data_Start_pos );
                        }

                        // Read the data block from the offset position
                        $DataStr = network_safe_fread( $filehnd, $Total_Data_Size );
                }
                else
                {
                        // The data block is less than 4 bytes, and is provided in the IFD entry, so read it
                        $DataStr = substr( $IFD_Data, $pos, $Total_Data_Size );
                }

                // Increment the position past the data
                $pos += 4;


                // Now create the entry for output array

                $Data_Array = array( );


                // Read the data items from the data block

                if ( ( $Data_Type != 2 ) && ( $Data_Type != 7 ) )
                {
                        // The data type is Numerical, Read the data items from the data block
                        for ( $j = 0; $j < $Data_Count; $j++ )
                        {
                                $Part_Data_Str = substr( $DataStr, $j * $GLOBALS['IFD_Data_Sizes'][ $Data_Type ], $GLOBALS['IFD_Data_Sizes'][ $Data_Type ] );
                                $Data_Array[] = get_IFD_Data_Type( $Part_Data_Str, $Data_Type, $Byte_Align );
                        }
                }
                elseif ( $Data_Type == 2 )
                {
                        // The data type is String(s)   (type 2)

                        // Strip the last terminating Null
                        $DataStr = substr( $DataStr, 0, strlen($DataStr)-1 );

                        // Split the data block into multiple strings whereever there is a Null
                        $Data_Array = explode( "\x00", $DataStr );
                }
                else
                {
                        // The data type is Unknown (type 7)
                        // Do nothing to data
                        $Data_Array = $DataStr;
                }


                // If this is a Sub-IFD entry,
                if ( ( array_key_exists( $Tag_No, $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name] ) ) &&
                     ( "SubIFD" == $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ]['Type'] ) )
                {
                        // This is a Sub-IFD entry, go and process the data forming Sub-IFD and use its output array as the new data for this entry
                        fseek( $filehnd, $Tiff_offset + $Data_Array[0] );
                        $Data_Array = read_Multiple_IFDs( $filehnd, $Tiff_offset, $Byte_Align, $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ]['Tags Name'] );
                }

                $desc = "";
                $units = "";

                // Check if this tag exists in the list of tag definitions,

                if ( array_key_exists ( $Tag_No, $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name]) )
                {

                        if ( array_key_exists ( 'Description', $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ] ) )
                        {
                                $desc = $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ]['Description'];
                        }

                        if ( array_key_exists ( 'Units', $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ] ) )
                        {
                                $units = $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ]['Units'];
                        }

                        // Tag exists in definitions, append details to output array
                        $OutputArray[ $Tag_No ] = array (       "Tag Number"      => $Tag_No,
                                                                "Tag Name"        => $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ]['Name'],
                                                                "Tag Description" => $desc,
                                                                "Data Type"       => $Data_Type,
                                                                "Type"            => $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag_No ]['Type'],
                                                                "Units"           => $units,
                                                                "Data"            => $Data_Array );

                }
                else
                {
                        // Tag doesnt exist in definitions, append unknown details to output array

                        $OutputArray[ $Tag_No ] = array (       "Tag Number"      => $Tag_No,
                                                                "Tag Name"        => "Unknown Tag #" . $Tag_No,
                                                                "Tag Description" => "",
                                                                "Data Type"       => $Data_Type,
                                                                "Type"            => "Unknown",
                                                                "Units"           => "",
                                                                "Data"            => $Data_Array );
                }



                // Some information of type "Unknown" (type 7) might require information about
                // how it's position and byte alignment in order to be decoded
                if ( $Data_Type == 7 )
                {
                        $OutputArray[ $Tag_No ]['Offset'] = $Data_Start_pos;
                        $OutputArray[ $Tag_No ]['Byte Align'] = $Byte_Align;
                }


                ////////////////////////////////////////////////////////////////////////
                // Special Data handling
                ////////////////////////////////////////////////////////////////////////


                // Check if this is a Print Image Matching entry
                if ( $OutputArray[ $Tag_No ]['Type'] == "PIM" )
                {
                        // This is a Print Image Matching entry, decode it.
                        $OutputArray[ $Tag_No ] = Decode_PIM( $OutputArray[ $Tag_No ], $Tag_Definitions_Name );
                }


                // Interpret the entry into a text string using a custom interpreter
                $text_val = get_Tag_Text_Value( $OutputArray[ $Tag_No ], $Tag_Definitions_Name );

                // Check if a text string was generated
                if ( $text_val !== FALSE )
                {
                        // A string was generated, append it to the output array entry
                        $OutputArray[ $Tag_No ]['Text Value'] = $text_val;
                        $OutputArray[ $Tag_No ]['Decoded'] = TRUE;
                }
                else
                {
                        // A string was NOT generated, append a generic string to the output array entry
                        $OutputArray[ $Tag_No ]['Text Value'] = get_IFD_value_as_text( $OutputArray[ $Tag_No ] )  . " " . $units;
                        $OutputArray[ $Tag_No ]['Decoded'] = FALSE;
                }




                // Check if this entry is the Maker Note
                if ( ( $Tag_Definitions_Name == "EXIF" ) && ( $Tag_No == 37500 ) )
                {

                        // Save some extra information which will allow Makernote Decoding with the output array entry
                        $OutputArray[ $Tag_No ]['Offset'] = $Data_Start_pos;
                        $OutputArray[ $Tag_No ][ 'Tiff Offset' ] = $Tiff_offset;
                        $OutputArray[ $Tag_No ]['ByteAlign'] = $Byte_Align;

                        // Save a pointer to this entry for Maker note processing later
                        $GLOBALS[ "Maker_Note_Tag" ] = & $OutputArray[ $Tag_No ];
                }


                // Check if this is a IPTC/NAA Record within the EXIF IFD
                if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) &&
                     ( $Tag_No == 33723 ) )
                {
                        // This is a IPTC/NAA Record, interpret it and put result in the data for this entry
                        $OutputArray[ $Tag_No ]['Data'] = get_IPTC( $DataStr );
                        $OutputArray[ $Tag_No ]['Decoded'] = TRUE;
                }
                // Change: Check for embedded XMP as of version 1.11
                // Check if this is a XMP Record within the EXIF IFD
                if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) &&
                     ( $Tag_No == 700 ) )
                {
                        // This is a XMP Record, interpret it and put result in the data for this entry
                        $OutputArray[ $Tag_No ]['Data'] =  read_XMP_array_from_text( $DataStr );
                        $OutputArray[ $Tag_No ]['Decoded'] = TRUE;
                }

                // Change: Check for embedded IRB as of version 1.11
                // Check if this is a Photoshop IRB Record within the EXIF IFD
                if ( ( ( $Tag_Definitions_Name == "EXIF" ) || ( $Tag_Definitions_Name == "TIFF" ) ) &&
                     ( $Tag_No == 34377 ) )
                {
                        // This is a Photoshop IRB Record, interpret it and put result in the data for this entry
                        $OutputArray[ $Tag_No ]['Data'] = unpack_Photoshop_IRB_Data( $DataStr );
                        $OutputArray[ $Tag_No ]['Decoded'] = TRUE;
                }

                // Exif Thumbnail
                // Check that both the thumbnail length and offset entries have been processed,
                // and that this is one of them
                if ( ( ( ( $Tag_No == 513 ) && ( array_key_exists( 514, $OutputArray ) ) ) ||
                       ( ( $Tag_No == 514 ) && ( array_key_exists( 513, $OutputArray ) ) ) )  &&
                     ( $Tag_Definitions_Name == "TIFF" ) )
                {
                        // Seek to the start of the thumbnail using the offset entry
                        fseek( $filehnd, $Tiff_offset + $OutputArray[513]['Data'][0] );

                        // Read the thumbnail data, and replace the offset data with the thumbnail
                        $OutputArray[513]['Data'] = network_safe_fread( $filehnd, $OutputArray[514]['Data'][0] );
                }


                // Casio Thumbnail
                // Check that both the thumbnail length and offset entries have been processed,
                // and that this is one of them
                if ( ( ( ( $Tag_No == 0x0004 ) && ( array_key_exists( 0x0003, $OutputArray ) ) ) ||
                       ( ( $Tag_No == 0x0003 ) && ( array_key_exists( 0x0004, $OutputArray ) ) ) )  &&
                     ( $Tag_Definitions_Name == "Casio Type 2" ) )
                {
                        // Seek to the start of the thumbnail using the offset entry
                        fseek( $filehnd, $Tiff_offset + $OutputArray[0x0004]['Data'][0] );

                        // Read the thumbnail data, and replace the offset data with the thumbnail
                        $OutputArray[0x0004]['Data'] = network_safe_fread( $filehnd, $OutputArray[0x0003]['Data'][0] );
                }

                // Minolta Thumbnail
                // Check that both the thumbnail length and offset entries have been processed,
                // and that this is one of them
                if ( ( ( ( $Tag_No == 0x0088 ) && ( array_key_exists( 0x0089, $OutputArray ) ) ) ||
                       ( ( $Tag_No == 0x0089 ) && ( array_key_exists( 0x0088, $OutputArray ) ) ) )  &&
                     ( $Tag_Definitions_Name == "Olympus" ) )
                {

                        // Seek to the start of the thumbnail using the offset entry
                        fseek( $filehnd, $Tiff_offset + $OutputArray[0x0088]['Data'][0] );

                        // Read the thumbnail data, and replace the offset data with the thumbnail
                        $OutputArray[0x0088]['Data'] = network_safe_fread( $filehnd, $OutputArray[0x0089]['Data'][0] );

                        // Sometimes the minolta thumbnail data is empty (or the offset is corrupt, which results in the same thing)

                        // Check if the thumbnail data exists
                        if ( $OutputArray[0x0088]['Data'] != "" )
                        {
                                // Thumbnail exists

                                // Minolta Thumbnails are missing their first 0xFF for some reason,
                                // which is replaced with some weird character, so fix this
                                $OutputArray[0x0088]['Data']{0} = "\xFF";
                        }
                        else
                        {
                                // Thumbnail doesnt exist - make it obvious
                                $OutputArray[0x0088]['Data'] = FALSE;
                        }
                }

        }







        // Return the array of IFD entries and the offset to the next IFD

        return array ($OutputArray , $Next_Offset);
}



/******************************************************************************
* End of Function:     read_IFD_universal
******************************************************************************/












/******************************************************************************
*
* Internal Function:     get_Tag_Text_Value
*
* Description:  Attempts to interpret an IFD entry into a text string using the
*               information in the IFD_Tag_Definitions global array.
*
* Parameters:   Tag - The IFD entry to process
*               Tag_Definitions_Name - The name of the tag definitions to use from within the IFD_Tag_Definitions global array
*
* Returns:      String - if the tag was successfully decoded into a text string
*               FALSE - if the tag could not be decoded using the information
*                       in the IFD_Tag_Definitions global array
*
******************************************************************************/

function get_Tag_Text_Value( $Tag, $Tag_Definitions_Name )
{
        // Check what format the entry is specified as

        if ( $Tag['Type'] == "String" )
        {
                // Format is Text String

                // If "Unknown" (type 7) data type,
                if ( $Tag['Data Type'] == 7 )
                {
                        // Return data as is.
                        return $Tag['Data'];
                }
                else
                {
                        // Otherwise return the default string value of the datatype
                        return get_IFD_value_as_text( $Tag );
                }
        }
        else if ( $Tag['Type'] == "Character Coded String" )
        {
                // Format is Character Coded String (First 8 characters indicate coding scheme)

                // Convert Data to a string
                if ( $Tag['Data Type'] == 7 )
                {
                        // If it is type "Unknown" (type 7) use data as is
                        $data =  $Tag['Data'];
                }
                else
                {
                        // Otherwise use the default string value of the datatype
                        $data = get_IFD_value_as_text( $Tag );
                }

                // Some implementations allow completely data with no Coding Scheme Name,
                // so we need to handle this to avoid errors
                if ( trim( $data ) == "" )
                {
                        return "";
                }

                // Extract the Coding Scheme Name from the first 8 characters
                $char_code = substr( $data, 0, 8 );

                // Extract the Data part from after the first 8 characters
                $characters = substr( $data, 8 );

                // Check coding scheme and interpret as neccessary

                if ( $char_code === "ASCII\x00\x00\x00" )
                {
                        // ASCII coding - return data as is.
                        return $characters;
                }
                elseif ( ( $char_code === "UNICODE\x00" ) ||
                         ( $char_code === "Unicode\x00" ) )             // Note lowercase is non standard
                {
                        // Unicode coding - interpret and return result.
                        return xml_UTF16_clean( $characters, TRUE );
                }
                else
                {
                        // Unknown coding - return string indicating this
                        return "Unsupported character coding : \"$char_code\"\n\"" . trim($characters) . "\"";
                }
                break;
        }
        else if ( $Tag['Type'] == "Numeric" )
        {
                // Format is numeric - return default text value with any required units text appended
                if ( array_key_exists ( 'Units', $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag["Tag Number"] ] ) )
                {
                        $units = $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag["Tag Number"] ]['Units'];
                }
                else
                {
                        $units = "";
                }
                return get_IFD_value_as_text( $Tag )  . " " . $units;
        }
        else if  ( $Tag['Type'] == "Lookup" )
        {
                // Format is a Lookup Table

                // Get a numeric value to use in lookup

                if ( is_array( $Tag['Data'] ) )
                {
                        // If data is an array, use first element
                        $first_val = $Tag['Data'][0];
                }
                else if ( is_string( $Tag['Data'] ) )
                {
                        // If data is a string, use the first character
                        $first_val = ord($Tag['Data']{0});
                }
                else
                {
                        // Otherwise use the data as is
                        $first_val = $Tag['Data'];
                }

                // Check if the data value exists in the lookup table for this IFD entry
                if ( array_key_exists( $first_val, $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag["Tag Number"] ] ) )
                {
                        // Data value exists in lookup table - return the matching string
                        return $GLOBALS[ "IFD_Tag_Definitions" ][$Tag_Definitions_Name][ $Tag["Tag Number"] ][ $first_val ];
                }
                else
                {
                        // Data value doesnt exist in lookup table - return explanation string
                        return "Unknown Reserved value $first_val ";
                }
        }
        else if  ( $Tag['Type'] == "Special" )
        {
                // Format is special - interpret to text with special handlers
                return get_Special_Tag_Text_Value( $Tag, $Tag_Definitions_Name );
        }
        else if  ( $Tag['Type'] == "PIM" )
        {
                // Format is Print Image Matching info - interpret with custom handler
                return get_PIM_Text_Value( $Tag, $Tag_Definitions_Name );
        }
        else if  ( $Tag['Type'] == "SubIFD" )
        {
                // Format is a Sub-IFD - this has no text value
                return "";
        }
        else
        {
                // Unknown Format - Couldn't interpret using the IFD_Tag_Definitions global array information
                return FALSE;
        }
}

/******************************************************************************
* End of Function:     get_Tag_Text_Value
******************************************************************************/






/******************************************************************************
*
* Internal Function:     get_Special_Tag_Text_Value
*
* Description:  Interprets an IFD entry marked as "Special" in the IFD_Tag_Definitions
*               global array into a text string using custom handlers
*
* Parameters:   Tag - The IFD entry to process
*               Tag_Definitions_Name - The name of the tag definitions to use from within the IFD_Tag_Definitions global array
*
* Returns:      String - if the tag was successfully decoded into a text string
*               FALSE - if the tag could not be decoded
*
******************************************************************************/

function get_Special_Tag_Text_Value( $Tag, $Tag_Definitions_Name )
{
        // Check what type of IFD is being decoded

        if ( $Tag_Definitions_Name == "TIFF" )
        {
                // This is a TIFF IFD (bottom level)

                // Check what tag number the IFD entry has.
                switch ( $Tag['Tag Number'] )
                {
                        case 530:  // YCbCr Sub Sampling Entry

                                // Data contains two numerical values

                                if ( ( $Tag['Data'][0] == 2 ) && ( $Tag['Data'][1] == 1 ) )
                                {
                                        // Values are 2,1 - hence YCbCr 4:2:2
                                        return "YCbCr 4:2:2 ratio of chrominance components to the luminance components";
                                }
                                elseif ( ( $Tag['Data'][0] == 2 ) && ( $Tag['Data'][1] == 2 ) )
                                {
                                        // Values are 2,2 - hence YCbCr 4:2:0
                                        return "YCbCr 4:2:0 ratio of chrominance components to the luminance components";
                                }
                                else
                                {
                                        // Other values are unknown
                                        return "Unknown Reserved value (" . $Tag['Data'][0] . ")";
                                }
                                break;

                        default:
                                return FALSE;
                }
        }
        else if ( $Tag_Definitions_Name == "EXIF" )
        {
                // This is an EXIF IFD

                // Check what tag number the IFD entry has.
                switch ( $Tag['Tag Number'] )
                {

                        case 37121: // Components configuration

                                // Data contains 4 numerical values indicating component type

                                $output_str = "";

                                // Cycle through each component
                                for ( $Num = 0; $Num < 4; $Num++ )
                                {
                                        // Construct first part of text string
                                        $output_str .= "Component " . ( $Num + 1 ) . ": ";

                                        // Construct second part of text string via
                                        // lookup using numerical value

                                        $value = ord( $Tag['Data']{$Num} );
                                        switch( $value )
                                        {
                                                case 0:
                                                        $output_str .= "Does not exist\n";
                                                        break;
                                                case 1:
                                                        $output_str .= "Y (Luminance)\n";
                                                        break;
                                                case 2:
                                                        $output_str .= "Cb (Chroma minus Blue)\n";
                                                        break;
                                                case 3:
                                                        $output_str .= "Cr (Chroma minus Red)\n";
                                                        break;
                                                case 4:
                                                        $output_str .= "Red\n";
                                                        break;
                                                case 5:
                                                        $output_str .= "Green\n";
                                                        break;
                                                case 6:
                                                        $output_str .= "Blue\n";
                                                        break;
                                                default:
                                                        $output_str .= "Unknown value $value\n";
                                        };
                                }

                                // Return the completed string

                                return $output_str;
                                break;



                        case 41730: // Colour Filter Array Pattern

                                // The first two characters are a SHORT for Horizontal repeat pixel unit -
                                $n_max = get_IFD_Data_Type( substr( $Tag['Data'], 0, 2 ), 3, $Tag['Byte Align'] );

                                // The next two characters are a SHORT for Vertical repeat pixel unit -
                                $m_max = get_IFD_Data_Type( substr( $Tag['Data'], 2, 2 ), 3, $Tag['Byte Align'] );


                                // At least one camera type appears to have byte reversed values for N_Max and M_Max
                                // Check if they need reversing
                                if ( $n_max > 256 )
                                {
                                        $n_max = $n_max/256 + 256*($n_max%256);
                                }

                                if ( $m_max > 256 )
                                {
                                        $m_max = $m_max/256 + 256*($m_max%256);
                                }


                                $output_str = "";


                                // Cycle through all the elements in the resulting 2 dimensional array,
                                for( $m = 1; $m <= $m_max; $m++ )
                                {
                                        for( $n = 1; $n <= $n_max; $n++ )
                                        {

											// Append text from a lookup table according to
											// the value read for this element
											if( isset( $Tag['Data']{($n_max*($m-1)+$n+3)} ) ) {
												switch ( ord($Tag['Data']{($n_max*($m-1)+$n+3)}) )
												{
														case 0:
																$output_str .= "RED     ";
																break;
														case 1:
																$output_str .= "GREEN   ";
																break;
														case 2:
																$output_str .= "BLUE    ";
																break;
														case 3:
																$output_str .= "CYAN    ";
																break;
														case 4:
																$output_str .= "MAGENTA ";
																break;
														case 5:
																$output_str .= "YELLOW  ";
																break;
														case 6:
																$output_str .= "WHITE   ";
																break;
														default:
																$output_str .= "Unknown ";
																break;
												};
											}
                                        };
                                        $output_str .= "\n";
                                };

                                // Return the resulting string
                                return $output_str;
                                break;

                        default:
                                return FALSE;
                }
        }
        else
        {
                // Unknown IFD type, see if it is part of a makernote
                return get_Makernote_Text_Value( $Tag, $Tag_Definitions_Name );
        }


}

/******************************************************************************
* End of Function:     get_Tag_Text_Value
******************************************************************************/








/******************************************************************************
*
* Function:     interpret_IFD
*
* Description:  Generates html detailing the contents a single IFD.
*
* Parameters:   IFD_array - the array containing an IFD
*               filename - the name of the Image file being processed ( used
*                          by scripts which displays EXIF thumbnails)
*
* Returns:      output_str - A string containing the HTML
*
******************************************************************************/

function interpret_IFD( $IFD_array, $filename )
{
        // Create the output string with the table tag
        $output_str = "<table class=\"EXIF_Table\" border=1>\n";

        // Create an extra output string to receive any supplementary html
        // which cannot go inside the table
        $extra_IFD_str = "";

        // Check that the IFD array is valid
        if ( ( $IFD_array === FALSE ) || ( $IFD_array === NULL ) )
        {
                // the IFD array is NOT valid - exit
                return "";
        }

        // Check if this is an EXIF IFD and if there is a makernote present
        if ( ( $IFD_array['Tags Name'] === "EXIF" ) &&
             ( ! array_key_exists( 37500, $IFD_array ) ) )
        {

                // This is an EXIF IFD but NO makernote is present - Add a message to the output
                $extra_IFD_str .= "<h3 class=\"EXIF_Secondary_Heading\">No Makernote Present</h3>";
        }

        // Cycle through each tag in the IFD

        foreach( $IFD_array as $Tag_ID => $Exif_Tag )
        {

                // Ignore the non numeric elements - they aren't tags
                if ( ! is_numeric ( $Tag_ID ) )
                {
                        // Skip Tags Name
                }
                        // Check if the Tag has been decoded successfully
                else if ( $Exif_Tag['Decoded'] == TRUE )
                {
                        // This tag has been successfully decoded

                        // Table cells won't get drawn with nothing in them -
                        // Ensure that at least a non breaking space exists in them

                        if ( trim($Exif_Tag['Text Value']) == "" )
                        {
                                $Exif_Tag['Text Value'] = "&nbsp;";
                        }

                        // Check if the tag is a sub-IFD
                        if ( $Exif_Tag['Type'] == "SubIFD" )
                        {
                                // This is a sub-IFD tag
                                // Add a sub-heading for the sub-IFD
                                $extra_IFD_str .= "<h3 class=\"EXIF_Secondary_Heading\">" . $Exif_Tag['Tag Name'] . " contents</h3>";

                                // Cycle through each sub-IFD in the chain
                                foreach ( $Exif_Tag['Data'] as $subIFD )
                                {
                                        // Interpret this sub-IFD and add the html to the secondary output
                                        $extra_IFD_str .= interpret_IFD( $subIFD, $filename );
                                }
                        }
                                // Check if the tag is a makernote
                        else if ( $Exif_Tag['Type'] == "Maker Note" )
                        {
                                // This is a Makernote Tag
                                // Add a sub-heading for the Makernote
                                $extra_IFD_str .= "<h3 class=\"EXIF_Secondary_Heading\">Maker Note Contents</h3>";

                                // Interpret the Makernote and add the html to the secondary output
                                $extra_IFD_str .= Interpret_Makernote_to_HTML( $Exif_Tag, $filename );
                        }
                                // Check if this is a IPTC/NAA Record within the EXIF IFD
                        else if ( $Exif_Tag['Type'] == "IPTC" )
                        {
                                // This is a IPTC/NAA Record, interpret it and output to the secondary html
                                $extra_IFD_str .= "<h3 class=\"EXIF_Secondary_Heading\">Contains IPTC/NAA Embedded in EXIF</h3>";
                                $extra_IFD_str .=Interpret_IPTC_to_HTML( $Exif_Tag['Data'] );
                        }
                                // Change: Check for embedded XMP as of version 1.11
                                // Check if this is a XMP Record within the EXIF IFD
                        else if ( $Exif_Tag['Type'] == "XMP" )
                        {
                                // This is a XMP Record, interpret it and output to the secondary html
                                $extra_IFD_str .= "<h3 class=\"EXIF_Secondary_Heading\">Contains XMP Embedded in EXIF</h3>";
                                $extra_IFD_str .= Interpret_XMP_to_HTML( $Exif_Tag['Data'] );
                        }
                                // Change: Check for embedded IRB as of version 1.11
                                // Check if this is a Photoshop IRB Record within the EXIF IFD
                        else if ( $Exif_Tag['Type'] == "IRB" )
                        {
                                // This is a Photoshop IRB Record, interpret it and output to the secondary html
                                $extra_IFD_str .= "<h3 class=\"EXIF_Secondary_Heading\">Contains Photoshop IRB Embedded in EXIF</h3>";
                                $extra_IFD_str .= Interpret_IRB_to_HTML( $Exif_Tag['Data'], $filename );
                        }
                                // Check if the tag is Numeric
                        else if ( $Exif_Tag['Type'] == "Numeric" )
                        {
                                // Numeric Tag - Output text value as is.
                                $output_str .= "<tr class=\"EXIF_Table_Row\"><td class=\"EXIF_Caption_Cell\">" . $Exif_Tag['Tag Name'] . "</td><td class=\"EXIF_Value_Cell\">" . $Exif_Tag['Text Value'] . "</td></tr>\n";
                        }
                        else
                        {
                                // Other tag - Output text as preformatted
                                $output_str .= "<tr class=\"EXIF_Table_Row\"><td class=\"EXIF_Caption_Cell\">" . $Exif_Tag['Tag Name'] . "</td><td class=\"EXIF_Value_Cell\"><pre>" . trim( $Exif_Tag['Text Value']) . "</pre></td></tr>\n";
                        }

                }
                else
                {
                        // Tag has NOT been decoded successfully
                        // Hence it is either an unknown tag, or one which
                        // requires processing at the time of html construction

                        // Table cells won't get drawn with nothing in them -
                        // Ensure that at least a non breaking space exists in them

                        if ( trim($Exif_Tag['Text Value']) == "" )
                        {
                                $Exif_Tag['Text Value'] = "&nbsp;";
                        }

                        // Check if this tag is the first IFD Thumbnail
                        if ( ( $IFD_array['Tags Name'] == "TIFF" ) &&
                             ( $Tag_ID == 513 ) )
                        {
                                // This is the first IFD thumbnail - Add html to the output

                                // Change: as of version 1.11 - Changed to make thumbnail link portable across directories
                                // Build the path of the thumbnail script and its filename parameter to put in a url
                                $link_str = get_relative_path( dirname(__FILE__) . "/get_exif_thumb.php" , getcwd ( ) );
                                $link_str .= "?filename=";
                                $link_str .= get_relative_path( $filename, dirname(__FILE__) );

                                // Add thumbnail link to html
                                $output_str .= "<tr class=\"EXIF_Table_Row\"><td class=\"EXIF_Caption_Cell\">" . $Exif_Tag['Tag Name'] . "</td><td class=\"EXIF_Value_Cell\"><a class=\"EXIF_First_IFD_Thumb_Link\" href=\"$link_str\"><img class=\"EXIF_First_IFD_Thumb\" src=\"$link_str\"></a></td></tr>\n";
                        }
                                // Check if this is the Makernote
                        else if ( $Exif_Tag['Type'] == "Maker Note" )
                        {
                                // This is the makernote, but has not been decoded
                                // Add a message to the secondary output
                                $extra_IFD_str .= "<h3 class=\"EXIF_Secondary_Heading\">Makernote Coding Unknown</h3>\n";
                        }
                        else
                        {
                                // This is an Unknown Tag

                                // Check if the user wants to hide unknown tags
                                if ( $GLOBALS['HIDE_UNKNOWN_TAGS'] === FALSE )
                                {
                                        // User wants to display unknown tags

                                        // Check if the Data is an ascii string
                                        if ( $Exif_Tag['Data Type'] == 2 )
                                        {
                                                // This is a Ascii String field - add it preformatted to the output
                                                $output_str .= "<tr class=\"EXIF_Table_Row\"><td class=\"EXIF_Caption_Cell\">" . $Exif_Tag['Tag Name'] . "</td><td class=\"EXIF_Value_Cell\"><pre>" . trim( $Exif_Tag['Text Value'] ) . "</pre></td></tr>\n";
                                        }
                                        else
                                        {
                                                // Not an ASCII string - add it as is to the output
                                                $output_str .= "<tr class=\"EXIF_Table_Row\"><td class=\"EXIF_Caption_Cell\">" . $Exif_Tag['Tag Name'] . "</td><td class=\"EXIF_Value_Cell\">" . trim( $Exif_Tag['Text Value'] ) . "</td></tr>\n";
                                        }
                                }
                        }
                }
        }

        // Close the table in the output
        $output_str .= "</table>\n";

        // Add the secondary output at the end of the main output
        $output_str .= "$extra_IFD_str\n";

        // Return the resulting html
        return $output_str;
}

/******************************************************************************
* End of Function:     interpret_IFD
******************************************************************************/















/******************************************************************************
*
* Function:     get_IFD_Data_Type
*
* Description:  Decodes an IFD field value from a binary data string, using
*               information supplied about the data type and byte alignment of
*               the stored data.
*               This function should be used for all datatypes except ASCII strings
*
* Parameters:   input_data - a binary data string containing the IFD value,
*                            must be exact length of the value
*               data_type - a number representing the IFD datatype as per the
*                           TIFF 6.0 specification:
*                               1 = Unsigned 8-bit Byte
*                               2 = ASCII String
*                               3 = Unsigned 16-bit Short
*                               4 = Unsigned 32-bit Long
*                               5 = Unsigned 2x32-bit Rational
*                               6 = Signed 8-bit Byte
*                               7 = Undefined
*                               8 = Signed 16-bit Short
*                               9 = Signed 32-bit Long
*                               10 = Signed 2x32-bit Rational
*                               11 = 32-bit Float
*                               12 = 64-bit Double
*               Byte_Align - Indicates the byte alignment of the data.
*                            MM = Motorola, MSB first, Big Endian
*                            II = Intel, LSB first, Little Endian
*
* Returns:      output - the value of the data (string or numeric)
*
******************************************************************************/

function get_IFD_Data_Type( $input_data, $data_type, $Byte_Align )
{
	$value = NULL;
	if( !empty( $input_data ) ) {
        // Check if this is a Unsigned Byte, Unsigned Short or Unsigned Long
        if (( $data_type == 1 ) || ( $data_type == 3 ) || ( $data_type == 4 ))
        {
                // This is a Unsigned Byte, Unsigned Short or Unsigned Long

                // Check the byte alignment to see if the bytes need tp be reversed
                if ( $Byte_Align == "II" )
                {
                        // This is in Intel format, reverse it
                        $input_data = strrev ( $input_data );
                }

                // Convert the binary string to a number and return it
                return hexdec( bin2hex( $input_data ) );
        }
                // Check if this is a ASCII string type
        elseif ( $data_type == 2 )
        {
                // Null terminated ASCII string(s)
                // The input data may represent multiple strings, as the
                // 'count' field represents the total bytes, not the number of strings
                // Hence this should not be processed here, as it would have
                // to return multiple values instead of a single value

                error_log( "Error - ASCII Strings should not be processed in get_IFD_Data_Type" );
                return "Error Should never get here"; //explode( "\x00", $input_data );
        }
                // Check if this is a Unsigned rational type
        elseif ( $data_type == 5 )
        {
                // This is a Unsigned rational type

                // Check the byte alignment to see if the bytes need to be reversed
                if ( $Byte_Align == "MM" )
                {
                        // Motorola MSB first byte aligment
                        // Unpack the Numerator and denominator and return them
                        return @unpack( 'NNumerator/NDenominator', $input_data );
                }
                else
                {
                        // Intel LSB first byte aligment
                        // Unpack the Numerator and denominator and return them
                        return @unpack( 'VNumerator/VDenominator', $input_data );
                }
        }
                // Check if this is a Signed Byte, Signed Short or Signed Long
        elseif ( ( $data_type == 6 ) || ( $data_type == 8 ) || ( $data_type == 9 ) )
        {
                // This is a Signed Byte, Signed Short or Signed Long

                // Check the byte alignment to see if the bytes need to be reversed
                if ( $Byte_Align == "II" )
                {
                        //Intel format, reverse the bytes
                        $input_data = strrev ( $input_data );
                }

                // Convert the binary string to an Unsigned number
                $value = hexdec( bin2hex( $input_data ) );

                // Convert to signed number

                // Check if it is a Byte above 128 (i.e. a negative number)
                if ( ( $data_type == 6 ) && ( $value > 128 ) )
                {
                        // number should be negative - make it negative
                        return  $value - 256;
                }

                // Check if it is a Short above 32767 (i.e. a negative number)
                if ( ( $data_type == 8 ) && ( $value > 32767 ) )
                {
                        // number should be negative - make it negative
                        return  $value - 65536;
                }

                // Check if it is a Long above 2147483648 (i.e. a negative number)
                if ( ( $data_type == 9 ) && ( $value > 2147483648 ) )
                {
                        // number should be negative - make it negative
                        return  $value - 4294967296;
                }

                // Return the signed number
                return $value;
        }
                // Check if this is Undefined type
        elseif ( $data_type == 7 )
        {
                // Custom Data - Do nothing
                return $input_data;
        }
                // Check if this is a Signed Rational type
        elseif ( $data_type == 10 )
        {
                // This is a Signed Rational type

                // Signed Long not available with endian in unpack , use unsigned and convert

                // Check the byte alignment to see if the bytes need to be reversed
                if ( $Byte_Align == "MM" )
                {
                        // Motorola MSB first byte aligment
                        // Unpack the Numerator and denominator
                        $value = @unpack( 'NNumerator/NDenominator', $input_data );
                }
                else
                {
                        // Intel LSB first byte aligment
                        // Unpack the Numerator and denominator
                        $value = @unpack( 'VNumerator/VDenominator', $input_data );
                }

                // Convert the numerator to a signed number
                // Check if it is above 2147483648 (i.e. a negative number)
                if ( $value['Numerator'] > 2147483648 )
                {
                        // number is negative
                        $value['Numerator'] -= 4294967296;
                }

                // Convert the denominator to a signed number
                // Check if it is above 2147483648 (i.e. a negative number)
                if ( $value['Denominator'] > 2147483648 )
                {
                        // number is negative
                        $value['Denominator'] -= 4294967296;
                }

                // Return the Signed Rational value
                return $value;
        }
                // Check if this is a Float type
        elseif ( $data_type == 11 )
        {
                // IEEE 754 Float
                // TODO - EXIF - IFD datatype Float not implemented yet
                return "FLOAT NOT IMPLEMENTED YET";
        }
                // Check if this is a Double type
        elseif ( $data_type == 12 )
        {
                // IEEE 754 Double
                // TODO - EXIF - IFD datatype Double not implemented yet
                return "DOUBLE NOT IMPLEMENTED YET";
        }
        else
        {
                // Error - Invalid Datatype
                return "Invalid Datatype $data_type";

        }
	} else {
		return "Invalid Datatype $data_type";
	}

}

/******************************************************************************
* End of Function:     get_IFD_Data_Type
******************************************************************************/






/******************************************************************************
*
* Function:     put_IFD_Data_Type
*
* Description:  Encodes an IFD field from a value to a binary data string, using
*               information supplied about the data type and byte alignment of
*               the stored data.
*
* Parameters:   input_data - an IFD data value, numeric or string
*               data_type - a number representing the IFD datatype as per the
*                           TIFF 6.0 specification:
*                               1 = Unsigned 8-bit Byte
*                               2 = ASCII String
*                               3 = Unsigned 16-bit Short
*                               4 = Unsigned 32-bit Long
*                               5 = Unsigned 2x32-bit Rational
*                               6 = Signed 8-bit Byte
*                               7 = Undefined
*                               8 = Signed 16-bit Short
*                               9 = Signed 32-bit Long
*                               10 = Signed 2x32-bit Rational
*                               11 = 32-bit Float
*                               12 = 64-bit Double
*               Byte_Align - Indicates the byte alignment of the data.
*                            MM = Motorola, MSB first, Big Endian
*                            II = Intel, LSB first, Little Endian
*
* Returns:      output - the packed binary string of the data
*
******************************************************************************/

function put_IFD_Data_Type( $input_data, $data_type, $Byte_Align )
{
        // Process according to the datatype
        switch ( $data_type )
        {
                case 1: // Unsigned Byte - return character as is
                        return chr($input_data);
                        break;

                case 2: // ASCII String
                        // Return the string with terminating null
                        return $input_data . "\x00";
                        break;

                case 3: // Unsigned Short
                        // Check byte alignment
                        if ( $Byte_Align == "II" )
                        {
                                // Intel/Little Endian - pack the short and return
                                return pack( "v", $input_data );
                        }
                        else
                        {
                                // Motorola/Big Endian - pack the short and return
                                return pack( "n", $input_data );
                        }
                        break;

                case 4: // Unsigned Long
                        // Check byte alignment
                        if ( $Byte_Align == "II" )
                        {
                                // Intel/Little Endian - pack the long and return
                                return pack( "V", $input_data );
                        }
                        else
                        {
                                // Motorola/Big Endian - pack the long and return
                                return pack( "N", $input_data );
                        }
                        break;

                case 5: // Unsigned Rational
                        // Check byte alignment
                        if ( $Byte_Align == "II" )
                        {
                                // Intel/Little Endian - pack the two longs and return
                                return pack( "VV", $input_data['Numerator'], $input_data['Denominator'] );
                        }
                        else
                        {
                                // Motorola/Big Endian - pack the two longs and return
                                return pack( "NN", $input_data['Numerator'], $input_data['Denominator'] );
                        }
                        break;

                case 6: // Signed Byte
                        // Check if number is negative
                        if ( $input_data < 0 )
                        {
                                // Number is negative - return signed character
                                return chr( $input_data + 256 );
                        }
                        else
                        {
                                // Number is positive - return character
                                return chr( $input_data );
                        }
                        break;

                case 7: // Unknown - return as is
                        return $input_data;
                        break;

                case 8: // Signed Short
                        // Check if number is negative
                        if (  $input_data < 0 )
                        {
                                // Number is negative - make signed value
                                $input_data = $input_data + 65536;
                        }
                        // Check byte alignment
                        if ( $Byte_Align == "II" )
                        {
                                // Intel/Little Endian - pack the short and return
                                return pack( "v", $input_data );
                        }
                        else
                        {
                                // Motorola/Big Endian - pack the short and return
                                return pack( "n", $input_data );
                        }
                        break;

                case 9: // Signed Long
                        // Check if number is negative
                        if (  $input_data < 0 )
                        {
                                // Number is negative - make signed value
                                $input_data = $input_data + 4294967296;
                        }
                        // Check byte alignment
                        if ( $Byte_Align == "II" )
                        {
                                // Intel/Little Endian - pack the long and return
                                return pack( "v", $input_data );
                        }
                        else
                        {
                                // Motorola/Big Endian - pack the long and return
                                return pack( "n", $input_data );
                        }
                        break;

                case 10: // Signed Rational
                        // Check if numerator is negative
                        if (  $input_data['Numerator'] < 0 )
                        {
                                // Number is numerator - make signed value
                                $input_data['Numerator'] = $input_data['Numerator'] + 4294967296;
                        }
                        // Check if denominator is negative
                        if (  $input_data['Denominator'] < 0 )
                        {
                                // Number is denominator - make signed value
                                $input_data['Denominator'] = $input_data['Denominator'] + 4294967296;
                        }
                        // Check byte alignment
                        if ( $Byte_Align == "II" )
                        {
                                // Intel/Little Endian - pack the two longs and return
                                return pack( "VV", $input_data['Numerator'], $input_data['Denominator'] );
                        }
                        else
                        {
                                // Motorola/Big Endian - pack the two longs and return
                                return pack( "NN", $input_data['Numerator'], $input_data['Denominator'] );
                        }
                        break;

                case 11: // Float
                        // IEEE 754 Float
                        // TODO - EXIF - IFD datatype Float not implemented yet
                        return "FLOAT NOT IMPLEMENTED YET";
                        break;

                case 12: // Double
                        // IEEE 754 Double
                        // TODO - EXIF - IFD datatype Double not implemented yet
                        return "DOUBLE NOT IMPLEMENTED YET";
                        break;

                default:
                        // Error - Invalid Datatype
                        return "Invalid Datatype $data_type";
                        break;

        }

        // Shouldn't get here
        return FALSE;
}

/******************************************************************************
* End of Function:     put_IFD_Data_Type
******************************************************************************/





/******************************************************************************
*
* Function:     get_IFD_value_as_text
*
* Description:  Decodes an IFD field value from a binary data string, using
*               information supplied about the data type and byte alignment of
*               the stored data.
*               This function should be used for all datatypes except ASCII strings
*
* Parameters:   input_data - a binary data string containing the IFD value,
*                            must be exact length of the value
*               data_type - a number representing the IFD datatype as per the
*                           TIFF 6.0 specification:
*                               1 = Unsigned 8-bit Byte
*                               2 = ASCII String
*                               3 = Unsigned 16-bit Short
*                               4 = Unsigned 32-bit Long
*                               5 = Unsigned 2x32-bit Rational
*                               6 = Signed 8-bit Byte
*                               7 = Undefined
*                               8 = Signed 16-bit Short
*                               9 = Signed 32-bit Long
*                               10 = Signed 2x32-bit Rational
*                               11 = 32-bit Float
*                               12 = 64-bit Double
*               Byte_Align - Indicates the byte alignment of the data.
*                            MM = Motorola, MSB first, Big Endian
*                            II = Intel, LSB first, Little Endian
*
* Returns:      output - the value of the data (string or numeric)
*
******************************************************************************/

function get_IFD_value_as_text( $Exif_Tag )
{
        // Create a string to receive the output text
        $output_str = "";

if( !empty( $Exif_Tag['Data Type'] ) ) {
        // Select Processing method according to the datatype
        switch  ($Exif_Tag['Data Type'])
        {
                case 1 : // Unsigned Byte
                case 3 : // Unsigned Short
                case 4 : // Unsigned Long
                case 6 : // Signed Byte
                case 8 : // Signed Short
                case 9 : // Signed Long

                        // Cycle through each of the values for this tag
                        foreach ( $Exif_Tag['Data'] as $val )
                        {
                                // Check that this isn't the first value,
                                if ( $output_str != "" )
                                {
                                        // This isn't the first value, Add a Comma and Newline to the output
                                        $output_str .= ",\n";
                                }
                                // Add the Value to the output
                                $output_str .= $val;
                        }
                        break;

                case 2 : // ASCII
                        // Append all the strings together, separated by Newlines
                        $output_str .= implode ( "\n", $Exif_Tag['Data']);
                        break;

                case 5 : // Unsigned Rational
                case 10: // Signed Rational

                        // Cycle through each of the values for this tag
                        foreach ( $Exif_Tag['Data'] as $val )
                        {
                                // Check that this isn't the first value,
                                if ( $output_str != "" )
                                {
                                        // This isn't the first value, Add a Comma and Newline to the output
                                        $output_str .= ",\n";
                                }

                                // Add the Full Value to the output
                                $output_str .= $val['Numerator'] ."/" . $val['Denominator'];

                                // Check if division by zero might be a problem
                                if ( $val['Denominator'] != 0 )
                                {
                                        // Denominator is not zero, Add the Decimal Value to the output text
                                        $output_str .= " (" . ($val['Numerator'] / $val['Denominator']) . ")";
                                }
                        }
                        break;

                case 11: // Float
                case 12: // Double
                        // TODO - EXIF - IFD datatype Double and Float not implemented yet
                        $output_str .= "Float and Double not implemented yet";
                        break;

                case 7 : // Undefined
                        // Unless the User has asked to see the raw binary data, this
                        // type should not be displayed

                        // Check if the user has requested to see the binary data in hex
                        if ( $GLOBALS['SHOW_BINARY_DATA_HEX'] == TRUE)
                        {
                                // User has requested to see the binary data in hex
                                // Add the value in hex
                                $output_str .= "( " . strlen( $Exif_Tag['Data'] ) . " bytes of binary data ): " . bin2hex( $Exif_Tag['Data'] )  ;
                        }
                                // Check if the user has requested to see the binary data as is
                        else if ( $GLOBALS['SHOW_BINARY_DATA_TEXT'] == TRUE)
                        {
                                // User has requested to see the binary data as is
                                // Add the value as is
                                $output_str .= "( " . strlen( $Exif_Tag['Data'] ) . " bytes of binary data ): " . $Exif_Tag['Data']  ;
                        }
                        else
                        {
                                // User has NOT requested to see binary data,
                                // Add a message indicating the number of bytes to the output
                                $output_str .= "( " . strlen( $Exif_Tag['Data'] ) . " bytes of binary data ) "  ;
                        }
                        break;

                default :
                        // Error - Unknown IFD datatype
                        $output_str .= "Error - Exif tag data type (" . $Exif_Tag['Data Type'] .") is invalid";
                        break;
        }
}

        // Return the resulting text string
        return $output_str;
}

/******************************************************************************
* End of Function:     get_IFD_value_as_text
******************************************************************************/




/******************************************************************************
* Global Variable:      IFD_Data_Sizes
*
* Contents:     The sizes (in bytes) of each EXIF IFD Datatype, indexed by
*               their datatype number
*
******************************************************************************/

$GLOBALS['IFD_Data_Sizes'] = array(     1 => 1,         // Unsigned Byte
                                        2 => 1,         // ASCII String
                                        3 => 2,         // Unsigned Short
                                        4 => 4,         // Unsigned Long
                                        5 => 8,         // Unsigned Rational
                                        6 => 1,         // Signed Byte
                                        7 => 1,         // Undefined
                                        8 => 2,         // Signed Short
                                        9 => 4,         // Signed Long
                                        10 => 8,        // Signed Rational
                                        11 => 4,        // Float
                                        12 => 8 );      // Double

/******************************************************************************
* End of Global Variable:     IFD_Data_Sizes
******************************************************************************/






?>