This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
PelIfd.php
1590 lines (1481 loc) · 54.8 KB
/
PelIfd.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
<?php
/**
* PEL: PHP Exif Library.
* A library with support for reading and
* writing all Exif headers in JPEG and TIFF images using PHP.
*
* Copyright (C) 2004, 2005, 2006, 2007, 2008 Martin Geisler.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in the file COPYING; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
namespace lsolesen\pel;
/**
* Classes for dealing with Exif IFDs.
*
* @author Martin Geisler <mgeisler@users.sourceforge.net>
* @license http://www.gnu.org/licenses/gpl.html GNU General Public
* License (GPL)
* @package PEL
*/
/**
* Class representing an Image File Directory (IFD).
*
* {@link PelTiff TIFF data} is structured as a number of Image File
* Directories, IFDs for short. Each IFD contains a number of {@link
* PelEntry entries}, some data and finally a link to the next IFD.
*
* @author Martin Geisler <mgeisler@users.sourceforge.net>
* @package PEL
*/
class PelIfd implements \IteratorAggregate, \ArrayAccess
{
/**
* Main image IFD.
*
* Pass this to the constructor when creating an IFD which will be
* the IFD of the main image.
*/
const IFD0 = 0;
/**
* Thumbnail image IFD.
*
* Pass this to the constructor when creating an IFD which will be
* the IFD of the thumbnail image.
*/
const IFD1 = 1;
/**
* Exif IFD.
*
* Pass this to the constructor when creating an IFD which will be
* the Exif sub-IFD.
*/
const EXIF = 2;
/**
* GPS IFD.
*
* Pass this to the constructor when creating an IFD which will be
* the GPS sub-IFD.
*/
const GPS = 3;
/**
* Interoperability IFD.
*
* Pass this to the constructor when creating an IFD which will be
* the interoperability sub-IFD.
*/
const INTEROPERABILITY = 4;
/**
* Canon Maker Notes IFD.
*
* Pass this to the constructor when creating an IFD which will be
* the canon maker notes sub-IFD.
*/
const CANON_MAKER_NOTES = 5;
/**
* Canon Camera Settings IFD.
*
* Pass this to the constructor when creating an IFD which will be
* the canon maker notes sub-IFD.
*/
const CANON_CAMERA_SETTINGS = 6;
/**
* Canon Shot Info IFD.
*
* Pass this to the constructor when creating an IFD which will be
* the canon maker notes sub-IFD.
*/
const CANON_SHOT_INFO = 7;
/**
* Canon Shot Info IFD.
*
* Pass this to the constructor when creating an IFD which will be
* the canon maker notes sub-IFD.
*/
const CANON_PANORAMA = 8;
/**
* Canon Shot Info IFD.
*
* Pass this to the constructor when creating an IFD which will be
* the canon maker notes sub-IFD.
*/
const CANON_PICTURE_INFO = 9;
/**
* Canon Shot Info IFD.
*
* Pass this to the constructor when creating an IFD which will be
* the canon maker notes sub-IFD.
*/
const CANON_FILE_INFO = 10;
/**
* Canon Shot Info IFD.
*
* Pass this to the constructor when creating an IFD which will be
* the canon maker notes sub-IFD.
*/
const CANON_CUSTOM_FUNCTIONS = 11;
private const TYPE_NAMES = [
self::IFD0 => '0',
self::IFD1 => '1',
self::EXIF => 'Exif',
self::GPS => 'GPS',
self::INTEROPERABILITY => 'Interoperability',
self::CANON_MAKER_NOTES => 'Canon Maker Notes',
self::CANON_CAMERA_SETTINGS => 'Canon Camera Settings',
self::CANON_SHOT_INFO => 'Canon Shot Information',
self::CANON_PANORAMA => 'Canon Panorama Information',
self::CANON_PICTURE_INFO => 'Canon Picture Information',
self::CANON_FILE_INFO => 'Canon File Information',
self::CANON_CUSTOM_FUNCTIONS => 'Canon Custom Functions'
];
private const VALID_TAGS = [
self::IFD0 => [
PelTag::IMAGE_WIDTH,
PelTag::IMAGE_LENGTH,
PelTag::BITS_PER_SAMPLE,
PelTag::COMPRESSION,
PelTag::PHOTOMETRIC_INTERPRETATION,
PelTag::DOCUMENT_NAME,
PelTag::IMAGE_DESCRIPTION,
PelTag::MAKE,
PelTag::MODEL,
PelTag::STRIP_OFFSETS,
PelTag::ORIENTATION,
PelTag::SAMPLES_PER_PIXEL,
PelTag::ROWS_PER_STRIP,
PelTag::STRIP_BYTE_COUNTS,
PelTag::X_RESOLUTION,
PelTag::Y_RESOLUTION,
PelTag::PLANAR_CONFIGURATION,
PelTag::RESOLUTION_UNIT,
PelTag::TRANSFER_FUNCTION,
PelTag::SOFTWARE,
PelTag::DATE_TIME,
PelTag::ARTIST,
PelTag::PREDICTOR,
PelTag::WHITE_POINT,
PelTag::PRIMARY_CHROMATICITIES,
PelTag::EXTRA_SAMPLES,
PelTag::SAMPLE_FORMAT,
PelTag::JPEG_INTERCHANGE_FORMAT,
PelTag::JPEG_INTERCHANGE_FORMAT_LENGTH,
PelTag::YCBCR_COEFFICIENTS,
PelTag::YCBCR_SUB_SAMPLING,
PelTag::YCBCR_POSITIONING,
PelTag::REFERENCE_BLACK_WHITE,
PelTag::COPYRIGHT,
PelTag::EXIF_IFD_POINTER,
PelTag::GPS_INFO_IFD_POINTER,
PelTag::PRINT_IM,
PelTag::XP_TITLE,
PelTag::XP_COMMENT,
PelTag::XP_AUTHOR,
PelTag::XP_KEYWORDS,
PelTag::XP_SUBJECT,
PelTag::RATING,
PelTag::RATING_PERCENT,
PelTag::APPLICATION_NOTES
],
self::EXIF => [
PelTag::EXPOSURE_TIME,
PelTag::FNUMBER,
PelTag::EXPOSURE_PROGRAM,
PelTag::SPECTRAL_SENSITIVITY,
PelTag::ISO_SPEED_RATINGS,
PelTag::OECF,
PelTag::EXIF_VERSION,
PelTag::DATE_TIME_ORIGINAL,
PelTag::DATE_TIME_DIGITIZED,
PelTag::OFFSET_TIME,
PelTag::OFFSET_TIME_ORIGINAL,
PelTag::OFFSET_TIME_DIGITIZED,
PelTag::COMPONENTS_CONFIGURATION,
PelTag::COMPRESSED_BITS_PER_PIXEL,
PelTag::SHUTTER_SPEED_VALUE,
PelTag::APERTURE_VALUE,
PelTag::BRIGHTNESS_VALUE,
PelTag::EXPOSURE_BIAS_VALUE,
PelTag::MAX_APERTURE_VALUE,
PelTag::SUBJECT_DISTANCE,
PelTag::METERING_MODE,
PelTag::LIGHT_SOURCE,
PelTag::FLASH,
PelTag::FOCAL_LENGTH,
PelTag::MAKER_NOTE,
PelTag::USER_COMMENT,
PelTag::SUB_SEC_TIME,
PelTag::SUB_SEC_TIME_ORIGINAL,
PelTag::SUB_SEC_TIME_DIGITIZED,
PelTag::FLASH_PIX_VERSION,
PelTag::COLOR_SPACE,
PelTag::PIXEL_X_DIMENSION,
PelTag::PIXEL_Y_DIMENSION,
PelTag::RELATED_SOUND_FILE,
PelTag::FLASH_ENERGY,
PelTag::SPATIAL_FREQUENCY_RESPONSE,
PelTag::FOCAL_PLANE_X_RESOLUTION,
PelTag::FOCAL_PLANE_Y_RESOLUTION,
PelTag::FOCAL_PLANE_RESOLUTION_UNIT,
PelTag::SUBJECT_LOCATION,
PelTag::EXPOSURE_INDEX,
PelTag::SENSING_METHOD,
PelTag::FILE_SOURCE,
PelTag::SCENE_TYPE,
PelTag::CFA_PATTERN,
PelTag::CUSTOM_RENDERED,
PelTag::EXPOSURE_MODE,
PelTag::WHITE_BALANCE,
PelTag::DIGITAL_ZOOM_RATIO,
PelTag::FOCAL_LENGTH_IN_35MM_FILM,
PelTag::SCENE_CAPTURE_TYPE,
PelTag::GAIN_CONTROL,
PelTag::CONTRAST,
PelTag::SATURATION,
PelTag::SHARPNESS,
PelTag::DEVICE_SETTING_DESCRIPTION,
PelTag::SUBJECT_DISTANCE_RANGE,
PelTag::IMAGE_UNIQUE_ID,
PelTag::INTEROPERABILITY_IFD_POINTER,
PelTag::GAMMA
],
self::GPS => [
PelTag::GPS_VERSION_ID,
PelTag::GPS_LATITUDE_REF,
PelTag::GPS_LATITUDE,
PelTag::GPS_LONGITUDE_REF,
PelTag::GPS_LONGITUDE,
PelTag::GPS_ALTITUDE_REF,
PelTag::GPS_ALTITUDE,
PelTag::GPS_TIME_STAMP,
PelTag::GPS_SATELLITES,
PelTag::GPS_STATUS,
PelTag::GPS_MEASURE_MODE,
PelTag::GPS_DOP,
PelTag::GPS_SPEED_REF,
PelTag::GPS_SPEED,
PelTag::GPS_TRACK_REF,
PelTag::GPS_TRACK,
PelTag::GPS_IMG_DIRECTION_REF,
PelTag::GPS_IMG_DIRECTION,
PelTag::GPS_MAP_DATUM,
PelTag::GPS_DEST_LATITUDE_REF,
PelTag::GPS_DEST_LATITUDE,
PelTag::GPS_DEST_LONGITUDE_REF,
PelTag::GPS_DEST_LONGITUDE,
PelTag::GPS_DEST_BEARING_REF,
PelTag::GPS_DEST_BEARING,
PelTag::GPS_DEST_DISTANCE_REF,
PelTag::GPS_DEST_DISTANCE,
PelTag::GPS_PROCESSING_METHOD,
PelTag::GPS_AREA_INFORMATION,
PelTag::GPS_DATE_STAMP,
PelTag::GPS_DIFFERENTIAL
],
self::INTEROPERABILITY => [
PelTag::INTEROPERABILITY_INDEX,
PelTag::INTEROPERABILITY_VERSION,
PelTag::RELATED_IMAGE_FILE_FORMAT,
PelTag::RELATED_IMAGE_WIDTH,
PelTag::RELATED_IMAGE_LENGTH
],
self::CANON_MAKER_NOTES => [
PelTag::CANON_CAMERA_SETTINGS,
PelTag::CANON_FOCAL_LENGTH,
PelTag::CANON_SHOT_INFO,
PelTag::CANON_PANORAMA,
PelTag::CANON_IMAGE_TYPE,
PelTag::CANON_FIRMWARE_VERSION,
PelTag::CANON_FILE_NUMBER,
PelTag::CANON_OWNER_NAME,
PelTag::CANON_SERIAL_NUMBER,
PelTag::CANON_CAMERA_INFO,
PelTag::CANON_CUSTOM_FUNCTIONS,
PelTag::CANON_MODEL_ID,
PelTag::CANON_PICTURE_INFO,
PelTag::CANON_THUMBNAIL_IMAGE_VALID_AREA,
PelTag::CANON_SERIAL_NUMBER_FORMAT,
PelTag::CANON_SUPER_MACRO,
PelTag::CANON_FIRMWARE_REVISION,
PelTag::CANON_AF_INFO,
PelTag::CANON_ORIGINAL_DECISION_DATA_OFFSET,
PelTag::CANON_WHITE_BALANCE_TABLE,
PelTag::CANON_LENS_MODEL,
PelTag::CANON_INTERNAL_SERIAL_NUMBER,
PelTag::CANON_DUST_REMOVAL_DATA,
PelTag::CANON_CUSTOM_FUNCTIONS_2,
PelTag::CANON_PROCESSING_INFO,
PelTag::CANON_MEASURED_COLOR,
PelTag::CANON_COLOR_SPACE,
PelTag::CANON_VRD_OFFSET,
PelTag::CANON_SENSOR_INFO,
PelTag::CANON_COLOR_DATA
],
self::CANON_CAMERA_SETTINGS => [
PelTag::CANON_CS_MACRO,
PelTag::CANON_CS_SELF_TIMER,
PelTag::CANON_CS_QUALITY,
PelTag::CANON_CS_FLASH_MODE,
PelTag::CANON_CS_DRIVE_MODE,
PelTag::CANON_CS_FOCUS_MODE,
PelTag::CANON_CS_RECORD_MODE,
PelTag::CANON_CS_IMAGE_SIZE,
PelTag::CANON_CS_EASY_MODE,
PelTag::CANON_CS_DIGITAL_ZOOM,
PelTag::CANON_CS_CONTRAST,
PelTag::CANON_CS_SATURATION,
PelTag::CANON_CS_SHARPNESS,
PelTag::CANON_CS_ISO_SPEED,
PelTag::CANON_CS_METERING_MODE,
PelTag::CANON_CS_FOCUS_TYPE,
PelTag::CANON_CS_AF_POINT,
PelTag::CANON_CS_EXPOSURE_PROGRAM,
PelTag::CANON_CS_LENS_TYPE,
PelTag::CANON_CS_LENS,
PelTag::CANON_CS_SHORT_FOCAL,
PelTag::CANON_CS_FOCAL_UNITS,
PelTag::CANON_CS_MAX_APERTURE,
PelTag::CANON_CS_MIN_APERTURE,
PelTag::CANON_CS_FLASH_ACTIVITY,
PelTag::CANON_CS_FLASH_DETAILS,
PelTag::CANON_CS_FOCUS_CONTINUOUS,
PelTag::CANON_CS_AE_SETTING,
PelTag::CANON_CS_IMAGE_STABILIZATION,
PelTag::CANON_CS_DISPLAY_APERTURE,
PelTag::CANON_CS_ZOOM_SOURCE_WIDTH,
PelTag::CANON_CS_ZOOM_TARGET_WIDTH,
PelTag::CANON_CS_SPOT_METERING_MODE,
PelTag::CANON_CS_PHOTO_EFFECT,
PelTag::CANON_CS_MANUAL_FLASH_OUTPUT,
PelTag::CANON_CS_COLOR_TONE,
PelTag::CANON_CS_SRAW_QUALITY
],
self::CANON_SHOT_INFO => [
PelTag::CANON_SI_ISO_SPEED,
PelTag::CANON_SI_MEASURED_EV,
PelTag::CANON_SI_TARGET_APERTURE,
PelTag::CANON_SI_TARGET_SHUTTER_SPEED,
PelTag::CANON_SI_WHITE_BALANCE,
PelTag::CANON_SI_SLOW_SHUTTER,
PelTag::CANON_SI_SEQUENCE,
PelTag::CANON_SI_AF_POINT_USED,
PelTag::CANON_SI_FLASH_BIAS,
PelTag::CANON_SI_AUTO_EXPOSURE_BRACKETING,
PelTag::CANON_SI_SUBJECT_DISTANCE,
PelTag::CANON_SI_APERTURE_VALUE,
PelTag::CANON_SI_SHUTTER_SPEED_VALUE,
PelTag::CANON_SI_MEASURED_EV2,
PelTag::CANON_SI_CAMERA_TYPE,
PelTag::CANON_SI_AUTO_ROTATE,
PelTag::CANON_SI_ND_FILTER
],
self::CANON_PANORAMA => [
PelTag::CANON_PA_PANORAMA_FRAME,
PelTag::CANON_PA_PANORAMA_DIRECTION
],
self::CANON_PICTURE_INFO => [
PelTag::CANON_PI_IMAGE_WIDTH,
PelTag::CANON_PI_IMAGE_HEIGHT,
PelTag::CANON_PI_IMAGE_WIDTH_AS_SHOT,
PelTag::CANON_PI_IMAGE_HEIGHT_AS_SHOT,
PelTag::CANON_PI_AF_POINTS_USED,
PelTag::CANON_PI_AF_POINTS_USED_20D
],
self::CANON_FILE_INFO => [
PelTag::CANON_FI_FILE_NUMBER,
PelTag::CANON_FI_BRACKET_MODE,
PelTag::CANON_FI_BRACKET_VALUE,
PelTag::CANON_FI_BRACKET_SHOT_NUMBER,
PelTag::CANON_FI_RAW_JPG_QUALITY,
PelTag::CANON_FI_RAW_JPG_SIZE,
PelTag::CANON_FI_NOISE_REDUCTION,
PelTag::CANON_FI_WB_BRACKET_MODE,
PelTag::CANON_FI_WB_BRACKET_VALUE_AB,
PelTag::CANON_FI_WB_BRACKET_VALUE_GM,
PelTag::CANON_FI_FILTER_EFFECT,
PelTag::CANON_FI_TONING_EFFECT,
PelTag::CANON_FI_MACRO_MAGNIFICATION,
PelTag::CANON_FI_LIVE_VIEW_SHOOTING,
PelTag::CANON_FI_FOCUS_DISTANCE_UPPER,
PelTag::CANON_FI_FOCUS_DISTANCE_LOWER,
PelTag::CANON_FI_FLASH_EXPOSURE_LOCK
]
/*
* TODO: Where do these tags belong?
* PelTag::FILL_ORDER,
* PelTag::TRANSFER_RANGE,
* PelTag::JPEG_PROC,
* PelTag::BATTERY_LEVEL,
* PelTag::IPTC_NAA,
* PelTag::INTER_COLOR_PROFILE,
* PelTag::CFA_REPEAT_PATTERN_DIM,
*/
];
/**
* The maker notes held by this directory.
*
* Stores information of the MakerNotes IFD.
* Available and required keys are: parent, data, components and offset
*
* @var array
*/
private $maker_notes = [];
/**
* The entries held by this directory.
*
* Each tag in the directory is represented by a {@link PelEntry}
* object in this array.
*
* @var array
*/
private $entries = [];
/**
* The type of this directory.
*
* Initialized in the constructor. Must be one of {@link IFD0},
* {@link IFD1}, {@link EXIF}, {@link GPS}, or {@link
* INTEROPERABILITY}.
*
* @var int
*/
private $type;
/**
* The next directory.
*
* This will be initialized in the constructor, or be left as null
* if this is the last directory.
*
* @var PelIfd
*/
private $next = null;
/**
* Sub-directories pointed to by this directory.
*
* This will be an array of ({@link PelTag}, {@link PelIfd}) pairs.
*
* @var array
*/
private $sub = [];
/**
* The thumbnail data.
*
* This will be initialized in the constructor, or be left as null
* if there are no thumbnail as part of this directory.
*
* @var PelDataWindow
*/
private $thumb_data = null;
// TODO: use this format to choose between the
// JPEG_INTERCHANGE_FORMAT and STRIP_OFFSETS tags.
// private $thumb_format;
/**
* Construct a new Image File Directory (IFD).
*
* The IFD will be empty, use the {@link addEntry()} method to add
* an {@link PelEntry}. Use the {@link setNext()} method to link
* this IFD to another.
*
* @param integer $type
* the type of this IFD. Must be one of {@link
* IFD0}, {@link IFD1}, {@link EXIF}, {@link GPS}, or {@link
* INTEROPERABILITY}. An {@link PelIfdException} will be thrown
* otherwise.
* @throws PelIfdException
*/
public function __construct($type)
{
if (! array_key_exists($type, self::TYPE_NAMES)) {
throw new PelIfdException('Unknown IFD type: %d', $type);
}
$this->type = $type;
}
/**
* Stores Maker Notes data for an IFD (Probably PelIfd::EXIF only).
*
* @param PelIfd $parent
* the parent PelIfd of the current PelIfd
* @param PelDataWindow $data
* the data window that will provide the data.
* @param PelIfd $parent
* the components in the entry.
* @param integer $offset
* the offset within the window where the directory will
* be found.
*/
public function setMakerNotes($parent, $data, $components, $offset)
{
$this->maker_notes = [
'parent' => $parent,
'data' => $data,
'components' => $components,
'offset' => $offset
];
}
/**
* Returns the Maker Notes data for an IFD (Probably PelIfd::EXIF only).
*
* @return array The maker_notes of IDF
*/
public function getMakerNotes()
{
return $this->maker_notes;
}
/**
* Load data into a Image File Directory (IFD).
*
* @param PelDataWindow $d
* the data window that will provide the data.
* @param integer $offset
* the offset within the window where the directory will
* be found.
* @throws PelException
* @throws PelUnexpectedFormatException
* @throws PelWrongComponentCountException
*/
public function load(PelDataWindow $d, $offset)
{
$starting_offset = $offset;
$thumb_offset = 0;
$thumb_length = 0;
Pel::debug('Constructing IFD at offset %d from %d bytes...', $offset, $d->getSize());
/* Read the number of entries */
$n = $d->getShort($offset);
Pel::debug('Loading %d entries...', $n);
$offset += 2;
/* Check if we have enough data. */
if ($offset + 12 * $n > $d->getSize()) {
$n = floor(($offset - $d->getSize()) / 12);
Pel::maybeThrow(new PelIfdException('Adjusted to: %d.', $n));
}
for ($i = 0; $i < $n; $i ++) {
// TODO: increment window start instead of using offsets.
$tag = $d->getShort($offset + 12 * $i);
Pel::debug('Loading entry with tag 0x%04X: %s (%d of %d)...', $tag, PelTag::getName($this->type, $tag), $i + 1, $n);
switch ($tag) {
case PelTag::EXIF_IFD_POINTER:
case PelTag::GPS_INFO_IFD_POINTER:
case PelTag::INTEROPERABILITY_IFD_POINTER:
case PelTag::MAKER_NOTE:
$o = $d->getLong($offset + 12 * $i + 8);
Pel::debug('Found sub IFD at offset %d', $o);
$components = $d->getLong($offset + 12 * $i + 4);
$ifdType = $this->mapTagToIfdType($d, $offset, $tag, $components, $i, $o);
if ($ifdType === null) {
break;
} elseif ($starting_offset == $o) {
Pel::maybeThrow(new PelIfdException('Bogus offset to next IFD: %d, same as offset being loaded from.', $o));
} else {
$ifd = new PelIfd($ifdType);
try {
$ifd->load($d, $o);
$this->sub[$ifdType] = $ifd;
} catch (PelDataWindowOffsetException $e) {
Pel::maybeThrow(new PelIfdException($e->getMessage()));
}
}
break;
case PelTag::JPEG_INTERCHANGE_FORMAT:
$thumb_offset = $d->getLong($offset + 12 * $i + 8);
$this->safeSetThumbnail($d, $thumb_offset, $thumb_length);
break;
case PelTag::JPEG_INTERCHANGE_FORMAT_LENGTH:
$thumb_length = $d->getLong($offset + 12 * $i + 8);
$this->safeSetThumbnail($d, $thumb_offset, $thumb_length);
break;
default:
$this->loadSingleValue($d, $offset, $i, $tag);
break;
}
}
$this->getOffsetToNextIfd($d, $offset, $n);
$this->checkIfLoadingFinished();
}
/**
* Map tag to IFD type
*
* @param PelDataWindow $d
* @param integer $offset
* @param integer $tag
* @param integer $components
* @param integer $i
* @param integer $o
* @return NULL|mixed|number[][]
*/
private function mapTagToIfdType(PelDataWindow $d, $offset, $tag, $components, $i, $o)
{
$ifdType = null;
/* Map tag to IFD type. */
if ($tag == PelTag::EXIF_IFD_POINTER) {
$ifdType = PelIfd::EXIF;
} elseif ($tag == PelTag::GPS_INFO_IFD_POINTER) {
$ifdType = PelIfd::GPS;
} elseif ($tag == PelTag::INTEROPERABILITY_IFD_POINTER) {
$ifdType = PelIfd::INTEROPERABILITY;
} elseif ($tag == PelTag::MAKER_NOTE) {
// Store maker notes infos, because we need PelTag::MAKE of PelIfd::IFD0 for MakerNotes
// Thus MakerNotes will be loaded at the end of loading PelIfd::IFD0
$this->setMakerNotes($this, $d, $components, $o);
$this->loadSingleValue($d, $offset, $i, $tag);
}
return $ifdType;
}
/**
* Offset to next IFD
*
* @param PelDataWindow $d
* @param integer $offset
* @param float $n
*/
private function getOffsetToNextIfd(PelDataWindow $d, $offset, $n)
{
$o = $d->getLong((int) ($offset + 12 * $n));
Pel::debug('Current offset is %d, link at %d points to %d.', $offset, $offset + 12 * $n, $o);
if ($o > 0) {
/* Sanity check: we need 6 bytes */
if ($o > $d->getSize() - 6) {
Pel::maybeThrow(new PelIfdException('Bogus offset to next IFD: ' . '%d > %d!', $o, $d->getSize() - 6));
} else {
if ($this->type == PelIfd::IFD1) {
// IFD1 shouldn't link further...
Pel::maybeThrow(new PelIfdException('IFD1 links to another IFD!'));
}
$this->next = new PelIfd(PelIfd::IFD1);
$this->next->load($d, $o);
}
} else {
Pel::debug('Last IFD.');
}
}
/**
* Check if we finished loading IFD0 and EXIF IFD is set (EXIF IFD holds the MakerNotes)
*/
private function checkIfLoadingFinished()
{
if ($this->type == PelIfd::IFD0 && isset($this->sub[PelIfd::EXIF])) {
// Get MakerNotes from EXIF IFD and check if they are set
$mk = $this->sub[PelIfd::EXIF]->getMakerNotes();
if (! empty($mk)) {
// get Make tag and load maker notes if tag is valid
$manufacturer = $this->getEntry(PelTag::MAKE);
if ($manufacturer !== null) {
$manufacturer = $manufacturer->getValue();
$mkNotes = PelMakerNotes::createMakerNotesFromManufacturer($manufacturer, $mk['parent'], $mk['data'], $mk['components'], $mk['offset']);
if ($mkNotes !== null) {
// remove pre-loaded undefined MakerNotes
$mk['parent']->offsetUnset(PelTag::MAKER_NOTE);
$mkNotes->load();
}
}
}
}
}
/**
* Load a single value which didn't match any special {@link PelTag}.
*
* This method will add a single value given by the {@link PelDataWindow} and it's offset ($offset) and element counter ($i).
*
* Please note that the data you pass to this method should come
* from an image, that is, it should be raw bytes. If instead you
* want to create an entry for holding, say, an short integer, then
* create a {@link PelEntryShort} object directly and load the data
* into it.
*
* @param PelDataWindow $d
* the data window that will provide the data.
* @param integer $offset
* the offset within the window where the directory will
* be found.
* @param integer $i
* the element's position in the {@link PelDataWindow} $d.
* @param integer $tag
* the tag of the entry as defined in {@link PelTag}.
* @throws PelException
* @throws PelUnexpectedFormatException
* @throws PelWrongComponentCountException
*/
public function loadSingleValue($d, $offset, $i, $tag)
{
$format = $d->getShort($offset + 12 * $i + 2);
$components = $d->getLong($offset + 12 * $i + 4);
$size = PelFormat::getSize($format);
try {
/*
* The data size. If bigger than 4 bytes, the actual data is
* not in the entry but somewhere else, with the offset stored
* in the entry.
*/
$s = $size * $components;
if ($s > 0) {
$doff = $offset + 12 * $i + 8;
if ($s > 4) {
$doff = $d->getLong($doff);
}
$data = $d->getClone($doff, $s);
} else {
$data = new PelDataWindow();
}
$entry = $this->newEntryFromData($tag, $format, $components, $data);
$this->addEntry($entry);
} catch (PelException $e) {
/*
* Throw the exception when running in strict mode, store
* otherwise.
*/
Pel::maybeThrow($e);
}
/* The format of the thumbnail is stored in this tag. */
// TODO: handle TIFF thumbnail.
// if ($tag == PelTag::COMPRESSION) {
// $this->thumb_format = $data->getShort();
// }
}
/**
* Load a single value which didn't match any special {@link PelTag}.
*
* This method will add a single value given by the {@link PelDataWindow} and it's offset ($offset) and element counter ($i).
*
* Please note that the data you pass to this method should come
* from an image, that is, it should be raw bytes. If instead you
* want to create an entry for holding, say, an short integer, then
* create a {@link PelEntryShort} object directly and load the data
* into it.
*
* @param integer $type
* the type of the ifd
* @param PelDataWindow $data
* the data window that will provide the data.
* @param integer $offset
* the offset within the window where the directory will
* be found.
* @param integer $size
* the size in bytes of the maker notes section
* @param integer $i
* the element's position in the {@link PelDataWindow} $data.
* @param integer $format
* the format {@link PelFormat} of the entry.
* @throws PelException
* @throws PelDataWindowWindowException
* @throws PelInvalidArgumentException
*/
public function loadSingleMakerNotesValue($type, PelDataWindow $data, $offset, $size, $i, $format)
{
$elemSize = PelFormat::getSize($format);
if ($size > 0) {
$subdata = $data->getClone($offset + $i * $elemSize, $elemSize);
} else {
$subdata = new PelDataWindow();
}
try {
$entry = $this->newEntryFromData($i + 1, $format, 1, $subdata);
$this->addEntry($entry);
} catch (PelException $e) {
// Throw the exception when running in strict mode, store otherwise.
Pel::maybeThrow($e);
}
/* The format of the thumbnail is stored in this tag. */
// TODO: handle TIFF thumbnail.
// if ($tag == PelTag::COMPRESSION) {
// $this->thumb_format = $data->getShort();
// }
}
/**
* Make a new entry from a bunch of bytes.
*
* This method will create the proper subclass of {@link PelEntry}
* corresponding to the {@link PelTag} and {@link PelFormat} given.
* The entry will be initialized with the data given.
*
* Please note that the data you pass to this method should come
* from an image, that is, it should be raw bytes. If instead you
* want to create an entry for holding, say, an short integer, then
* create a {@link PelEntryShort} object directly and load the data
* into it.
*
* A {@link PelUnexpectedFormatException} is thrown if a mismatch is
* discovered between the tag and format, and likewise a {@link
* PelWrongComponentCountException} is thrown if the number of
* components does not match the requirements of the tag. The
* requirements for a given tag (if any) can be found in the
* documentation for {@link PelTag}.
*
* @param integer $tag
* the tag of the entry as defined in {@link PelTag}.
* @param integer $format
* the format of the entry as defined in {@link PelFormat}.
* @param integer $components
* the components in the entry.
* @param PelDataWindow $data
* the data which will be used to construct the
* entry.
* @return PelEntry a newly created entry, holding the data given.
* @throws PelException
* @throws PelUnexpectedFormatException
* @throws PelWrongComponentCountException
*/
public function newEntryFromData($tag, $format, $components, PelDataWindow $data)
{
/*
* First handle tags for which we have a specific PelEntryXXX
* class.
*/
switch ($this->type) {
case self::IFD0:
case self::IFD1:
case self::EXIF:
case self::INTEROPERABILITY:
switch ($tag) {
case PelTag::DATE_TIME:
case PelTag::DATE_TIME_ORIGINAL:
case PelTag::DATE_TIME_DIGITIZED:
if ($format != PelFormat::ASCII) {
throw new PelUnexpectedFormatException($this->type, $tag, $format, PelFormat::ASCII);
}
if ($components != 20) {
throw new PelWrongComponentCountException($this->type, $tag, $components, 20);
}
// TODO: handle timezones.
return new PelEntryTime($tag, $data->getBytes(0, - 1), PelEntryTime::EXIF_STRING);
case PelTag::COPYRIGHT:
if ($format != PelFormat::ASCII) {
throw new PelUnexpectedFormatException($this->type, $tag, $format, PelFormat::ASCII);
}
$v = explode("\0", trim($data->getBytes(), ' '));
if (! isset($v[1])) {
Pel::maybeThrow(new PelException('Invalid copyright: %s', $data->getBytes()));
// when not in strict mode, set empty copyright and continue
$v[1] = '';
}
return new PelEntryCopyright($v[0], $v[1]);
case PelTag::EXIF_VERSION:
case PelTag::FLASH_PIX_VERSION:
case PelTag::INTEROPERABILITY_VERSION:
if ($format != PelFormat::UNDEFINED) {
throw new PelUnexpectedFormatException($this->type, $tag, $format, PelFormat::UNDEFINED);
}
return new PelEntryVersion($tag, (float) $data->getBytes() / 100);
case PelTag::USER_COMMENT:
if ($format != PelFormat::UNDEFINED) {
throw new PelUnexpectedFormatException($this->type, $tag, $format, PelFormat::UNDEFINED);
}
if ($data->getSize() < 8) {
return new PelEntryUserComment();
} else {
return new PelEntryUserComment($data->getBytes(8), rtrim($data->getBytes(0, 8)));
}
// this point can not be reached
case PelTag::XP_TITLE:
case PelTag::XP_COMMENT:
case PelTag::XP_AUTHOR:
case PelTag::XP_KEYWORDS:
case PelTag::XP_SUBJECT:
if ($format != PelFormat::BYTE) {
throw new PelUnexpectedFormatException($this->type, $tag, $format, PelFormat::BYTE);
}
return new PelEntryWindowsString($tag, $data->getBytes(), true);
}
// This point can be reached! Continue with default.
case self::GPS:
default:
/* Then handle the basic formats. */
switch ($format) {
case PelFormat::BYTE:
$v = new PelEntryByte($tag);
for ($i = 0; $i < $components; $i ++) {
$v->addNumber($data->getByte($i));
}
return $v;
case PelFormat::SBYTE:
$v = new PelEntrySByte($tag);
for ($i = 0; $i < $components; $i ++) {
$v->addNumber($data->getSByte($i));
}
return $v;
case PelFormat::ASCII:
// cut off string after the first nul byte
$canonicalString = strstr($data->getBytes(0), "\0", true);
if ($canonicalString !== false) {
return new PelEntryAscii($tag, $canonicalString);
}
// TODO throw exception if string isn't nul-terminated
return new PelEntryAscii($tag, $data->getBytes(0));
case PelFormat::SHORT:
$v = new PelEntryShort($tag);
for ($i = 0; $i < $components; $i ++) {
$v->addNumber($data->getShort($i * 2));
}
return $v;
case PelFormat::SSHORT:
$v = new PelEntrySShort($tag);
for ($i = 0; $i < $components; $i ++) {
$v->addNumber($data->getSShort($i * 2));
}
return $v;
case PelFormat::LONG:
$v = new PelEntryLong($tag);
for ($i = 0; $i < $components; $i ++) {
$v->addNumber($data->getLong($i * 4));
}
return $v;
case PelFormat::SLONG:
$v = new PelEntrySLong($tag);
for ($i = 0; $i < $components; $i ++) {
$v->addNumber($data->getSLong($i * 4));
}
return $v;
case PelFormat::RATIONAL:
$v = new PelEntryRational($tag);
for ($i = 0; $i < $components; $i ++) {
$v->addNumber($data->getRational($i * 8));