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
|
<?php
// $Id: exif.module,v 1.9.2.13 2010/03/19 22:17:00 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',
'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',
'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',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* The form definition for the admin settings
* @return array
* form definition
*/
function exif_admin_settings_form() {
$forms = array();
/*$forms['exif_granularity'] = array(
'#type' => 'select',
'#title' => t('Granularity'),
'#options' => array(0 => t('Default'), 1 => ('Day')),
'#default_value' => variable_get('exif_granularity', 0),
'#description' => t('If a timestamp is select (for example the date the picture was taken), you can specify here how granular the timestamp should be. If you select default it will just take whatever is available in the picture. If you select Day, the Date saved will look something like 13-12-2008. This can be useful if you want to use some kind of grouping on the data.'),
);*/
$all_nodetypes = node_get_types();
$all_nt = array();
foreach ($all_nodetypes as $item) {
$all_nt[$item->type] = $item->name;
}
$forms['exif_nodetypes'] = array(
'#type' => 'checkboxes',
'#title' => t('Nodetypes'),
'#options' => $all_nt,
'#default_value' => variable_get('exif_nodetypes', array()),
'#description' => t('Select nodetypes which should be checked for exif data. Incase the nodetypes contains more than one filefield, make sure that the imagefield is the first one!!!!'),
);
$forms['exif_update'] = array(
'#type' => 'checkbox',
'#title' => t('Refresh on node update'),
'#default_value' => variable_get('exif_update', TRUE),
'#description' => t('If you enable this option, Exif data is being updated when the node is being updated.'),
);
return system_settings_form($forms);
}
/**
* 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);
$data3 = $exif->readXMPTags($file, $ar_exif_fields);
$data = array_merge($data1, $data2, $data3);
// 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) {
$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;
}
/**
* Just some help page. Gives you an overview over the available tags
* @return string html
*/
function exif_admin_settings() {
drupal_add_css(drupal_get_path('module', 'exif') .'/exif.css');
$filepath = drupal_get_path('module', 'exif') .'/sample.jpg';
$exif = _exif_get_class();
$ar_exif = read_exif_data($filepath, 0, TRUE);
// CCK field names must be lowercase
$ar_exif = array_change_key_case($ar_exif, CASE_LOWER);
$out = t('This is an overview over the most common data that is extracted with the exif module. How to read this table: The grey table header would be the key identifier and the following attributes would be the field to read.');
$out .= ' ' . t('For example: If you want to import datetimeoriginal into an CCK field, you would name the CCK field field_exif_datetimeoriginal. Since this is a date field you can use a datetime field.');
$rows1 = array();
$help = t('This would be the keyword for your CCK field.');
foreach ($ar_exif as $key => $value) {
if (is_array($value)) {
$value = _exif_reformat($value);
$rows1[] = array('data' => array($key, $help), 'class' => 'tag_type');
foreach ($value as $key2 => $value2) {
$rows1[] = array('data' => array($key2, check_plain(utf8_encode($value2))));
}
}
}
$human_readable_key = $exif->getHumanReadableIPTCkey();
$size = GetImageSize($filepath, $info_image);
$iptc = iptcparse($info_image["APP13"]);
$rows2 = array();
$help = t('This would be the keyword for your CCK field.');
if (is_array($iptc)) {
$rows2[] = array('data' => array('IPTC', $help), 'class' => 'tag_type');
foreach ($iptc as $key => $value) {
$result_tag = "";
foreach ($value as $innerkey => $innervalue) {
if ( ($innerkey+1) != count($value) ) {
$result_tag .= $innervalue .", ";
}
else {
$result_tag .= $innervalue;
}
}
$rows2[] = array('data' => array($human_readable_key[$key], check_plain(utf8_encode($result_tag))));
}
}
$rows = array_merge($rows1, $rows2);
$header = array(t('Key'), t('Value'));
$out .= theme('table', $header, $rows, array('id' => 'exif-fields'));
// TODO Prevent binary data values from busting the page layout
return $out;
}
/**
* 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);
}
if ($key == 'gps_latitude') {
$value = _exif_DMS2D($value, $data['gps_gpslatituderef']);
}
elseif ($key == 'gps_longitude') {
$value = _exif_DMS2D($value, $data['gps_gpslongituderef']);
}
}
return $data;
}
/**
* Helper function to change GPS co-ords into decimals.
*/
function _exif_DMS2D($value, $ref) {
$parts = split('/', $value[0]);
$dec = $parts[0] / $parts[1];
$parts = split('/', $value[1]);
$dec += ($parts[0] / $parts[1]) / 60;
$parts = split('/', $value[2]);
$dec += ($parts[0] / $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;
}
|