aboutsummaryrefslogtreecommitdiff
path: root/exif.module
blob: ecbbd6e17c80a3fe8511ab7d8559a7bb8361d4ae (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
<?php
// $Id: exif.module,v 1.9.2.18 2010/07/23 17:47:46 rapsli Exp $:

/**
 * @file implementing the drupal api
 */
/**
 * @author: Raphael Schär - www.rapsli.ch
 */

function exif_menu() {
  $items['admin/settings/exif'] = array(
    'title' => 'Exif',
    'page callback' => 'exif_admin_settings',
    'access arguments' => array('administer site configuration'),
    'description' => t('Display available fields'),
    'access callback' => 'user_access',
    'file' => 'exif.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/settings/exif/general'] = array(
    'title' => 'Exif',
    'page callback' => 'exif_admin_settings',
    'access arguments' => array('administer site configuration'),
    'description' => t('Display available fields'),
    'access callback' => 'user_access',
    'file' => 'exif.admin.inc',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/settings/exif/config'] = array(
    'title' => 'Config',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('exif_admin_settings_form'),
    'access arguments' => array('administer site configuration'),
    'description' => t('Some Settings'),
    'access callback' => 'user_access',
    'file' => 'exif.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * implementation of hook_nodeapi
 */
function exif_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {

  if ($teaser) {
    return;
  }

  switch ($op) {

    case 'update':
      //we are only going to update if we have said so
      if (!variable_get('exif_update', TRUE)) {
        break;
      }
    case 'insert':
      if (! _exif_check_for_exif_data($node->type)) {
        return;
      }
      $info = content_types($node->type);
      $fields = $info['fields'];
      $exif = _exif_get_class();

      //get all the fields that will be filled with exif data
      $ar_exif_fields = $exif->getExifFields($fields);

      //get the path to the image
      $image_path = _exif_get_image_path($fields, $node);

      $fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $image_path));
      $file = file_create_path($image_path);

      $data1 = _exif_reformat($exif->readExifTags($file, $ar_exif_fields));
      $data2 = $exif->readIPTCTags($file, $ar_exif_fields);

      if (class_exists('SXMPFiles')) {
        $data3 = $exif->readXMPTags($file, $ar_exif_fields);
        $data = array_merge($data1, $data2, $data3);
      }
      else {
        $data = array_merge($data1, $data2);
      }

      // Loop through every exif enabled field and set its value to the
      // corresponding exif value. If no exif value was found, set the field
      // value to NULL, to avoid strange behaviour in other field modules
      // (date).
      foreach ($ar_exif_fields as $ar_exif_field) {
        $exif_name = $ar_exif_field['section'] .'_'. $ar_exif_field['tag'];
        $exif_value = isset($data[$exif_name]) ? $data[$exif_name] : NULL;

        $field_name = 'field_'. $exif_name;
        if (! $exif_value) {
          if (variable_get('exif_empty_values', TRUE)) {
            $node->{$field_name}[0]['value'] = NULL;
          }
          continue;
        }
        $field = $fields[$field_name];

        // Setup the field value array for delta = 0.
        switch ($exif_name) {
          case 'exif_datetimeoriginal':
          case 'exif_datetimedigitized':
          case 'ifd0_datetime':
            $first_delta = _exif_date_handler($field, $exif_value);
            break;
          default:
            $first_delta = array('value' => $data[$exif_name]);
            break;
        }
        $node->{$field_name}[0] = $first_delta;
      }
      break;
  }
}

/**
 * Date API hook.
 *
 * Make exif a date format in Date API. This makes it possible to alter the
 * format exif dates is parsed as.
 */
function exif_date_format_types() {
  return array('exif' => 'EXIF');
}

/**
 * Date API hook.
 *
 * Make the EXIF date format default for the 'exif' date type.
 */
function exif_date_formats() {
  return array(
  array(
      'type' => 'exif',
      'format' => 'Y:m:d H:i:s',
  ),
  );
}

/**
 * Helper function to handle all date values from exif header. This is
 * designed for the date_api and date modules, but is compatible if these
 * aren't enabled.
 *
 * @param array $field
 *   The field definition for the matcing exif date
 * @param string $exif_date
 *   The date extracted from exif header.
 * @return array
 *   The field value array for delta = 0
 */
function _exif_date_handler($field, $exif_date) {
  if (! module_exists('date_api')) {
    // Don't bother doing anything if the webmaster doesn't ...
    return array('value' => $exif_date);
  }

  require_once drupal_get_path('module', 'date_api') .'/date_api_elements.inc';
  $date_datetime = date_convert_from_custom($exif_date, variable_get('date_format_exif', 'Y:m:d H:i:s'));
  if (! in_array($field['type'], array('date', 'datetime', 'datestamp'))) {
    // Field is not a date field type, so we return a ISO-8601 representation
    return array('value' => date_convert($date_datetime, DATE_DATETIME, DATE_ISO));
  }

  // Exif doesn't handles timezones, so we assume the exif date is in the
  // timezone configured for this date field.  This means the exif date needs
  // to be converted to UTC before it's stored.
  $timezone = date_get_timezone($field['tz_handling']);
  $date = date_make_date($date_datetime, $timezone, DATE_DATETIME, $field['granularity']);

  // Store date offset before converting to UTC as this is lost when setting
  // timezone to 'UTC'.
  $offset = date_offset_get($date);
  date_timezone_set($date, timezone_open('UTC'));

  // Finally, convert the date object in UTC to a date according to the field
  // type: DATE_ISO, DATE_DATETIME or DATE_UNIX.
  $date_field = date_convert($date, DATE_OBJECT, $field['type']);
  return array(
    'value' => $date_field,
    'value2' => $date_field,
    'timezone' => $timezone,
    'offset' => $offset,
    'offset2' => $offset,
  );
}

/**
 * Let's check if this node type contains an image field.
 *
 * @param $fields fields from this content type
 * @return boolean
 */
function _exif_check_for_exif_data($node_type) {

  $new_types = array();
  //fill up array with checked nodetypes
  foreach (variable_get('exif_nodetypes', array()) as $type) {
    if ($type != "0" ) {
      $new_types[] = $type;
    }
  }
  if (in_array($node_type, $new_types)) {
    return TRUE;
  }
  return FALSE;
}

/**
 * From a given node we are going to get the imagepath of the image. if it's an image node
 * it's just going to be images[IMAGE_ORIGINAL]. If it's an imagefield node, we have to go
 * through the fields and look if there is an imagefield and then return the path
 *
 * @param $fields
 * @param $node
 * @return unknown_type
 */
function _exif_get_image_path($fields, &$node) {
  if ($node->type == 'image') {
    return $node->images[IMAGE_ORIGINAL];
  }

  foreach ($fields as $field) {
    if ($field['type'] == 'filefield') {
      $tmp = $node->$field['field_name'];
      return $tmp[0]['filepath'];
    }
  }
  return NULL;
}


/**
 * Helper function to reformat fields where required.
 *
 * Some values (lat/lon) break down into structures, not strings.
 */
function _exif_reformat($data) {
  $date_array = array('datetimeoriginal', 'datetime', 'datetimedigitized');

  // Make the key lowercase as CCK field names must be
  $data = array_change_key_case($data, CASE_LOWER);

  foreach ($data as $key => &$value) {
    if (is_array($value))  {
      $value = array_change_key_case($value, CASE_LOWER);
    }

    // Check for individual keys
    switch ($key) {
      case 'gpslatitude':
        $value = _exif_DMS2D($value, $data['gpslatituderef']);
        break;

      case 'gpslongitude':
        $value = _exif_DMS2D($value, $data['gpslongituderef']);
        break;

      case 'gps_gpslatitude':
        $value = _exif_DMS2D($value, $data['gps_gpslatituderef']);
        break;

      case 'gps_gpslongitude':
        $value = _exif_DMS2D($value, $data['gps_gpslongituderef']);
        break;
         
    }
  }
  return $data;
}

/**
 * Helper function to change GPS co-ords into decimals.
 */
function _exif_DMS2D($value, $ref) {
  $parts = split('/', $value[0]);
  $dec = (float) ((float) $parts[0] /  (float) $parts[1]);

  $parts = split('/', $value[1]);
  $dec += (float) (((float) $parts[0] /  (float) $parts[1]) / 60);

  $parts = split('/', $value[2]);
  $dec += (float) (((float) $parts[0] /  (float) $parts[1]) / 3600);

  if ($ref == 'S' || $ref == 'W') $dec *= -1;
  return $dec;
}

/**
 * Helper function to get the exif class
 * @return Exif
 */
function _exif_get_class() {
  include_once drupal_get_path('module', 'exif') .'/exif.class.php';
  $exif = Exif::getInstance();
  return $exif;
}

/**
 * Implementation of hook_hoken_list
 * @param array $type
 */
function fast_gallery_token_list($type = 'node') {
  if ($type == 'node') {
    $exif = _exif_get_class();
    $ar_iptc = $exif->getHumanReadableIPTCkey();
    foreach ($ar_iptc as $iptc) {
      $tokens['iptc']['iptc_' . $iptc] = 'IPTC Field: ' . $iptc;
    }
    return $tokens;
  }
}

/**
 * implementation of hook_token_values
 * @param unknown_type $type
 * @param unknown_type $object
 * @param unknown_type $options
 */
function fast_gallery_token_values($type, $object = NULL, $options = array()) {
  if ($type == 'node') {
    $node = $object;
    $exif = _exif_get_class();
    $ar_iptc = $exif->getHumanReadableIPTCkey();

    $info = content_types($node->type);
    $fields = $info['fields'];
    //get the path to the image
    $image_path = _exif_get_image_path($fields, $node);
    
    //dsm("start reading");
    $iptc_values = $exif->readIPTCTags($image_path, array(), array('style' => 'fullSmall'));
    //dsm($iptc_values);

    // TODO: needs to be finished
    foreach ($iptc_values as $key => $iptc) {
      $tokens['iptc_' . $key] = 'IPTC Field: ' . utf8_encode($iptc);
    }
    //dsm($tokens);
    return $tokens;
  }
}