aboutsummaryrefslogtreecommitdiff
path: root/exif.module
blob: 2f41afb316cb112472b36dcbd22d898bb7190eea (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
<?php
// $Id: exif.module,v 1.9 2008/04/05 23:22:05 davidlesieur Exp $

/**
 * Implementation of hook_menu().
 */
function exif_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/exif',
      'title' => t('Exif'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('exif_admin_settings_form'),
      'access' => user_access('administer site configuration'),
      'description' => t('Configure what Exif tags to display.'),
    );
  }

  return $items;
}

/**
 * Implementation of hook_nodeapi().
 */
function exif_nodeapi(&$node, $op, $teaser) {
  if ($teaser || $node->type != 'image') {
    return;
  }

  switch ($op) {
    case 'insert':
    case 'update':
      $fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $node->images[IMAGE_ORIGINAL]));
      $file = file_create_path($node->images[IMAGE_ORIGINAL]);
      $data = _exif_read_exif($file);
      db_query('DELETE FROM {exif} WHERE fid = %d', $fid);

      // Cache data in db.
      foreach ($data as $ifd => $tags) {
        foreach ($tags as $tag => $value) {
          db_query("INSERT INTO {exif} (fid, ifd, tag, value) VALUES (%d, %d, %d, '%s')",
            $fid, $ifd, $tag, $value);
        }
      }
<<<<<<< exif.module
    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);
=======
      break;
>>>>>>> 1.9

    case 'load':
      $fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $node->images[IMAGE_ORIGINAL]));
      return  array('exif_data' => _exif_get_exif($fid));
    case 'view':
      $node->content['exif'] = array(
        '#value' => theme('exif_table', $node),
        '#weight' => 10,
      );
      break;
  }
}

/**
 * Administration page callback.
 */
function exif_admin_settings_form() {
  _exif_bootstrap();
  $tags = exif_load_settings();
  foreach ($tags as $tag) {
    $form['tags']["{$tag->ifd}_{$tag->tag}"]['type'] = array(
      '#type' => 'markup',
      '#value' => PelIfd::getTypeName($tag->ifd),
    );
    $form['tags']["{$tag->ifd}_{$tag->tag}"]['ifd'] = array(
      '#type' => 'hidden',
      '#value' => $tag->ifd,
    );
    $form['tags']["{$tag->ifd}_{$tag->tag}"]['tag'] = array(
      '#type' => 'hidden',
      '#value' => $tag->tag,
    );
    $form['tags']["{$tag->ifd}_{$tag->tag}"]['status'] = array(
      '#type' => 'checkbox',
      '#title' => utf8_encode(PelTag::getTitle($tag->ifd, $tag->tag)),
      '#default_value' => $tag->status,
    );
    $form['tags']["{$tag->ifd}_{$tag->tag}"]['weight'] = array(
      '#type' => 'weight',
      '#delta' => 10,
      '#default_value' => $tag->weight,
    );
    $form['tags']["{$tag->ifd}_{$tag->tag}"]['#tree'] = TRUE;
    $form['tags']["{$tag->ifd}_{$tag->tag}"]['#weight'] = $tag->weight;
  }
  $form['buttons']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration')
  );
  $form['buttons']['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset to defaults')
  );

  return $form;
}

function exif_admin_settings_form_submit($form_id, $values) {
  $op = isset($_POST['op']) ? $_POST['op'] : '';

  if ($op == t('Reset to defaults')) {
    exif_reset_settings();
    drupal_set_message(t('The configuration options have been reset to their default values.'));
  }
  elseif ($op == t('Save configuration')) {
    exif_save_settings($values);
    drupal_set_message(t('The configuration options have been saved.'));
  }
}

/**
 * Fetch exif data.
 */
function _exif_get_exif($fid) {
  $data = array();
  if ($result = db_query('SELECT * FROM {exif} WHERE fid = %d', $fid)) {
    while ($row = db_fetch_object($result)) {
      $data[$row->ifd][$row->tag] = $row->value;
    }
  }

  return $data;
}

/**
 * Reads exif data from a file.
 */
