-
Notifications
You must be signed in to change notification settings - Fork 40
/
islandora_openseadragon.module
301 lines (284 loc) · 9.97 KB
/
islandora_openseadragon.module
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
<?php
/**
* @file
* Hooks and callbacks for this module.
*/
/**
* Implements hook_menu().
*/
function islandora_openseadragon_menu() {
return array(
'admin/islandora/islandora_viewers/openseadragon' => array(
'title' => 'OpenSeadragon',
'description' => 'Configure the OpenSeadragon viewer.',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('islandora_openseadragon_admin'),
'file' => 'includes/admin.form.inc',
'type' => MENU_NORMAL_ITEM,
),
'islandora/object/%islandora_object/download_clip' => array(
'page callback' => 'islandora_openseadragon_download_clip',
'page arguments' => array(2),
'type' => MENU_CALLBACK,
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 2),
'load arguments' => array(2),
),
);
}
/**
* Implements hook_theme().
*/
function islandora_openseadragon_theme() {
return array(
'islandora_openseadragon_viewer' => array(
'variables' => array(
'uri' => '',
'fedora_object' => NULL,
),
'template' => 'theme/islandora-openseadragon',
),
// A link for clipping JP2 datastreams via djatoka.
'islandora_openseadragon_clipper' => array(
'variables' => array(
'pid' => NULL,
),
),
);
}
/**
* Implements hook_islandora_viewer_info().
*/
function islandora_openseadragon_islandora_viewer_info() {
return array(
// @XXX The element returned has a width:100% but no natural flow width; it
// is completely dependent on it's parent.
'islandora_openseadragon' => array(
'label' => t('OpenSeadragon'),
// Later we'll add DZI too.
'description' => t('OpenSeadragon viewer with djatoka as tilesource.'),
'configuration' => 'admin/islandora/islandora_viewers/openseadragon',
'callback' => 'islandora_openseadragon_callback',
// DZI has xml as mimetype? Not sure how to handle that.
'mimetype' => array('image/jp2'),
),
);
}
/**
* Callback function for the viewer.
*
* @param array $params
* Params required by the theme. We require the keys:
* - jp2_url: The URL to the JP2 image.
* @param IslandoraAbstractObject $fedora_object
* The object that we are viewing.
*
* @return string
* HTML repersentation of the OpenSeadragon viewer
*/
function islandora_openseadragon_callback($params = NULL, $fedora_object = NULL) {
if (isset($params['jp2_url'])) {
$uri = $params['jp2_url'];
return theme('islandora_openseadragon_viewer', array('uri' => $uri, 'fedora_object' => $fedora_object));
}
}
/**
* Implements template_preprocess_HOOK().
*/
function template_preprocess_islandora_openseadragon_viewer(&$variables) {
// Variable fedora_object included in results, but not used.
$library_path = libraries_get_path('openseadragon');
$module_path = drupal_get_path('module', 'islandora_openseadragon');
$variables['viewer_id'] = 'islandora-openseadragon';
$settings = array(
'id' => $variables['viewer_id'],
'prefixUrl' => file_create_url("$library_path/images/"),
);
module_load_include('inc', 'islandora_openseadragon', 'includes/utilities');
$installed_version = islandora_openseadragon_get_installed_version();
if (version_compare($installed_version, '0.9.82') <= 0) {
// XXX: Version numbers don't quite line-up for the change in the nav image
// prefix URL, so now, let the hacking begin!... Just for compatibility
// with older versions.
$types = array('zoomIn', 'zoomOut', 'home', 'fullpage', 'previous', 'next');
$actions = array(
'REST' => 'rest',
'GROUP' => 'grouphover',
'HOVER' => 'hover',
'DOWN' => 'pressed',
);
foreach ($types as $type) {
foreach ($actions as $action => $namepart) {
$settings['navImages'][$type][$action] = format_string('!type_!action.png', array(
'!type' => strtolower($type),
'!action' => $namepart,
));
}
}
}
$settings = array_merge($settings, islandora_openseadragon_get_settings());
foreach ($settings as $key => $val) {
if (filter_var($val, FILTER_VALIDATE_FLOAT)) {
$settings[$key] = (float) $val;
}
elseif (filter_var($val, FILTER_VALIDATE_INT)) {
$settings[$key] = (int) $val;
}
elseif (filter_var($val, FILTER_VALIDATE_URL)) {
$settings[$key] = check_url($val);
}
}
// This is where we jam in some HOCR coordinates.
if (isset($variables['fedora_object']) && isset($_GET['solr'])) {
$params = $_GET['solr']['params'];
$params['fq'][] = format_string('PID:"!pid"', array(
'!pid' => $variables['fedora_object']->id,
));
module_load_include('inc', 'islandora_paged_content', 'includes/highlighting');
$highlighting_info = islandora_paged_content_perform_solr_highlighting_query($_GET['solr']['query'], $params);
$highlights = array();
foreach ($highlighting_info as $pid => $info) {
if (array_key_exists('snippets', $info)) {
foreach ($info['snippets'] as $snippet => $bounding_boxes) {
foreach ($bounding_boxes as $bounding_box) {
// The size on the x axis is used to calculate the proportional
// values under the openseadragon coordinate system.
$highlights[] = array(
'x' => $bounding_box['left'] / $info['page']['width'],
'y' => $bounding_box['top'] / $info['page']['width'],
'width' => ($bounding_box['right'] - $bounding_box['left']) / $info['page']['width'],
'height' => ($bounding_box['bottom'] - $bounding_box['top']) / $info['page']['width'],
'className' => "islandora-openseadragon-highlight",
);
}
}
}
}
$settings['overlays'] = $highlights;
}
drupal_add_js(array(
'islandoraOpenSeadragon' => array(
'pid' => isset($variables['fedora_object']) ? $variables['fedora_object']->id : NULL,
'resourceUri' => $variables['uri'],
'tileSize' => (int) variable_get('islandora_openseadragon_tile_size', '256'),
'tileOverlap' => (int) variable_get('islandora_openseadragon_tile_overlap', '0'),
'fitToAspectRatio' => variable_get('islandora_openseadragon_fit_to_aspect_ratio', FALSE),
'settings' => $settings,
),
), 'setting');
drupal_add_js("$library_path/openseadragon.js", array('weight' => -4));
drupal_add_js("$module_path/js/islandora_openseadragon.js", array('weight' => -3));
drupal_add_js("$module_path/js/djtilesource.js", array('weight' => -2));
drupal_add_css("$module_path/css/islandora_openseadragon.theme.css");
}
/**
* Get OpenSeadragon settings.
*
* @return array
* Configuration options for OpenSeadragon.
*/
function islandora_openseadragon_get_settings() {
return variable_get('islandora_openseadragon_settings', array()) + array(
'debugMode' => FALSE,
'djatokaServerBaseURL' => '/adore-djatoka/resolver',
'animationTime' => '1.5',
'blendTime' => '0.1',
'alwaysBlend' => FALSE,
'autoHideControls' => TRUE,
'immediateRender' => FALSE,
'wrapHorizontal' => FALSE,
'wrapVertical' => FALSE,
'wrapOverlays' => FALSE,
'panHorizontal' => TRUE,
'panVertical' => TRUE,
'minZoomImageRatio' => '0.8',
'maxZoomPixelRatio' => '2',
'visibilityRatio' => '0.5',
'springStiffness' => '5.0',
'imageLoaderLimit' => '5',
'clickTimeThreshold' => '300',
'clickDistThreshold' => '5',
'zoomPerClick' => '2.0',
'zoomPerScroll' => '1.2',
'zoomPerSecond' => '2.0',
'showNavigator' => TRUE,
'defaultZoomLevel' => '1.0',
);
}
/**
* Implements hook_process_theme().
*/
function islandora_openseadragon_preprocess_islandora_object_print(array &$variables) {
if (isset($_GET['clip'])) {
module_load_include('inc', 'islandora_openseadragon', 'includes/utilities');
$clip_parts = islandora_openseadragon_construct_clip_url($_GET['clip']);
if ($clip_parts) {
$variables['clip'] = $clip_parts['original_params'];
$variables['content']['clip'] = array(
'#weight' => 0,
'#prefix' => "<div id='clip'>",
'#markup' => theme_image(array(
'path' => $clip_parts['djatoka_image_url'], 'attributes' => $clip_parts['dimensions'])),
'#suffix' => '</div>',
);
}
else {
drupal_set_message(t('Invalid clip parameters passed.'), 'error');
}
}
}
/**
* Theme function to create a clipper link.
*/
function theme_islandora_openseadragon_clipper(&$variables) {
$image = theme(
'image',
array(
'path' => drupal_get_path('module', 'islandora_openseadragon') . '/images/clip_icon.png',
)
);
return l(
$image,
"islandora/object/{$variables['pid']}/print",
array(
'attributes' => array(
'title' => t('Clip Image'),
'id' => 'clip',
),
'html' => TRUE,
)
);
}
/**
* Menu callback downloads the given clip.
*
* @param AbstractObject $object
* An AbstractObject representing an object within Fedora.
*/
function islandora_openseadragon_download_clip(AbstractObject $object) {
if (isset($_GET['clip'])) {
module_load_include('inc', 'islandora_openseadragon', 'includes/utilities');
$clip_parts = islandora_openseadragon_construct_clip_url($_GET['clip'], TRUE);
if ($clip_parts) {
$filename = $object->label;
header("Content-Disposition: attachment; filename=\"{$filename}.jpg\"");
header("Content-type: image/jpeg");
header("Content-Transfer-Encoding: binary");
$ch = curl_init();
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $clip_parts['djatoka_image_url']);
curl_exec($ch);
curl_close($ch);
}
else {
drupal_access_denied();
watchdog('islandora_openseadragon', 'Invalid parameters specified for downloading of a clip for @pid. Parameters attempted: @params.', array(
'@pid' => $object->id,
'@params' => $_GET['clip'],
));
}
}
exit();
}