xref: /plugin/panoview/tiles.php (revision 05eebc514d42f3a84a0241b9c31aaa995d2d0ca1)
127bbde00SAndreas Gohr<?php
227bbde00SAndreas Gohr/**
327bbde00SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
427bbde00SAndreas Gohr * @author     Andreas Gohr <gohr@cosmocode.de>
527bbde00SAndreas Gohr */
627bbde00SAndreas Gohr
727bbde00SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
827bbde00SAndreas Gohrdefine('DOKU_DISABLE_GZIP_OUTPUT', 1);
927bbde00SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
1027bbde00SAndreas Gohrrequire_once(DOKU_INC.'inc/common.php');
1127bbde00SAndreas Gohrrequire_once(DOKU_INC.'inc/pageutils.php');
1227bbde00SAndreas Gohrrequire_once(DOKU_INC.'inc/httputils.php');
1327bbde00SAndreas Gohrrequire_once(DOKU_INC.'inc/auth.php');
1427bbde00SAndreas Gohrsession_write_close();
1527bbde00SAndreas Gohr
1627bbde00SAndreas Gohr    $data = array();
1727bbde00SAndreas Gohr    // get parameters
1827bbde00SAndreas Gohr    list($data['zoom'],$data['col'],$data['row']) = explode('-',$_GET['tile']);
1927bbde00SAndreas Gohr    $data['id']    = cleanID($_GET['image']);
2027bbde00SAndreas Gohr    $data['file']  = mediaFN($data['id']);
2127bbde00SAndreas Gohr    $data['mtime'] = @filemtime($data['file']);
2227bbde00SAndreas Gohr
2327bbde00SAndreas Gohr    // check auth and existance
2427bbde00SAndreas Gohr    if(auth_quickaclcheck(getNS($data['id']).':X') < AUTH_READ) gfx_error('noauth');
2527bbde00SAndreas Gohr    if(!$data['mtime']) gfx_error('notfound');
2627bbde00SAndreas Gohr
2727bbde00SAndreas Gohr    // calculate zoom level scaling
2827bbde00SAndreas Gohr    $data['ts']    = 256;
2927bbde00SAndreas Gohr    list($data['width'],$data['height']) = getimagesize($data['file']);
3027bbde00SAndreas Gohr    $data['scale'] = (int)pow(2,$data['zoom']);
3127bbde00SAndreas Gohr    $data['max']   = max($data['width'],$data['height']);
3227bbde00SAndreas Gohr    $data['inv']   = $data['max']  / ($data['ts'] * $data['scale']);
3327bbde00SAndreas Gohr    if($data['inv'] < 1.0) gfx_error('maxzoom');
3427bbde00SAndreas Gohr
3527bbde00SAndreas Gohr    // calculate tile boundaries
3627bbde00SAndreas Gohr    $data['tlx']   = (int) ($data['col'] * $data['ts'] * $data['inv']);
3727bbde00SAndreas Gohr    $data['tly']   = (int) ($data['row'] * $data['ts'] * $data['inv']);
3827bbde00SAndreas Gohr    $data['brx']   = (int) ($data['tlx'] + ($data['ts'] * $data['inv']));
3927bbde00SAndreas Gohr    $data['bry']   = (int) ($data['tly'] + ($data['ts'] * $data['inv']));
40*05eebc51SAndreas Gohr    if($data['tlx'] > $data['width'] || $data['tly'] > $data['height']) gfx_error('blank');
4127bbde00SAndreas Gohr
4227bbde00SAndreas Gohr    // cache times
4327bbde00SAndreas Gohr    $data['cache']  = getCacheName($data['file'],'.pv.'.$data['zoom'].'-'.$data['col'].'-'.$data['row'].'.jpg');
4427bbde00SAndreas Gohr    $data['cachet'] = @filemtime($data['cache']);
4527bbde00SAndreas Gohr    $data['selft']  = filemtime(__FILE__);
4627bbde00SAndreas Gohr
4727bbde00SAndreas Gohr    // (re)generate
4827bbde00SAndreas Gohr    if( ($data['cachet'] < $data['mtime']) || ($data['cachet'] < $data['selft']) ){
49dc838624SAndreas Gohr        if($conf['im_convert']){
50dc838624SAndreas Gohr            tile_im($data);
51dc838624SAndreas Gohr        }else{
5227bbde00SAndreas Gohr            tile_gd($data);
5327bbde00SAndreas Gohr        }
54dc838624SAndreas Gohr    }
5527bbde00SAndreas Gohr
5627bbde00SAndreas Gohr    // send
5727bbde00SAndreas Gohr    header('Content-type: image/jpeg');
5827bbde00SAndreas Gohr    http_conditionalRequest(max($data['mtime'],$data['selft']));
5927bbde00SAndreas Gohr    http_sendfile($data['cache']);
6027bbde00SAndreas Gohr    readfile($data['cache']);
6127bbde00SAndreas Gohr
6227bbde00SAndreas Gohr
6327bbde00SAndreas Gohr
6427bbde00SAndreas Gohr
6527bbde00SAndreas Gohr/* --------------- functions -------------------- */
6627bbde00SAndreas Gohr
6727bbde00SAndreas Gohr
6827bbde00SAndreas Gohrfunction tile_gd($d){
69dc838624SAndreas Gohr    global $conf;
70dc838624SAndreas Gohr
7127bbde00SAndreas Gohr    $img = null;
7227bbde00SAndreas Gohr    if(preg_match('/\.jpe?g$/',$d['file'])){
7327bbde00SAndreas Gohr        $img   = @imagecreatefromjpeg($d['file']);
7427bbde00SAndreas Gohr    }elseif(preg_match('/\.png$/',$d['file'])){
7527bbde00SAndreas Gohr        $img   = @imagecreatefrompng($d['file']);
7627bbde00SAndreas Gohr    }elseif(preg_match('/\.gif$/',$d['file'])){
7727bbde00SAndreas Gohr        $img   = @imagecreatefromgif($d['file']);
7827bbde00SAndreas Gohr    }
7927bbde00SAndreas Gohr    if(!$img) gfx_error('generic');
8027bbde00SAndreas Gohr
8127bbde00SAndreas Gohr    $crop  = image_crop($img,$d['width'],$d['height'],$d['tlx'],$d['tly'],$d['brx'],$d['bry']);
8227bbde00SAndreas Gohr    imagedestroy($img);
8327bbde00SAndreas Gohr
8427bbde00SAndreas Gohr    $scale = image_scale($crop,abs($d['brx'] - $d['tlx']),abs($d['bry'] - $d['tly']),$d['ts'],$d['ts']);
8527bbde00SAndreas Gohr    imagedestroy($crop);
8627bbde00SAndreas Gohr
87dc838624SAndreas Gohr    imagejpeg($scale,$d['cache'],$conf['jpg_quality']);
8827bbde00SAndreas Gohr    imagedestroy($scale);
89dc838624SAndreas Gohr
90dc838624SAndreas Gohr    if($conf['fperm']) chmod($d['cache'], $conf['fperm']);
9127bbde00SAndreas Gohr}
9227bbde00SAndreas Gohr
93dc838624SAndreas Gohrfunction tile_im($d){
94dc838624SAndreas Gohr    global $conf;
95dc838624SAndreas Gohr
96dc838624SAndreas Gohr    $cmd  = $conf['im_convert'];
97dc838624SAndreas Gohr    $cmd .= ' '.escapeshellarg($d['file']);
98a82f8f5cSAndreas Gohr    $cmd .= ' -crop \''.abs($d['brx'] - $d['tlx']).'x'.abs($d['bry'] - $d['tly']).'!+'.$d['tlx'].'+'.$d['tly'].'\'';
99a82f8f5cSAndreas Gohr    $cmd .= ' -background black';
100a82f8f5cSAndreas Gohr    $cmd .= ' -extent \''.abs($d['brx'] - $d['tlx']).'x'.abs($d['bry'] - $d['tly']).'!\'';
101a82f8f5cSAndreas Gohr    $cmd .= ' -resize \''.$d['ts'].'x'.$d['ts'].'!\'';
102dc838624SAndreas Gohr
103dc838624SAndreas Gohr    $cmd .= ' -quality '.$conf['jpg_quality'];
104dc838624SAndreas Gohr    $cmd .= ' '.escapeshellarg($d['cache']);
105dc838624SAndreas Gohr
106dc838624SAndreas Gohr#    dbg($cmd); exit;
107dc838624SAndreas Gohr
108dc838624SAndreas Gohr    @exec($cmd,$out,$retval);
109dc838624SAndreas Gohr    if ($retval == 0) return true;
110dc838624SAndreas Gohr    gfx_error('generic');
111dc838624SAndreas Gohr}
112dc838624SAndreas Gohr
113dc838624SAndreas Gohr
114dc838624SAndreas Gohr
11527bbde00SAndreas Gohrfunction image_scale($image,$x,$y,$w,$h){
11627bbde00SAndreas Gohr    $scale=imagecreatetruecolor($w,$h);
11727bbde00SAndreas Gohr    imagecopyresampled($scale,$image,0,0,0,0,$w,$h,$x,$y);
11827bbde00SAndreas Gohr    return $scale;
11927bbde00SAndreas Gohr}
12027bbde00SAndreas Gohr
12127bbde00SAndreas Gohrfunction image_crop($image,$x,$y,$left,$upper,$right,$lower) {
12227bbde00SAndreas Gohr    $w=abs($right-$left);
12327bbde00SAndreas Gohr    $h=abs($lower-$upper);
12427bbde00SAndreas Gohr    $crop = imagecreatetruecolor($w,$h);
12527bbde00SAndreas Gohr    imagecopy($crop,$image,0,0,$left,$upper,$w,$h);
12627bbde00SAndreas Gohr    return $crop;
12727bbde00SAndreas Gohr}
12827bbde00SAndreas Gohr
12927bbde00SAndreas Gohrfunction gfx_error($type){
13027bbde00SAndreas Gohr    $file = dirname(__FILE__).'/gfx/'.$type.'.gif';
13127bbde00SAndreas Gohr    $time = filemtime($file);
13227bbde00SAndreas Gohr    header('Content-type: image/gif');
13327bbde00SAndreas Gohr
13427bbde00SAndreas Gohr    http_conditionalRequest($time);
13527bbde00SAndreas Gohr    http_sendfile($file);
13627bbde00SAndreas Gohr    readfile($file);
13727bbde00SAndreas Gohr    exit;
13827bbde00SAndreas Gohr}
13927bbde00SAndreas Gohr
14027bbde00SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
141