-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.php
263 lines (218 loc) · 8.01 KB
/
syntax.php
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
<?php
/**
* Embed an image gallery
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <andi@splitbrain.org>
*/
if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
require_once(DOKU_INC.'inc/search.php');
require_once(DOKU_INC.'inc/JpegMeta.php');
class syntax_plugin_panoview extends DokuWiki_Syntax_Plugin {
/**
* What kind of syntax are we?
*/
function getType() {
return 'substition';
}
/**
* What about paragraphs?
*/
function getPType() {
return 'block';
}
/**
* Where to sort in?
*/
function getSort() {
return 301;
}
/**
* Connect pattern to lexer
*/
function connectTo($mode) {
$this->Lexer->addSpecialPattern('\{\{panoview>[^}]*\}\}', $mode, 'plugin_panoview');
}
/**
* Handle the match
*/
function handle($match, $state, $pos, Doku_Handler $handler) {
global $ID;
$data = array(
'width' => 500,
'height' => 250,
'align' => 0,
'initialZoom' => 1,
'tileBaseUri' => DOKU_BASE.'lib/plugins/panoview/tiles.php',
'tileSize' => 256,
'maxZoom' => 10,
'blankTile' => DOKU_BASE.'lib/plugins/panoview/gfx/blank.gif',
'loadingTile' => DOKU_BASE.'lib/plugins/panoview/gfx/progress.gif',
);
$match = substr($match, 11, -2); //strip markup from start and end
// alignment
$data['align'] = 0;
if(substr($match, 0, 1) == ' ') $data['align'] += 1;
if(substr($match, -1, 1) == ' ') $data['align'] += 2;
// extract params
list($img, $params) = explode('?', $match, 2);
$img = trim($img);
// resolving relatives
$data['image'] = resolve_id(getNS($ID), $img);
$file = mediaFN($data['image']);
list($data['imageWidth'], $data['imageHeight']) = @getimagesize($file);
// calculate maximum zoom
$data['maxZoom'] = ceil(sqrt(max($data['imageWidth'], $data['imageHeight']) / $data['tileSize']));
// size
if(preg_match('/\b(\d+)[xX](\d+)\b/', $params, $match)) {
$data['width'] = $match[1];
$data['height'] = $match[2];
}
// initial zoom
if(preg_match('/\b[zZ](\d+)\b/', $params, $match)) {
$data['initialZoom'] = $match[1];
}
if($data['initialZoom'] < 0) $data['initialZoom'] = 0;
if($data['initialZoom'] > $data['maxZoom']) $data['initialZoom'] = $data['maxZoom'];
return $data;
}
/**
* Create output
*/
function render($mode, Doku_Renderer $R, $data) {
if($mode != 'xhtml') return false;
global $ID;
require_once(DOKU_INC.'inc/JSON.php');
$json = new JSON();
$img = '<a href="'.ml($data['image'], array('id'=> $ID), false).'"><img src="'.
ml($data['image'], array('w'=> $data['width'], 'h'=> $data['height'])).'" width="'.
$data['width'].'" height="'.$data['height'].'" alt="" /></a>';
if($data['align'] == 1) {
$align = 'medialeft';
} elseif($data['align'] == 2) {
$align = 'mediaright';
} else {
$align = 'mediacenter';
}
$R->doc .= '
<div class="panoview_plugin '.$align.'" style="width: '.$data['width'].'px; height: '.$data['height'].'px;">
<div class="well"><!-- --></div>
<div class="surface">'.$img.'</div>
<p class="controls" style="display: none">
<span class="zoomIn" title="Zoom In">+</span>
<span class="zoomOut" title="Zoom Out">-</span>
<span class="maximize"><img src="'.DOKU_BASE.'lib/plugins/panoview/gfx/window.gif" style="position: absolute; bottom: 4px; right: 5px;" title="Maximize"></span>
</p>
<div class="options" style="display:none">'.hsc($json->encode($data)).'</div>
</div>
';
return true;
}
// ----------- Tile Generator below ---------------
/**
* Create a tile using libGD
*/
function tile_gd($d) {
global $conf;
$img = null;
if(preg_match('/\.jpe?g$/', $d['file'])) {
$img = @imagecreatefromjpeg($d['file']);
} elseif(preg_match('/\.png$/', $d['file'])) {
$img = @imagecreatefrompng($d['file']);
} elseif(preg_match('/\.gif$/', $d['file'])) {
$img = @imagecreatefromgif($d['file']);
}
if(!$img) $this->gfx_error('generic');
$crop = $this->image_crop($img, $d['width'], $d['height'], $d['tlx'], $d['tly'], $d['brx'], $d['bry']);
imagedestroy($img);
$scale = $this->image_scale($crop, abs($d['brx'] - $d['tlx']), abs($d['bry'] - $d['tly']), $d['ts'], $d['ts']);
imagedestroy($crop);
imagejpeg($scale, $d['cache'], $conf['jpg_quality']);
imagedestroy($scale);
if($conf['fperm']) chmod($d['cache'], $conf['fperm']);
}
/**
* Create a tile using Image Magick
*/
function tile_im($d) {
global $conf;
$cmd = $this->getConf('nice');
$cmd .= ' '.$conf['im_convert'];
$cmd .= ' '.escapeshellarg($d['file']);
$cmd .= ' -crop \''.abs($d['brx'] - $d['tlx']).'x'.abs($d['bry'] - $d['tly']).'!+'.$d['tlx'].'+'.$d['tly'].'\'';
$cmd .= ' -background black';
$cmd .= ' -extent \''.abs($d['brx'] - $d['tlx']).'x'.abs($d['bry'] - $d['tly']).'!\'';
$cmd .= ' -resize \''.$d['ts'].'x'.$d['ts'].'!\'';
$cmd .= ' -quality '.$conf['jpg_quality'];
$cmd .= ' '.escapeshellarg($d['cache']);
# dbg($cmd); exit;
@exec($cmd, $out, $retval);
if($retval == 0) return true;
$this->gfx_error('generic');
}
/**
* Scale an image with libGD
*/
function image_scale($image, $x, $y, $w, $h) {
$scale = imagecreatetruecolor($w, $h);
imagecopyresampled($scale, $image, 0, 0, 0, 0, $w, $h, $x, $y);
return $scale;
}
/**
* Crop an image with libGD
*/
function image_crop($image, $x, $y, $left, $upper, $right, $lower) {
$w = abs($right - $left);
$h = abs($lower - $upper);
$crop = imagecreatetruecolor($w, $h);
imagecopy($crop, $image, 0, 0, $left, $upper, $w, $h);
return $crop;
}
/**
* Send a graphical error message and stop script
*/
function gfx_error($type) {
$file = dirname(__FILE__).'/gfx/'.$type.'.gif';
$time = filemtime($file);
header('Content-type: image/gif');
http_conditionalRequest($time);
http_sendfile($file);
readfile($file);
exit;
}
/**
* Acquire a lock for the tile generator
*/
function tile_lock($d) {
global $conf;
$lockDir = $conf['lockdir'].'/'.md5($d['id']).'.panoview';
@ignore_user_abort(1);
$timeStart = time();
do {
//waited longer than 25 seconds? -> stale lock?
if((time() - $timeStart) > 25) {
if(time() - @filemtime($lockDir) > 30) $this->tile_unlock($d);
send_redirect(DOKU_URL.'lib/plugins/panoview/tiles.php?tile='.$d['zoom'].'-'.$d['col'].'-'.$d['row'].'&image='.rawurlencode($d['id']));
exit;
}
$locked = @mkdir($lockDir, $conf['dmode']);
if($locked) {
if(!empty($conf['dperm'])) chmod($lockDir, $conf['dperm']);
break;
}
usleep(rand(500, 3000));
} while($locked === false);
}
/**
* Unlock the tile generator
*/
function tile_unlock($d) {
global $conf;
$lockDir = $conf['lockdir'].'/'.md5($d['id']).'.panoview';
@rmdir($lockDir);
@ignore_user_abort(0);
}
}
//Setup VIM: ex: et ts=4 enc=utf-8 :