127bbde00SAndreas Gohr<?php 227bbde00SAndreas Gohr/** 327bbde00SAndreas Gohr * Embed an image gallery 427bbde00SAndreas Gohr * 527bbde00SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 627bbde00SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 727bbde00SAndreas Gohr */ 827bbde00SAndreas Gohrclass syntax_plugin_panoview extends DokuWiki_Syntax_Plugin { 927bbde00SAndreas Gohr 1027bbde00SAndreas Gohr /** 1127bbde00SAndreas Gohr * What kind of syntax are we? 1227bbde00SAndreas Gohr */ 1327bbde00SAndreas Gohr function getType() { 1427bbde00SAndreas Gohr return 'substition'; 1527bbde00SAndreas Gohr } 1627bbde00SAndreas Gohr 1727bbde00SAndreas Gohr /** 1827bbde00SAndreas Gohr * What about paragraphs? 1927bbde00SAndreas Gohr */ 2027bbde00SAndreas Gohr function getPType() { 2127bbde00SAndreas Gohr return 'block'; 2227bbde00SAndreas Gohr } 2327bbde00SAndreas Gohr 2427bbde00SAndreas Gohr /** 2527bbde00SAndreas Gohr * Where to sort in? 2627bbde00SAndreas Gohr */ 2727bbde00SAndreas Gohr function getSort() { 2827bbde00SAndreas Gohr return 301; 2927bbde00SAndreas Gohr } 3027bbde00SAndreas Gohr 3127bbde00SAndreas Gohr /** 3227bbde00SAndreas Gohr * Connect pattern to lexer 3327bbde00SAndreas Gohr */ 3427bbde00SAndreas Gohr function connectTo($mode) { 3527bbde00SAndreas Gohr $this->Lexer->addSpecialPattern('\{\{panoview>[^}]*\}\}', $mode, 'plugin_panoview'); 3627bbde00SAndreas Gohr } 3727bbde00SAndreas Gohr 3827bbde00SAndreas Gohr /** 3927bbde00SAndreas Gohr * Handle the match 4027bbde00SAndreas Gohr */ 410f7356f2SAndreas Gohr function handle($match, $state, $pos, Doku_Handler $handler) { 4227bbde00SAndreas Gohr global $ID; 4327bbde00SAndreas Gohr 441a675665SAndreas Gohr $data = array( 4527bbde00SAndreas Gohr 'width' => 500, 4627bbde00SAndreas Gohr 'height' => 250, 471a675665SAndreas Gohr 'align' => 0, 484212171fSAndreas Gohr 'initialZoom' => 1, 4927bbde00SAndreas Gohr 'tileBaseUri' => DOKU_BASE.'lib/plugins/panoview/tiles.php', 5027bbde00SAndreas Gohr 'tileSize' => 256, 5127bbde00SAndreas Gohr 'maxZoom' => 10, 5227bbde00SAndreas Gohr 'blankTile' => DOKU_BASE.'lib/plugins/panoview/gfx/blank.gif', 5327bbde00SAndreas Gohr 'loadingTile' => DOKU_BASE.'lib/plugins/panoview/gfx/progress.gif', 5427bbde00SAndreas Gohr ); 5527bbde00SAndreas Gohr 561a675665SAndreas Gohr $match = substr($match, 11, -2); //strip markup from start and end 5727bbde00SAndreas Gohr 581a675665SAndreas Gohr // alignment 591a675665SAndreas Gohr $data['align'] = 0; 601a675665SAndreas Gohr if(substr($match, 0, 1) == ' ') $data['align'] += 1; 611a675665SAndreas Gohr if(substr($match, -1, 1) == ' ') $data['align'] += 2; 621a675665SAndreas Gohr 631a675665SAndreas Gohr // extract params 641a675665SAndreas Gohr list($img, $params) = explode('?', $match, 2); 651a675665SAndreas Gohr $img = trim($img); 661a675665SAndreas Gohr 671a675665SAndreas Gohr // resolving relatives 681a675665SAndreas Gohr $data['image'] = resolve_id(getNS($ID), $img); 691a675665SAndreas Gohr 701a675665SAndreas Gohr $file = mediaFN($data['image']); 711a675665SAndreas Gohr list($data['imageWidth'], $data['imageHeight']) = @getimagesize($file); 721a675665SAndreas Gohr 734212171fSAndreas Gohr // calculate maximum zoom 74359cb25cSAndreas Gohr $data['maxZoom'] = ceil(sqrt(max($data['imageWidth'], $data['imageHeight']) / $data['tileSize'])); 754212171fSAndreas Gohr 761a675665SAndreas Gohr // size 774212171fSAndreas Gohr if(preg_match('/\b(\d+)[xX](\d+)\b/', $params, $match)) { 781a675665SAndreas Gohr $data['width'] = $match[1]; 791a675665SAndreas Gohr $data['height'] = $match[2]; 801a675665SAndreas Gohr } 811a675665SAndreas Gohr 824212171fSAndreas Gohr // initial zoom 834212171fSAndreas Gohr if(preg_match('/\b[zZ](\d+)\b/', $params, $match)) { 844212171fSAndreas Gohr $data['initialZoom'] = $match[1]; 854212171fSAndreas Gohr } 864212171fSAndreas Gohr if($data['initialZoom'] < 0) $data['initialZoom'] = 0; 8724e066d7SAndreas Gohr if($data['initialZoom'] > $data['maxZoom']) $data['initialZoom'] = $data['maxZoom']; 884212171fSAndreas Gohr 891a675665SAndreas Gohr return $data; 9027bbde00SAndreas Gohr } 9127bbde00SAndreas Gohr 9227bbde00SAndreas Gohr /** 9327bbde00SAndreas Gohr * Create output 9427bbde00SAndreas Gohr */ 950f7356f2SAndreas Gohr function render($mode, Doku_Renderer $R, $data) { 9627bbde00SAndreas Gohr if($mode != 'xhtml') return false; 97da5b8f02SAndreas Gohr global $ID; 9827bbde00SAndreas Gohr 99da5b8f02SAndreas Gohr $img = '<a href="'.ml($data['image'], array('id'=> $ID), false).'"><img src="'. 1001a675665SAndreas Gohr ml($data['image'], array('w'=> $data['width'], 'h'=> $data['height'])).'" width="'. 1011a675665SAndreas Gohr $data['width'].'" height="'.$data['height'].'" alt="" /></a>'; 10227bbde00SAndreas Gohr 1031613315fSAndreas Gohr if($data['align'] == 1) { 1041613315fSAndreas Gohr $align = 'medialeft'; 1051613315fSAndreas Gohr } elseif($data['align'] == 2) { 1061613315fSAndreas Gohr $align = 'mediaright'; 1071613315fSAndreas Gohr } else { 1081613315fSAndreas Gohr $align = 'mediacenter'; 1091613315fSAndreas Gohr } 1101613315fSAndreas Gohr 11127bbde00SAndreas Gohr $R->doc .= ' 1121613315fSAndreas Gohr <div class="panoview_plugin '.$align.'" style="width: '.$data['width'].'px; height: '.$data['height'].'px;"> 1139d7db126SAndreas Gohr <div class="well"><!-- --></div> 1149d7db126SAndreas Gohr <div class="surface">'.$img.'</div> 11527bbde00SAndreas Gohr <p class="controls" style="display: none"> 11627bbde00SAndreas Gohr <span class="zoomIn" title="Zoom In">+</span> 11727bbde00SAndreas Gohr <span class="zoomOut" title="Zoom Out">-</span> 118493e6d40SAndreas Gohr <span class="maximize"><img src="'.DOKU_BASE.'lib/plugins/panoview/gfx/window.gif" style="position: absolute; bottom: 4px; right: 5px;" title="Maximize"></span> 11927bbde00SAndreas Gohr </p> 120*7242a9edSAndreas Gohr <div class="options" style="display:none">'.hsc(json_encode($data)).'</div> 12127bbde00SAndreas Gohr </div> 12227bbde00SAndreas Gohr '; 12327bbde00SAndreas Gohr 12427bbde00SAndreas Gohr return true; 12527bbde00SAndreas Gohr } 12627bbde00SAndreas Gohr 1271faa1ff0SAndreas Gohr // ----------- Tile Generator below --------------- 1281faa1ff0SAndreas Gohr 1291faa1ff0SAndreas Gohr /** 1301faa1ff0SAndreas Gohr * Create a tile using libGD 1311faa1ff0SAndreas Gohr */ 1321faa1ff0SAndreas Gohr function tile_gd($d) { 1331faa1ff0SAndreas Gohr global $conf; 1341faa1ff0SAndreas Gohr 1351faa1ff0SAndreas Gohr $img = null; 1361faa1ff0SAndreas Gohr if(preg_match('/\.jpe?g$/', $d['file'])) { 1371faa1ff0SAndreas Gohr $img = @imagecreatefromjpeg($d['file']); 1381faa1ff0SAndreas Gohr } elseif(preg_match('/\.png$/', $d['file'])) { 1391faa1ff0SAndreas Gohr $img = @imagecreatefrompng($d['file']); 1401faa1ff0SAndreas Gohr } elseif(preg_match('/\.gif$/', $d['file'])) { 1411faa1ff0SAndreas Gohr $img = @imagecreatefromgif($d['file']); 1421faa1ff0SAndreas Gohr } 1431faa1ff0SAndreas Gohr if(!$img) $this->gfx_error('generic'); 1441faa1ff0SAndreas Gohr 1451faa1ff0SAndreas Gohr $crop = $this->image_crop($img, $d['width'], $d['height'], $d['tlx'], $d['tly'], $d['brx'], $d['bry']); 1461faa1ff0SAndreas Gohr imagedestroy($img); 1471faa1ff0SAndreas Gohr 1481faa1ff0SAndreas Gohr $scale = $this->image_scale($crop, abs($d['brx'] - $d['tlx']), abs($d['bry'] - $d['tly']), $d['ts'], $d['ts']); 1491faa1ff0SAndreas Gohr imagedestroy($crop); 1501faa1ff0SAndreas Gohr 1511faa1ff0SAndreas Gohr imagejpeg($scale, $d['cache'], $conf['jpg_quality']); 1521faa1ff0SAndreas Gohr imagedestroy($scale); 1531faa1ff0SAndreas Gohr 1541faa1ff0SAndreas Gohr if($conf['fperm']) chmod($d['cache'], $conf['fperm']); 1551faa1ff0SAndreas Gohr } 1561faa1ff0SAndreas Gohr 1571faa1ff0SAndreas Gohr /** 1581faa1ff0SAndreas Gohr * Create a tile using Image Magick 1591faa1ff0SAndreas Gohr */ 1601faa1ff0SAndreas Gohr function tile_im($d) { 1611faa1ff0SAndreas Gohr global $conf; 1621faa1ff0SAndreas Gohr 1631faa1ff0SAndreas Gohr $cmd = $this->getConf('nice'); 1641faa1ff0SAndreas Gohr $cmd .= ' '.$conf['im_convert']; 1651faa1ff0SAndreas Gohr $cmd .= ' '.escapeshellarg($d['file']); 1661faa1ff0SAndreas Gohr $cmd .= ' -crop \''.abs($d['brx'] - $d['tlx']).'x'.abs($d['bry'] - $d['tly']).'!+'.$d['tlx'].'+'.$d['tly'].'\''; 1671faa1ff0SAndreas Gohr $cmd .= ' -background black'; 1681faa1ff0SAndreas Gohr $cmd .= ' -extent \''.abs($d['brx'] - $d['tlx']).'x'.abs($d['bry'] - $d['tly']).'!\''; 1691faa1ff0SAndreas Gohr $cmd .= ' -resize \''.$d['ts'].'x'.$d['ts'].'!\''; 1701faa1ff0SAndreas Gohr 1711faa1ff0SAndreas Gohr $cmd .= ' -quality '.$conf['jpg_quality']; 1721faa1ff0SAndreas Gohr $cmd .= ' '.escapeshellarg($d['cache']); 1731faa1ff0SAndreas Gohr 1741faa1ff0SAndreas Gohr # dbg($cmd); exit; 1751faa1ff0SAndreas Gohr 1761faa1ff0SAndreas Gohr @exec($cmd, $out, $retval); 1771faa1ff0SAndreas Gohr if($retval == 0) return true; 1781faa1ff0SAndreas Gohr $this->gfx_error('generic'); 1791faa1ff0SAndreas Gohr } 1801faa1ff0SAndreas Gohr 1811faa1ff0SAndreas Gohr /** 1821faa1ff0SAndreas Gohr * Scale an image with libGD 1831faa1ff0SAndreas Gohr */ 1841faa1ff0SAndreas Gohr function image_scale($image, $x, $y, $w, $h) { 1851faa1ff0SAndreas Gohr $scale = imagecreatetruecolor($w, $h); 1861faa1ff0SAndreas Gohr imagecopyresampled($scale, $image, 0, 0, 0, 0, $w, $h, $x, $y); 1871faa1ff0SAndreas Gohr return $scale; 1881faa1ff0SAndreas Gohr } 1891faa1ff0SAndreas Gohr 1901faa1ff0SAndreas Gohr /** 1911faa1ff0SAndreas Gohr * Crop an image with libGD 1921faa1ff0SAndreas Gohr */ 1931faa1ff0SAndreas Gohr function image_crop($image, $x, $y, $left, $upper, $right, $lower) { 1941faa1ff0SAndreas Gohr $w = abs($right - $left); 1951faa1ff0SAndreas Gohr $h = abs($lower - $upper); 1961faa1ff0SAndreas Gohr $crop = imagecreatetruecolor($w, $h); 1971faa1ff0SAndreas Gohr imagecopy($crop, $image, 0, 0, $left, $upper, $w, $h); 1981faa1ff0SAndreas Gohr return $crop; 1991faa1ff0SAndreas Gohr } 2001faa1ff0SAndreas Gohr 2011faa1ff0SAndreas Gohr /** 2021faa1ff0SAndreas Gohr * Send a graphical error message and stop script 2031faa1ff0SAndreas Gohr */ 2041faa1ff0SAndreas Gohr function gfx_error($type) { 2051faa1ff0SAndreas Gohr $file = dirname(__FILE__).'/gfx/'.$type.'.gif'; 2061faa1ff0SAndreas Gohr $time = filemtime($file); 2071faa1ff0SAndreas Gohr header('Content-type: image/gif'); 2081faa1ff0SAndreas Gohr 2091faa1ff0SAndreas Gohr http_conditionalRequest($time); 2101faa1ff0SAndreas Gohr http_sendfile($file); 2111faa1ff0SAndreas Gohr readfile($file); 2121faa1ff0SAndreas Gohr exit; 2131faa1ff0SAndreas Gohr } 2141faa1ff0SAndreas Gohr 2151faa1ff0SAndreas Gohr /** 2161faa1ff0SAndreas Gohr * Acquire a lock for the tile generator 2171faa1ff0SAndreas Gohr */ 2181faa1ff0SAndreas Gohr function tile_lock($d) { 2191faa1ff0SAndreas Gohr global $conf; 2201faa1ff0SAndreas Gohr 2211faa1ff0SAndreas Gohr $lockDir = $conf['lockdir'].'/'.md5($d['id']).'.panoview'; 2221faa1ff0SAndreas Gohr @ignore_user_abort(1); 2231faa1ff0SAndreas Gohr 2241faa1ff0SAndreas Gohr $timeStart = time(); 2251faa1ff0SAndreas Gohr do { 2261faa1ff0SAndreas Gohr //waited longer than 25 seconds? -> stale lock? 2271faa1ff0SAndreas Gohr if((time() - $timeStart) > 25) { 228d6577f0fSAndreas Gohr if(time() - @filemtime($lockDir) > 30) $this->tile_unlock($d); 229d6577f0fSAndreas Gohr send_redirect(DOKU_URL.'lib/plugins/panoview/tiles.php?tile='.$d['zoom'].'-'.$d['col'].'-'.$d['row'].'&image='.rawurlencode($d['id'])); 2301faa1ff0SAndreas Gohr exit; 2311faa1ff0SAndreas Gohr } 2321faa1ff0SAndreas Gohr $locked = @mkdir($lockDir, $conf['dmode']); 2331faa1ff0SAndreas Gohr if($locked) { 2341faa1ff0SAndreas Gohr if(!empty($conf['dperm'])) chmod($lockDir, $conf['dperm']); 2351faa1ff0SAndreas Gohr break; 2361faa1ff0SAndreas Gohr } 2371faa1ff0SAndreas Gohr usleep(rand(500, 3000)); 2381faa1ff0SAndreas Gohr } while($locked === false); 2391faa1ff0SAndreas Gohr } 2401faa1ff0SAndreas Gohr 2411faa1ff0SAndreas Gohr /** 2421faa1ff0SAndreas Gohr * Unlock the tile generator 2431faa1ff0SAndreas Gohr */ 2441faa1ff0SAndreas Gohr function tile_unlock($d) { 2451faa1ff0SAndreas Gohr global $conf; 2461faa1ff0SAndreas Gohr 2471faa1ff0SAndreas Gohr $lockDir = $conf['lockdir'].'/'.md5($d['id']).'.panoview'; 2481faa1ff0SAndreas Gohr @rmdir($lockDir); 2491faa1ff0SAndreas Gohr @ignore_user_abort(0); 2501faa1ff0SAndreas Gohr } 25127bbde00SAndreas Gohr} 252