This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia_imagezoom.module
420 lines (370 loc) · 13.3 KB
/
media_imagezoom.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
/**
* @file
* Functions for tile cutting and rendering OpenLayers viewer
*/
/**
* Implements hook_menu().
*/
function media_imagezoom_menu() {
$items['admin/config/media/media_imagezoom'] = array(
'title' => 'Media Imagezoom',
'description' => 'Configuration for Media Imagezoom',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_imagezoom_admin_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'media_imagezoom.admin.inc',
);
$items['media/tms/1.0.0/%/%/%/%'] = array(
'page callback' => '_media_imagezoom_tile',
'page arguments' => array(3,4,5,6),
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
);
return $items;
}
/**
* Cuts a file into tiles if it hasn't already been, using Image API.
*
* @deprecated in favor of the drush commands
*/
function _media_imagezoom_generate_zoomlevel_tiles($fid, $zoom_level) {
$tile_root = variable_get('media_imagezoom_tilepath', 'public://tiles');
$pow2 = pow(2.0, $zoom_level);
// Check if the last tile to be generated for this set already exists.
// If it does, assume the tiles already exist, and don't waste time
// recreating.
$max_tile_name = $tile_root . '/tms/1.0.0/' . $fid . '/' . $zoom_level . '/' . ($zoom_level == 0 ? '0' : (intval($pow2 - 1))) . '/' . ($zoom_level == 0 ? '0' : intval($pow2 - 1)) . '.jpg';
if (file_exists($max_tile_name)) {
return;
}
$media = file_load($fid);
if ($media->type != 'image') {
return;
}
$image = image_load($media->uri);
$full_tile_width = max($image->info['width'], $image->info['height']) / pow(2.0, $zoom_level);
// Squarify.
image_crop($image, 0, 0, max($image->info['width'], $image->info['height']), max($image->info['width'], $image->info['height']), TRUE);
image_resize($image, 256 * $pow2, 256 * $pow2);
// Save temporary image.
image_save($image, $tile_root . '/tmpResizedForCutting.jpg');
for ($x = 0; $x < $pow2; ++$x) {
for ($y = 0; $y < $pow2; ++$y) {
$tile_name = $tile_root . '/tms/1.0.0/' . $fid . '/' . $zoom_level . '/' . $x . '/' . $y . '.jpg';
$tile_cut = image_load($tile_root . '/tmpResizedForCutting.jpg');
image_crop($tile_cut, $x * 256, ($pow2 - $y - 1) * 256, 256, 256);
@drupal_mkdir(dirname($tile_name), NULL, TRUE, NULL);
image_save($tile_cut, $tile_name);
}
}
}
/**
* Callback function for serving TMS tiles.
*
* Automatically generates tile if it doesn't exist.
*
* @deprecated It's absurdly faster to let the web server serve tiles
* directly, and simply fail if they haven't been generated yet, rather
* than bootstrapping Drupal to serve each tile.
*
* @param int $fid
* File ID of the image file to cut the tile from
*
* @param int $zoom_level
* The zoom level of the requested tile
*
* @param int $x
* The x offset of the tile
* (range [0,pow(2,$zoom_level)-1])
*
* @param int $y
* The y offset of the tile. Note that 0 is the
* bottom row of the image, not the top, according to TMS spec.
* (range [0,pow(2,$zoom_level)-1])
*/
function _media_imagezoom_tile($fid, $zoom_level, $x, $y) {
$y = intval(str_replace('.jpg', '', $y));
$tile_name = variable_get('media_imagezoom_tilepath', 'public://tiles') . '/tms/1.0.0/' . $fid . '/' . $zoom_level . '/' . $x . '/' . $y . '.jpg';
if (file_exists($tile_name)) {
watchdog('media_imagezoom', 'Serving cached tile ' . $tile_name, array(), WATCHDOG_INFO);
if (function_exists('drush_main')) {
return;
}
drupal_goto(file_create_url($tile_name));
}
$media = file_load($fid);
if ($media->type == 'image') {
$image = image_load($media->uri);
$full_tile_width = max($image->info['width'], $image->info['height']) / pow(2.0, $zoom_level);
// Squarify.
image_crop($image, 0, 0, max($image->info['width'], $image->info['height']), max($image->info['width'], $image->info['height']), TRUE);
image_crop($image, $x * $full_tile_width, $image->info['height'] - ($y + 1) * $full_tile_width, $full_tile_width - 1, $full_tile_width - 1);
image_resize($image, 256, 256);
// Ensure the directory is created, recursively if necessary.
// Ignore warning if it already exists.
@drupal_mkdir(dirname($tile_name), NULL, TRUE, NULL);
image_save($image, $tile_name);
if (function_exists('drush_main')) {
return;
}
drupal_goto(file_create_url($tile_name));
}
else {
if (module_exists('devel') && function_exists('dpm')) {
dpm($media, 'Cannot tile non-image media');
}
}
}
/**
* Enumerate image files.
*
* Ripped from media.browser.inc - media_browser_list.
*/
function media_imagezoom_getimages() {
$params = drupal_get_query_parameters();
// How do we validate these? I don't know.
// I think PDO should protect them, but I'm not 100% certain.
module_load_include('inc', 'media', 'includes/media.browser');
array_walk_recursive($params, '_media_recursive_check_plain');
$types = isset($params['types']) ? $params['types'] : NULL;
$url_include_patterns = isset($params['url_include_patterns']) ? $params['url_include_patterns'] : NULL;
$url_exclude_patterns = isset($params['url_exclude_patterns']) ? $params['url_exclude_patterns'] : NULL;
$start = isset($params['start']) ? $params['start'] : 0;
$limit = isset($params['limit']) ? $params['limit'] : media_variable_get('browser_pager_limit');
$query = db_select('file_managed', 'f');
$query->fields('f', array('fid'));
$query->orderBy('f.timestamp', 'DESC');
// Add conditions based on file type *or* allowed extensions.
$condition = $query;
if (!empty($types) && !empty($params['file_extensions'])) {
$condition = db_or();
}
if (!empty($types)) {
$condition->condition('f.type', $types, 'IN');
}
if (!empty($params['file_extensions'])) {
$extensions = array_filter(explode(' ', $params['file_extensions']));
foreach ($extensions as $extension) {
$condition->condition('f.uri', '%' . db_like('.' . $extension), 'LIKE');
}
}
if ($condition instanceof DatabaseCondition) {
$query->condition($condition);
}
if ($url_include_patterns) {
$query->condition('f.uri', '%' . db_like($url_include_patterns) . '%', 'LIKE');
// Insert stream related restrictions here.
}
if ($url_exclude_patterns) {
$query->condition('f.uri', '%' . db_like($url_exclude_patterns) . '%', 'NOT LIKE');
}
// @todo Implement granular editorial access: http://drupal.org/node/696970.
// In the meantime, protect information about private files from being
// discovered by unprivileged users. See also media_view_page().
if (!user_access('administer media')) {
$query->condition('f.uri', db_like('private://') . '%', 'NOT LIKE');
}
$query->condition('f.status', FILE_STATUS_PERMANENT);
foreach (array_keys(media_get_hidden_stream_wrappers()) as $name) {
$query->condition('f.uri', db_like($name . '://') . '%', 'NOT LIKE');
}
$fids = $query->execute()->fetchCol();
$files = file_load_multiple($fids);
$images = array();
foreach ($files as $file) {
if ($file->type === 'image') {
$images[] = $file;
}
}
return $images;
}
/**
* Helper function to create a single layer for the OpenLayers renderer.
*/
function media_imagezoom_build_layer($image_file, $base_layer = FALSE) {
$items = array();
$openlayers_layers = new stdClass();
$openlayers_layers->disabled = FALSE; /* Edit this to true to make a default openlayers_layers disabled initially */
$openlayers_layers->api_version = 1;
$openlayers_layers->name = ($base_layer ? 'base_' : '') . 'media_imagezoom_' . $image_file->fid;
$openlayers_layers->title = $image_file->filename;
$openlayers_layers->description = 'Media Image Zoom' . ($base_layer ? ' base' : '') . ' layer of image ' . $image_file->filename;
$openlayers_layers->data = array(
'title' => $openlayers_layers->title,
'baselayer' => $base_layer,
'base_url' => file_create_url(variable_get('media_imagezoom_tilepath', 'public://tiles') . '/tms') . '/',
'layername' => $image_file->fid,
'type' => 'jpg',
'transparent' => TRUE,
'resolutions' => array(
0 => 156543.0339,
1 => 78271.51695,
2 => 39135.758475,
3 => 19567.8792375,
4 => 9783.93961875,
5 => 4891.969809375,
),
'params' => array(
'isBaseLayer' => $base_layer,
'layers' => 'media_imagezoom_' . $image_file->fid,
'opacity' => 0.5,
),
'wrapDateLine' => 0,
'layer_type' => 'openlayers_layer_type_tms',
'projection' => array('900913'),
'serverResolutions' => openlayers_get_resolutions('900913'),
'maxExtent' => openlayers_get_extent('900913'),
'layer_handler' => 'tms',
'weight' => 0,
);
return $openlayers_layers;
}
/**
* Implements hook_ctools_plugin_api().
*/
function media_imagezoom_ctools_plugin_api($module, $api) {
// Define plugins for OpenLayers plugins api.
if ($module == "openlayers") {
switch ($api) {
case 'openlayers_layers':
return array('version' => 1);
}
}
}
/**
* Implements hook_field_formatter_info().
*/
function media_imagezoom_field_formatter_info() {
$formatters = array();
$formatters['media_imagezoom_image'] = array(
'label' => t('OpenLayers deep-image zoom'),
'field types' => array('file', 'image'),
'settings' => array(),
);
$formatters['media_imagezoom_crossfader'] = array(
'label' => t('OpenLayers deep-image zoom crossfader'),
'field types' => array('file', 'image'),
'settings' => array(),
);
return $formatters;
}
/**
* Implements hook_file_formatter_view().
*/
function media_imagezoom_file_formatter_info() {
// TODO: see if adding settings here will make a difference, maybe specify
// 'file type'.
}
/**
* Implements hook_field_formatter_view().
*/
function media_imagezoom_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$settings = $display['settings'];
$element = array();
$element[0] = array(
'#theme' => 'imagezoom_openlayers',
'files' => array(),
);
foreach ($items as $delta => $item) {
if ($field['type'] == 'image'
&& $item['image_dimensions']['width'] > 1920
&& $item['image_dimensions']['height'] > 1920) {
$element[0]['files'][$delta] = $item;
}
}
return $element;
}
/**
* Implements hook_theme().
*/
function media_imagezoom_theme($existing, $type, $theme, $path) {
return array(
'imagezoom_openlayers' => array(
'function' => 'theme_media_imagezoom_formatter_imagezoom_openlayers',
'render element' => 'element',
),
);
}
/**
* Theme function for formatter.
*/
function theme_media_imagezoom_formatter_imagezoom_openlayers($element) {
$map = new stdClass();
// Edit this to true to make a default map disabled initially.
$map->disabled = FALSE;
$map->api_version = 1;
$map->name = 'media_imagezoom_' . $element['element']['files'][0]['fid'];
$map->title = 'Media Image Zoom - ' . $element['element']['files'][0]['filename'];
$map->description = 'blah blah';
$map->media_imagezoom_files = $element['element']['files'];
$map->data = array(
'width' => 'auto',
'height' => '1000px',
'image_path' => 'sites/all/modules/openlayers/themes/default_dark/img/',
'css_path' => 'sites/all/modules/openlayers/themes/default_dark/style.css',
'proxy_host' => '',
'hide_empty_map' => 0,
'media_imagezoom_files' => $element['element']['files'],
'center' => array(
'initial' => array(
'centerpoint' => '0, 0',
'zoom' => '2',
),
'restrict' => array(
'restrictextent' => 0,
'restrictedExtent' => '',
),
),
'behaviors' => array(
'openlayers_behavior_keyboarddefaults' => array(),
'openlayers_behavior_attribution' => array(
'seperator' => '',
),
'openlayers_behavior_navigation' => array(
'zoomWheelEnabled' => 1,
'zoomBoxEnabled' => 1,
'documentDrag' => 0,
),
'openlayers_behavior_panzoombar' => array(
'zoomWorldIcon' => 0,
'panIcons' => 1,
),
'openlayers_behavior_fullscreen' => array(),
),
'default_layer' => 'base_' . $map->name,
'layers' => array(),
'layer_weight' => array(),
'layer_styles' => array(),
'layer_styles_select' => array(),
'layer_activated' => array(),
'layer_switcher' => array(),
'projection' => '900913',
'displayProjection' => '4326',
'styles' => array(
'default' => 'default',
'select' => 'default_select',
'temporary' => 'default',
),
'map_name' => $map->name,
);
openlayers_include();
drupal_add_js(drupal_get_path('module', 'openlayers') . '/plugins/layer_types/openlayers_layer_type_tms.js');
drupal_add_library('system', 'ui.slider');
drupal_add_js(drupal_get_path('module', 'media_imagezoom') . '/media_imagezoom_crossfader.js');
return openlayers_render_map($map);
}
/**
* Implements hook_openlayers_map_alter().
*
* Necessary for on-the-fly layer creation.
*/
function media_imagezoom_openlayers_map_alter(&$map) {
foreach ($map['media_imagezoom_files'] as $delta => $image) {
$image_file = file_load($image['fid']);
$map['layers']['base_media_imagezoom_' . $image['fid']] = media_imagezoom_build_layer($image_file, TRUE)->data;
$map['layers']['media_imagezoom_' . $image['fid']] = media_imagezoom_build_layer($image_file, FALSE)->data;
}
return;
}