aboutsummaryrefslogtreecommitdiff
path: root/exif.class.php
blob: 967066d3ac1dfbab336e72b7c037cc02cc792632 (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
<?php
//$Id:

/**
 *
 * @author Raphael Schär
 * This is a helper class to handle the whole data processing of exif
 *
 */
Class Exif {
  static private $instance = NULL;

  /**
   * We are implementing a singleton pattern
   */
  private function __construct() {
  }

  public function getInstance() {
    if(is_null(self::$instance)) {
      self::$instance = new self;
    }
    return self::$instance;
  }

  /**
   * Going through all the fields that have been created for a given node type
   * and try to figure out which match the naming convention -> so that we know
   * which exif information we have to read
   *
   * Naming convention are: field_exif_xxx (xxx would be the name of the exif
   * tag to read
   *
   * @param $arCckFields array of CCK fields
   * @return array a list of exif tags to read for this image
   */
  public function getExifFields($arCckFields=array()) {
    $arSections = array('exif', 'file', 'computed', 'ifd0', 'gps', 'winxp', 'iptc');
    $arExif = array();

    foreach ($arCckFields as $field) {
      $ar = explode("_", $field['field_name']);
      if ($ar[0] == 'field' && isset($ar[1]) && in_array($ar[1], $arSections)) {
        unset($ar[0]);
        $section = $ar[1];
        unset($ar[1]);
        $arExif[] = array('section'=>$section, 'tag'=>implode("_", $ar));
      }
    }
    return $arExif;
  }

  /**
   * Read the Information from a picture according to the fields specified in CCK
   * @param $file
   * @param $arTagNames
   * @return array
   */
  public function readExifTags($file, $arTagNames=array()) {
    if (!file_exists($file)) {
      return array();
    }

    $ar_supported_types = array('jpg', 'jpeg');
    if (!in_array(strtolower($this->getFileType($file)), $ar_supported_types)) {
      return array();
    }
    $exif = exif_read_data($file, 0);
    $arSmallExif = array();
    foreach ((array)$exif as $key => $value) {
      $arSmallExif[strtolower($key)] = $value;
    }
    $info = array();

    foreach ($arTagNames as $tagName) {
      if ($tagName['section'] != 'iptc' && !empty($arSmallExif[$tagName['tag']])) {
        $info[$tagName['section'] .'_'. $tagName['tag']] = $arSmallExif[$tagName['tag']];
      }
    }
    return $info;
  }

  private function getFileType($file) {
    $ar = explode('.', $file);
    $ending = $ar[count($ar)-1];
    return $ending;
  }

  public function readIPTCTags($file, $arTagNames=array()) {
    $humanReadableKey = $this->getHumanReadableIPTCkey();
    $size = GetImageSize ($file, $infoImage);
    $iptc = empty($infoImage["APP13"]) ? array() : iptcparse($infoImage["APP13"]);
    $arSmallIPTC = array();
    if (is_array($iptc)) {
      foreach($iptc as $key => $value)
      {
        $resultTag = "";
        foreach($value as $innerkey => $innervalue)
        {
          if( ($innerkey+1) != count($value) ) {
             $resultTag .= $innervalue . ", ";     
          }
          else {
            $resultTag .= $innervalue;
          }
        }
        $arSmallIPTC[$humanReadableKey[$key]] = $resultTag;
      }
    }
    $info = array();
    foreach ($arTagNames as $tagName) {
      if ($tagName['section'] == "iptc") {
        $info[$tagName['section'] .'_'. $tagName['tag']] = utf8_encode($arSmallIPTC[$tagName['tag']]);
      }
    }
    return $info;
  }

  /**
   * Just some little helper function to get the iptc fields
   * @return array
   *
   */
  public function getHumanReadableIPTCkey() {
    return array(
      "2#202" => "object_data_preview_data",
      "2#201" => "object_data_preview_file_format_version",
      "2#200" => "object_data_preview_file_format",
      "2#154" => "audio_outcue",
      "2#153" => "audio_duration",
      "2#152" => "audio_sampling_resolution",
      "2#151" => "audio_sampling_rate",
      "2#150" => "audio_type",
      "2#135" => "language_identifier",
      "2#131" => "image_orientation",
      "2#130" => "image_type",
      "2#125" => "rasterized_caption",    
      "2#122" => "writer",
      "2#120" => "caption",
      "2#118" => "contact",
      "2#116" => "copyright_notice",
      "2#115" => "source",
      "2#110" => "credit",
      "2#105" => "headline",
      "2#103" => "original_transmission_reference",
      "2#101" => "country_name",
      "2#100" => "country_code",
      "2#095" => "state",
      "2#092" => "sublocation",
      "2#090" => "city",
      "2#085" => "by_line_title",
      "2#080" => "by_line",
      "2#075" => "object_cycle",
      "2#070" => "program_version",
      "2#065" => "originating_program",
      "2#063" => "digital_creation_time",
      "2#062" => "digital_creation_date",   
      "2#060" => "creation_time",
      "2#055" => "creation_date",
      "2#050" => "reference_number",
      "2#047" => "reference_date",
      "2#045" => "reference_service",
      "2#042" => "action_advised",
      "2#040" => "special_instruction",
      "2#038" => "expiration_time",
      "2#037" => "expiration_date",
      "2#035" => "release_time",
      "2#030" => "release_date",
      "2#027" => "content_location_name",
      "2#026" => "content_location_code",
      "2#025" => "keywords",
      "2#022" => "fixture_identifier",
      "2#020" => "supplemental_category", 
      "2#015" => "category",
      "2#010" => "subject_reference", 
      "2#010" => "urgency",
      "2#008" => "editorial_update",
      "2#007" => "edit_status",
      "2#005" => "object_name",
      "2#004" => "object_attribute_reference",
      "2#003" => "object_type_reference",
      "2#000" => "record_version"
      );
  }

  /**
   * WMP fields mapper. As we're dealing with a mapper between RDF
   * elements and CCK fields, we have to define custom keys that
   * both on the field name and the namespace used.
   *
   * And, as the WMP specs also defines some datatypes like properties,
   * arrays and structures, we have to deal with those as well.
   *
   * @return array
   *   Mapping between CCK and WMP fields.
   */
  public function getWMPFields() {
    return array(
      'headline'            => array(
        'name'              => 'Headline',
        'ns'                => 'photoshop',
        'type'              => 'property',
      ),
      'authorsposition'     => array(
        'name'              => 'AuthorsPosition',
        'ns'                => 'photoshop',
        'type'              => 'property',
        ),
      'instructions'        => array(
        'name'              => 'Instructions',
        'ns'                => 'photoshop',
        'type'              => 'property',
        ),
      'subject'             => array(
        'name'              => 'subject',
        'ns'                => 'dc',
        'type'              => 'array',
        ),
      'description'         => array(
        'name'              => 'description',
        'ns'                => 'dc',
        'type'              => 'array',
        ),
      'creator'             => array(
        'name'              => 'creator',
        'ns'                => 'dc',
        'type'              => 'array',
        ),
      'rights'              => array(
        'name'              => 'rights',
        'ns'                => 'dc',
        'type'              => 'array',
        ),
      'ciadrextadr'         => array(
        'name'              => 'CiAdrExtadr',
        'ns'                => 'Iptc4XmpCore',
        'type'              => 'struct',
        ),        
      'ciemailwork'         => array(
        'name'              => 'CiEmailWork',
        'ns'                => 'Iptc4XmpCore',
        'type'              => 'struct',
        ),        
      'ciurlwork'           => array(
        'name'              => 'CiUrlWork',
        'ns'                => 'Iptc4XmpCore',
        'type'              => 'struct',
        ),        
      'scene'               => array(
        'name'              => 'Scene',
        'ns'                => 'Iptc4XmpCore',
        'type'              => 'array',
        ),        
      'hierarchicalsubject' => array(
        'name'              => 'hierarchicalSubject',
        'ns'                => 'lr',
        'type'              => 'array',
        ),        
      'location'            => array(
        'name'              => 'Location',
        'ns'                => 'Iptc4XmpCore',
        'type'              => 'property',
        ),        
    );
  }

  /**
   * Read WMP data from an image file.
   */
  public function readWMPTags($file, $arTagNames=array()) {
  }
}