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