3 A Drupal 8 module providing a file metadata plugin for the EXIF protocol.
8 Uses the [PHP Exif Library](https://github.com/lsolesen/pel) to read/write EXIF
9 information to image files, so bypassing the limitations of the standard PHP
10 Exif extensions which only provides read capabilities.
15 1. __Get EXIF information from a file:__
17 a. Prepare collecting metadata for the file located at a desired URI:
20 $fmdm = \Drupal::service('file_metadata_manager');
21 $my_file_metadata = $fmdm->uri('public::/my_directory/test-exif.jpeg');
25 b. Get the metadata for the metadata _$key_ required. The value returned is an
26 associative array with two keys:
27 - 'text': the string representation of the EXIF tag, suitable for
29 - 'value': the EXIF tag value in PEL internal format.
33 $val = $my_file_metadata->getMetadata('exif', $key);
37 c. EXIF metadata is organized in 'headers' (IFDs) and 'tags'. For this reason,
38 the metadata _$key_ can be specified in the ```getMetadata``` method:
39 - as a string: in this case, it is assumed that a TAG is specified, and the
40 default IFD for that TAG will be used to fetch the information:
41 - as an array: in this case, the first and the second array elements
42 specify respectively the IFD and the TAG requested. IFD and TAG can be
44 The following statements all are equivalent in returning the same
45 information about the 'ApertureValue' TAG in the 'Exif' IFD:
49 $aperture = $my_file_metadata->getMetadata('exif', 'ApertureValue');
50 $aperture = $my_file_metadata->getMetadata('exif', ['Exif', 'ApertureValue']);
51 $aperture = $my_file_metadata->getMetadata('exif', [2, 'ApertureValue']);
52 $aperture = $my_file_metadata->getMetadata('exif', ['Exif', 0x9202]);
53 $aperture = $my_file_metadata->getMetadata('exif', [2, 0x9202]);
57 d. Get a list of IFDs:
61 $my_file_metadata->getSupportedKeys('exif', ['ifds' => TRUE]);
65 e. Get a list of TAGs for a given IFD:
69 $my_file_metadata->getSupportedKeys('exif', ['ifd' => 'GPS']);
73 f. Walk through all possible IFDs/TAGs and build a table with results:
83 foreach ($my_file_metadata->getSupportedKeys('exif', ['ifds' => TRUE]) as $ifd) {
84 $rows[] = [['data' => $ifd[0], 'colspan' => 3]];
85 $keys = $my_file_metadata->getSupportedKeys('exif', ['ifd' => $ifd[0]]);
86 foreach ($keys as $key) {
87 $x = $my_file_metadata->getMetadata('exif', $key);
89 $rows[] = ['data' => [$key[1], $x ? $x['text'] : NULL, $x ? var_export($x['value'], TRUE) : NULL]];
100 2. __Change EXIF information and save to file:__
102 Changing EXIF information and saving it back to the file requires some
103 understanding of how the PEL library manages EXIF entries.
105 a. If you are changing information that is _already_ existing in the source
106 file, then you can use the plugin ```setMetadata``` method, passing the value
107 that the PEL Exif entry expects:
111 $my_file_metadata->setMetadata('exif', 'Orientation', 7);
115 b. If you are _adding_ a TAG that was not existing before, you need to pass a
116 new PEL Exif entry, as expected for that entry. This can also be done as an
117 alternative to change an existing entry:
121 $artist_tag = \Drupal::service('file_mdm_exif.tag_mapper')->resolveKeyToIfdAndTag('Artist');
123 $artist = new PelEntryAscii($artist_tag['tag'], $value);
124 $my_file_metadata->setMetadata('exif', 'Artist', $artist);
128 c. Save changed metadata to file:
132 $my_file_metadata->saveMetadataToFile('exif');