function _exif_read_exif($file) {
  _exif_bootstrap();
  $data = array();
  if (!file_exists($file)) {
    watchdog('exif', t('Image %file not found.', array('%file' => $file)), WATCHDOG_WARNING);
    return $exif;
  }
  if (exif_imagetype($file) != IMAGETYPE_JPEG) {
    return $data;
  }
  $jpeg = new PelJpeg($file);
  $exif = $jpeg->getExif();
  if (!$exif) {
    return $data;
  }
  $tiff = $exif->getTiff();
  if (!$tiff) {
    return $data;
  }
  $ifd0 = $tiff->getIfd();
  if (!$ifd0) {
    return $data;
  }
  $ifds[PelIfd::IFD0] = $ifd0;
  if ($exif = $ifd0->getSubIfd(PelIfd::EXIF)) {
    $ifds[PelIfd::EXIF] = $exif;
  }
  if ($gps = $ifd0->getSubIfd(PelIfd::GPS)) {
    $ifds[PelIfd::GPS] = $gps;
  }
  $tags = exif_get_enabled_tags();
  $data = array();
  foreach ($tags as $tag) {
    $entry = $ifds[$tag->ifd]->getEntry($tag->tag);
    if ($entry) {
      $row = array();
      switch($tag->tag) {
        case PelTag::DATE_TIME:
        case PelTag::DATE_TIME_ORIGINAL:
        case PelTag::DATE_TIME_DIGITIZED:
          // Return a unixtimestamp. Theme will handle date formating.
          $data[$tag->ifd][$tag->tag] = $entry->getValue();
          break;

        default:
          $data[$tag->ifd][$tag->tag] = utf8_encode($entry->getText());
      }
    }
  }
  return $data;
}

/**
 * Return an array containing only the tags that were enabled.
 */
function exif_get_enabled_tags() {
  static $tags = array();

  if (!count($tags)) {
    $result = db_query('SELECT * FROM {exif_tags} WHERE status = 1');
    while ($tag = db_fetch_object($result)) {
      $tags[] = $tag;
    }
    if (!count($tags)) {
      // Table is empty, get some defaults
      $tags = exif_get_default_settings();
      foreach ($tags as $key => $tag) {
        if (!$tag->status) {
          unset($tags[$key]);
        }
      }
    }
    usort($tags, '_exif_compare_tags');
  }

  return $tags;
}

/**
 * Return an array with all the valid tags and their settings.
 */
function exif_load_settings() {
  $tags = exif_get_default_settings();

  $result = db_query('SELECT * FROM {exif_tags}');
  while ($tag = db_fetch_object($result)) {
    $tags["{$tag->ifd}_{$tag->tag}"] = $tag;
  }
  usort($tags, '_exif_compare_tags');

  return $tags;
}

function exif_save_settings($values) {
  db_lock_table('exif_tags');
  foreach ($values as $tag) {
    if (!is_array($tag) || !isset($tag['ifd'])) {
      continue; // Save only appropriate form values
    }
    db_query('DELETE FROM {exif_tags} WHERE ifd = %d AND tag = %d', $tag['ifd'], $tag['tag']);
    db_query('INSERT INTO {exif_tags} (ifd, tag, status, weight) VALUES (%d, %d, %d, %d)', $tag['ifd'], $tag['tag'], $tag['status'], $tag['weight']);
  }
  db_unlock_tables();
}

function exif_reset_settings() {
  db_query('DELETE FROM {exif_tags}');
}

/**
 * Return an array with all the valid tags, with some useful default settings.
 */
function exif_get_default_settings() {
  $tags = exif_get_valid_tags();
  $tags[PelIfd::EXIF .'_'. PelTag::DATE_TIME_ORIGINAL]->status = 1;
  $tags[PelIfd::EXIF .'_'. PelTag::DATE_TIME_ORIGINAL]->weight = -10;
  $tags[PelIfd::IFD0 .'_'. PelTag::MODEL]->status = 1;
  $tags[PelIfd::IFD0 .'_'. PelTag::MODEL]->weight = -8;
  $tags[PelIfd::EXIF .'_'. PelTag::FOCAL_LENGTH]->status = 1;
  $tags[PelIfd::EXIF .'_'. PelTag::FOCAL_LENGTH]->weight = -6;
  $tags[PelIfd::EXIF .'_'. PelTag::APERTURE_VALUE]->status = 1;
  $tags[PelIfd::EXIF .'_'. PelTag::APERTURE_VALUE]->weight = -4;
  $tags[PelIfd::EXIF .'_'. PelTag::EXPOSURE_TIME]->status = 1;
  $tags[PelIfd::EXIF .'_'. PelTag::EXPOSURE_TIME]->weight = -2;
  $tags[PelIfd::EXIF .'_'. PelTag::ISO_SPEED_RATINGS]->status = 1;
  $tags[PelIfd::EXIF .'_'. PelTag::ISO_SPEED_RATINGS]->weight = 0;
  return $tags;
}

