1<?php 2/** 3 * Embed an image gallery 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 11require_once(DOKU_PLUGIN.'syntax.php'); 12require_once(DOKU_INC.'inc/search.php'); 13require_once(DOKU_INC.'inc/JpegMeta.php'); 14 15class syntax_plugin_panoview extends DokuWiki_Syntax_Plugin { 16 /** 17 * return some info 18 */ 19 function getInfo(){ 20 return confToHash(dirname(__FILE__).'/info.txt'); 21 } 22 23 /** 24 * What kind of syntax are we? 25 */ 26 function getType(){ 27 return 'substition'; 28 } 29 30 /** 31 * What about paragraphs? 32 */ 33 function getPType(){ 34 return 'block'; 35 } 36 37 /** 38 * Where to sort in? 39 */ 40 function getSort(){ 41 return 301; 42 } 43 44 45 /** 46 * Connect pattern to lexer 47 */ 48 function connectTo($mode) { 49 $this->Lexer->addSpecialPattern('\{\{panoview>[^}]*\}\}',$mode,'plugin_panoview'); 50 } 51 52 /** 53 * Handle the match 54 */ 55 function handle($match, $state, $pos, &$handler){ 56 global $ID; 57 58 $data = array( 59 'width' => 500, 60 'height' => 250, 61 'align' => 0, 62 'initialZoom' => 2, 63 'tileBaseUri' => DOKU_BASE.'lib/plugins/panoview/tiles.php', 64 'tileSize' => 256, 65 'maxZoom' => 10, 66 'blankTile' => DOKU_BASE.'lib/plugins/panoview/gfx/blank.gif', 67 'loadingTile' => DOKU_BASE.'lib/plugins/panoview/gfx/progress.gif', 68 ); 69 70 $match = substr($match,11,-2); //strip markup from start and end 71 72 // alignment 73 $data['align'] = 0; 74 if(substr($match,0,1) == ' ') $data['align'] += 1; 75 if(substr($match,-1,1) == ' ') $data['align'] += 2; 76 77 // extract params 78 list($img,$params) = explode('?',$match,2); 79 $img = trim($img); 80 81 // resolving relatives 82 $data['image'] = resolve_id(getNS($ID),$img); 83 84 $file = mediaFN($data['image']); 85 list($data['imageWidth'],$data['imageHeight']) = @getimagesize($file); 86 87 // size 88 if(preg_match('/^(\d+)[xX](\d+)$/',$params,$match)){ 89 $data['width'] = $match[1]; 90 $data['height'] = $match[2]; 91 } 92 93 return $data; 94 } 95 96 /** 97 * Create output 98 */ 99 function render($mode, &$R, $data) { 100 if($mode != 'xhtml') return false; 101 global $ID; 102 require_once(DOKU_INC.'inc/JSON.php'); 103 $json = new JSON(); 104 105 $img = '<a href="'.ml($data['image'],array('id'=>$ID),false).'"><img src="'. 106 ml($data['image'], array('w'=>$data['width'],'h'=>$data['height'])).'" width="'. 107 $data['width'].'" height="'.$data['height'].'" alt="" /></a>'; 108 109 if($data['align'] == 1){ 110 $align = 'medialeft'; 111 }elseif($data['align'] == 2){ 112 $align = 'mediaright'; 113 }else{ 114 $align = 'mediacenter'; 115 } 116 117 118 $R->doc .= ' 119 <div class="panoview_plugin '.$align.'" style="width: '.$data['width'].'px; height: '.$data['height'].'px;"> 120 <div class="well"><!-- --></div> 121 <div class="surface">'.$img.'</div> 122 <p class="controls" style="display: none"> 123 <span class="zoomIn" title="Zoom In">+</span> 124 <span class="zoomOut" title="Zoom Out">-</span> 125 </p> 126 <div class="options" style="display:none">'.hsc($json->encode($data)).'</div> 127 </div> 128 '; 129 130 131 return true; 132 } 133 134 // ----------- Tile Generator below --------------- 135 136 /** 137 * Create a tile using libGD 138 */ 139 function tile_gd($d){ 140 global $conf; 141 142 $img = null; 143 if(preg_match('/\.jpe?g$/',$d['file'])){ 144 $img = @imagecreatefromjpeg($d['file']); 145 }elseif(preg_match('/\.png$/',$d['file'])){ 146 $img = @imagecreatefrompng($d['file']); 147 }elseif(preg_match('/\.gif$/',$d['file'])){ 148 $img = @imagecreatefromgif($d['file']); 149 } 150 if(!$img) $this->gfx_error('generic'); 151 152 $crop = $this->image_crop($img,$d['width'],$d['height'],$d['tlx'],$d['tly'],$d['brx'],$d['bry']); 153 imagedestroy($img); 154 155 $scale = $this->image_scale($crop,abs($d['brx'] - $d['tlx']),abs($d['bry'] - $d['tly']),$d['ts'],$d['ts']); 156 imagedestroy($crop); 157 158 imagejpeg($scale,$d['cache'],$conf['jpg_quality']); 159 imagedestroy($scale); 160 161 if($conf['fperm']) chmod($d['cache'], $conf['fperm']); 162 } 163 164 /** 165 * Create a tile using Image Magick 166 */ 167 function tile_im($d){ 168 global $conf; 169 170 $cmd = $this->getConf('nice'); 171 $cmd .= ' '.$conf['im_convert']; 172 $cmd .= ' '.escapeshellarg($d['file']); 173 $cmd .= ' -crop \''.abs($d['brx'] - $d['tlx']).'x'.abs($d['bry'] - $d['tly']).'!+'.$d['tlx'].'+'.$d['tly'].'\''; 174 $cmd .= ' -background black'; 175 $cmd .= ' -extent \''.abs($d['brx'] - $d['tlx']).'x'.abs($d['bry'] - $d['tly']).'!\''; 176 $cmd .= ' -resize \''.$d['ts'].'x'.$d['ts'].'!\''; 177 178 $cmd .= ' -quality '.$conf['jpg_quality']; 179 $cmd .= ' '.escapeshellarg($d['cache']); 180 181 # dbg($cmd); exit; 182 183 @exec($cmd,$out,$retval); 184 if ($retval == 0) return true; 185 $this->gfx_error('generic'); 186 } 187 188 /** 189 * Scale an image with libGD 190 */ 191 function image_scale($image,$x,$y,$w,$h){ 192 $scale=imagecreatetruecolor($w,$h); 193 imagecopyresampled($scale,$image,0,0,0,0,$w,$h,$x,$y); 194 return $scale; 195 } 196 197 /** 198 * Crop an image with libGD 199 */ 200 function image_crop($image,$x,$y,$left,$upper,$right,$lower) { 201 $w=abs($right-$left); 202 $h=abs($lower-$upper); 203 $crop = imagecreatetruecolor($w,$h); 204 imagecopy($crop,$image,0,0,$left,$upper,$w,$h); 205 return $crop; 206 } 207 208 /** 209 * Send a graphical error message and stop script 210 */ 211 function gfx_error($type){ 212 $file = dirname(__FILE__).'/gfx/'.$type.'.gif'; 213 $time = filemtime($file); 214 header('Content-type: image/gif'); 215 216 http_conditionalRequest($time); 217 http_sendfile($file); 218 readfile($file); 219 exit; 220 } 221 222 /** 223 * Acquire a lock for the tile generator 224 */ 225 function tile_lock($d){ 226 global $conf; 227 228 $lockDir = $conf['lockdir'].'/'.md5($d['id']).'.panoview'; 229 @ignore_user_abort(1); 230 231 $timeStart = time(); 232 do { 233 //waited longer than 25 seconds? -> stale lock? 234 if ((time() - $timeStart) > 25){ 235 if(time() - filemtime($lockDir) > 30) $this->tile_unlock($d); 236 send_redirect(DOKU_URL.'lib/plugins/panaoview/tiles.php?tile='.$d['zoom'].'-'.$d['col'].'-'.$d['row'].'&image='.rawurlencode($d['id'])); 237 exit; 238 } 239 $locked = @mkdir($lockDir, $conf['dmode']); 240 if($locked){ 241 if(!empty($conf['dperm'])) chmod($lockDir, $conf['dperm']); 242 break; 243 } 244 usleep(rand(500,3000)); 245 } while ($locked === false); 246 } 247 248 /** 249 * Unlock the tile generator 250 */ 251 function tile_unlock($d){ 252 global $conf; 253 254 $lockDir = $conf['lockdir'].'/'.md5($d['id']).'.panoview'; 255 @rmdir($lockDir); 256 @ignore_user_abort(0); 257 } 258 259 260} 261 262//Setup VIM: ex: et ts=4 enc=utf-8 : 263