xref: /plugin/panoview/tiles.php (revision 27bbde004b4e000a9433308c2ec2bd5417ea0989)
1*27bbde00SAndreas Gohr<?php
2*27bbde00SAndreas Gohr/**
3*27bbde00SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4*27bbde00SAndreas Gohr * @author     Andreas Gohr <gohr@cosmocode.de>
5*27bbde00SAndreas Gohr */
6*27bbde00SAndreas Gohr
7*27bbde00SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
8*27bbde00SAndreas Gohrdefine('DOKU_DISABLE_GZIP_OUTPUT', 1);
9*27bbde00SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
10*27bbde00SAndreas Gohrrequire_once(DOKU_INC.'inc/common.php');
11*27bbde00SAndreas Gohrrequire_once(DOKU_INC.'inc/pageutils.php');
12*27bbde00SAndreas Gohrrequire_once(DOKU_INC.'inc/httputils.php');
13*27bbde00SAndreas Gohrrequire_once(DOKU_INC.'inc/auth.php');
14*27bbde00SAndreas Gohrsession_write_close();
15*27bbde00SAndreas Gohr
16*27bbde00SAndreas Gohr    $data = array();
17*27bbde00SAndreas Gohr    // get parameters
18*27bbde00SAndreas Gohr    list($data['zoom'],$data['col'],$data['row']) = explode('-',$_GET['tile']);
19*27bbde00SAndreas Gohr    $data['id']    = cleanID($_GET['image']);
20*27bbde00SAndreas Gohr    $data['file']  = mediaFN($data['id']);
21*27bbde00SAndreas Gohr    $data['mtime'] = @filemtime($data['file']);
22*27bbde00SAndreas Gohr
23*27bbde00SAndreas Gohr    // check auth and existance
24*27bbde00SAndreas Gohr    if(auth_quickaclcheck(getNS($data['id']).':X') < AUTH_READ) gfx_error('noauth');
25*27bbde00SAndreas Gohr    if(!$data['mtime']) gfx_error('notfound');
26*27bbde00SAndreas Gohr
27*27bbde00SAndreas Gohr    // calculate zoom level scaling
28*27bbde00SAndreas Gohr    $data['ts']    = 256;
29*27bbde00SAndreas Gohr    list($data['width'],$data['height']) = getimagesize($data['file']);
30*27bbde00SAndreas Gohr    $data['scale'] = (int)pow(2,$data['zoom']);
31*27bbde00SAndreas Gohr    $data['max']   = max($data['width'],$data['height']);
32*27bbde00SAndreas Gohr    $data['inv']   = $data['max']  / ($data['ts'] * $data['scale']);
33*27bbde00SAndreas Gohr    if($data['inv'] < 1.0) gfx_error('maxzoom');
34*27bbde00SAndreas Gohr
35*27bbde00SAndreas Gohr    // calculate tile boundaries
36*27bbde00SAndreas Gohr    $data['tlx']   = (int) ($data['col'] * $data['ts'] * $data['inv']);
37*27bbde00SAndreas Gohr    $data['tly']   = (int) ($data['row'] * $data['ts'] * $data['inv']);
38*27bbde00SAndreas Gohr    $data['brx']   = (int) ($data['tlx'] + ($data['ts'] * $data['inv']));
39*27bbde00SAndreas Gohr    $data['bry']   = (int) ($data['tly'] + ($data['ts'] * $data['inv']));
40*27bbde00SAndreas Gohr
41*27bbde00SAndreas Gohr    // cache times
42*27bbde00SAndreas Gohr    $data['cache']  = getCacheName($data['file'],'.pv.'.$data['zoom'].'-'.$data['col'].'-'.$data['row'].'.jpg');
43*27bbde00SAndreas Gohr    $data['cachet'] = @filemtime($data['cache']);
44*27bbde00SAndreas Gohr    $data['selft']  = filemtime(__FILE__);
45*27bbde00SAndreas Gohr
46*27bbde00SAndreas Gohr    // (re)generate
47*27bbde00SAndreas Gohr    if( ($data['cachet'] < $data['mtime']) || ($data['cachet'] < $data['selft']) ){
48*27bbde00SAndreas Gohr        tile_gd($data);
49*27bbde00SAndreas Gohr    }
50*27bbde00SAndreas Gohr
51*27bbde00SAndreas Gohr    // send
52*27bbde00SAndreas Gohr    header('Content-type: image/jpeg');
53*27bbde00SAndreas Gohr    http_conditionalRequest(max($data['mtime'],$data['selft']));
54*27bbde00SAndreas Gohr    http_sendfile($data['cache']);
55*27bbde00SAndreas Gohr    readfile($data['cache']);
56*27bbde00SAndreas Gohr
57*27bbde00SAndreas Gohr
58*27bbde00SAndreas Gohr
59*27bbde00SAndreas Gohr
60*27bbde00SAndreas Gohr/* --------------- functions -------------------- */
61*27bbde00SAndreas Gohr
62*27bbde00SAndreas Gohr
63*27bbde00SAndreas Gohrfunction tile_gd($d){
64*27bbde00SAndreas Gohr    $img = null;
65*27bbde00SAndreas Gohr    if(preg_match('/\.jpe?g$/',$d['file'])){
66*27bbde00SAndreas Gohr        $img   = @imagecreatefromjpeg($d['file']);
67*27bbde00SAndreas Gohr    }elseif(preg_match('/\.png$/',$d['file'])){
68*27bbde00SAndreas Gohr        $img   = @imagecreatefrompng($d['file']);
69*27bbde00SAndreas Gohr    }elseif(preg_match('/\.gif$/',$d['file'])){
70*27bbde00SAndreas Gohr        $img   = @imagecreatefromgif($d['file']);
71*27bbde00SAndreas Gohr    }
72*27bbde00SAndreas Gohr    if(!$img) gfx_error('generic');
73*27bbde00SAndreas Gohr
74*27bbde00SAndreas Gohr    $crop  = image_crop($img,$d['width'],$d['height'],$d['tlx'],$d['tly'],$d['brx'],$d['bry']);
75*27bbde00SAndreas Gohr    imagedestroy($img);
76*27bbde00SAndreas Gohr
77*27bbde00SAndreas Gohr    $scale = image_scale($crop,abs($d['brx'] - $d['tlx']),abs($d['bry'] - $d['tly']),$d['ts'],$d['ts']);
78*27bbde00SAndreas Gohr    imagedestroy($crop);
79*27bbde00SAndreas Gohr
80*27bbde00SAndreas Gohr    imagejpeg($scale,$d['cache']);
81*27bbde00SAndreas Gohr    imagedestroy($scale);
82*27bbde00SAndreas Gohr}
83*27bbde00SAndreas Gohr
84*27bbde00SAndreas Gohrfunction image_scale($image,$x,$y,$w,$h){
85*27bbde00SAndreas Gohr    $scale=imagecreatetruecolor($w,$h);
86*27bbde00SAndreas Gohr    imagecopyresampled($scale,$image,0,0,0,0,$w,$h,$x,$y);
87*27bbde00SAndreas Gohr    return $scale;
88*27bbde00SAndreas Gohr}
89*27bbde00SAndreas Gohr
90*27bbde00SAndreas Gohrfunction image_crop($image,$x,$y,$left,$upper,$right,$lower) {
91*27bbde00SAndreas Gohr    $w=abs($right-$left);
92*27bbde00SAndreas Gohr    $h=abs($lower-$upper);
93*27bbde00SAndreas Gohr    $crop = imagecreatetruecolor($w,$h);
94*27bbde00SAndreas Gohr    imagecopy($crop,$image,0,0,$left,$upper,$w,$h);
95*27bbde00SAndreas Gohr    return $crop;
96*27bbde00SAndreas Gohr}
97*27bbde00SAndreas Gohr
98*27bbde00SAndreas Gohrfunction gfx_error($type){
99*27bbde00SAndreas Gohr    $file = dirname(__FILE__).'/gfx/'.$type.'.gif';
100*27bbde00SAndreas Gohr    $time = filemtime($file);
101*27bbde00SAndreas Gohr    header('Content-type: image/gif');
102*27bbde00SAndreas Gohr
103*27bbde00SAndreas Gohr    http_conditionalRequest($time);
104*27bbde00SAndreas Gohr    http_sendfile($file);
105*27bbde00SAndreas Gohr    readfile($file);
106*27bbde00SAndreas Gohr    exit;
107*27bbde00SAndreas Gohr}
108*27bbde00SAndreas Gohr
109*27bbde00SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
110