/**
 * Helper function to return all the valid tags (well, at least those this module cares about).
 *
 * For convenience, each tag has a key in the form: "ifd_tag", where ifd and tag
 * are standard Exif ids. Those ids can be found in PelIfd.php and PelTag.php.
 */
function exif_get_valid_tags() {
  $valid_tags = array();
  $valid_tags = array_merge($valid_tags, _exif_get_valid_ifd_tags(PelIfd::IFD0, new PelIfd(PelIfd::IFD0)));
  $valid_tags = array_merge($valid_tags, _exif_get_valid_ifd_tags(PelIfd::EXIF, new PelIfd(PelIfd::EXIF)));
  $valid_tags = array_merge($valid_tags, _exif_get_valid_ifd_tags(PelIfd::GPS, new PelIfd(PelIfd::GPS)));
  return $valid_tags;
}

function _exif_get_valid_ifd_tags($ifd_id, $ifd) {
  $tags = array();
  $pel_tags = $ifd->getValidTags();
  foreach ($pel_tags as $pel_tag) {
    $tag = new StdClass();
    $tag->ifd = $ifd_id;
    $tag->tag = $pel_tag;
    $tags["{$tag->ifd}_{$tag->tag}"] = $tag;
  }
  return $tags;
}

function _exif_compare_tags($a, $b) {
  $status = $b->status - $a->status;
  // Separate enabled from disabled.
  if ($status) {
    return $status;
  }
  // Enabled tags
  if ($a->status) {
    $weight = $a->weight - $b->weight;
    if ($weight) {
      return $weight;
    }
    return $a->ifd - $b->ifd;
  }
  // Disabled tags
  else {
    return $a->ifd - $b->ifd;
  }
}

function theme_exif_table($node) {
  _exif_bootstrap();
  // Retrieve the desired tag values from the file
  $tags = exif_get_enabled_tags();
  $rows = array();
  $data = $node->exif_data;

  foreach ($tags as $tag) {
    if (!empty($data[$tag->ifd][$tag->tag])) {
      $row = array();
      $row[] = array('data' => check_plain(utf8_encode(PelTag::getTitle($tag->ifd, $tag->tag))), 'class' => 'exif-title');
      $val = $data[$tag->ifd][$tag->tag];
      switch($tag->tag) {
        case PelTag::DATE_TIME:
        case PelTag::DATE_TIME_ORIGINAL:
        case PelTag::DATE_TIME_DIGITIZED:
          // Use Drupal's date formatting instead of Pel's
          $row[] = array('data' => format_date($val, 'medium', '', 0), 'class' => 'exif-value');
          break;

        default:
          $row[] = array('data' => check_plain($val), 'class' => 'exif-value');
      }
      $rows[] = $row;
    }
  }
<<<<<<< exif.module
}
=======

  if (empty($rows)) {
    return '';
  }
  $header = array(array('data' => t('Image information'), 'colspan' => 2));
  return theme('table', $header, $rows, array('class' => 'exif'));
}

function theme_exif_admin_settings_form($form) {
  $header = array(t('Tag'), t('Weight'), t('Type'));
  $rows = array();
  foreach (element_children($form['tags']) as $key) {
    $row = array();
    $row[] = drupal_render($form['tags'][$key]['status']);
    $row[] = drupal_render($form['tags'][$key]['weight']);
    $row[] = drupal_render($form['tags'][$key]['type']);
    $rows[] = $row;
  }
  $output .= theme('table', $header, $rows, array('class' => 'admin-settings-exif'));
  $output .= drupal_render($form);
  return $output;
}

function _exif_bootstrap() {
  include_once drupal_get_path('module', 'exif') .'/pel/PelJpeg.php';
}

>>>>>>> 1.9