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'])); 4027bbde00SAndreas Gohr 4127bbde00SAndreas Gohr // cache times 4227bbde00SAndreas Gohr $data['cache'] = getCacheName($data['file'],'.pv.'.$data['zoom'].'-'.$data['col'].'-'.$data['row'].'.jpg'); 4327bbde00SAndreas Gohr $data['cachet'] = @filemtime($data['cache']); 4427bbde00SAndreas Gohr $data['selft'] = filemtime(__FILE__); 4527bbde00SAndreas Gohr 4627bbde00SAndreas Gohr // (re)generate 4727bbde00SAndreas Gohr if( ($data['cachet'] < $data['mtime']) || ($data['cachet'] < $data['selft']) ){ 48dc838624SAndreas Gohr if($conf['im_convert']){ 49dc838624SAndreas Gohr tile_im($data); 50dc838624SAndreas Gohr }else{ 5127bbde00SAndreas Gohr tile_gd($data); 5227bbde00SAndreas Gohr } 53dc838624SAndreas Gohr } 5427bbde00SAndreas Gohr 5527bbde00SAndreas Gohr // send 5627bbde00SAndreas Gohr header('Content-type: image/jpeg'); 5727bbde00SAndreas Gohr http_conditionalRequest(max($data['mtime'],$data['selft'])); 5827bbde00SAndreas Gohr http_sendfile($data['cache']); 5927bbde00SAndreas Gohr readfile($data['cache']); 6027bbde00SAndreas Gohr 6127bbde00SAndreas Gohr 6227bbde00SAndreas Gohr 6327bbde00SAndreas Gohr 6427bbde00SAndreas Gohr/* --------------- functions -------------------- */ 6527bbde00SAndreas Gohr 6627bbde00SAndreas Gohr 6727bbde00SAndreas Gohrfunction tile_gd($d){ 68dc838624SAndreas Gohr global $conf; 69dc838624SAndreas Gohr 7027bbde00SAndreas Gohr $img = null; 7127bbde00SAndreas Gohr if(preg_match('/\.jpe?g$/',$d['file'])){ 7227bbde00SAndreas Gohr $img = @imagecreatefromjpeg($d['file']); 7327bbde00SAndreas Gohr }elseif(preg_match('/\.png$/',$d['file'])){ 7427bbde00SAndreas Gohr $img = @imagecreatefrompng($d['file']); 7527bbde00SAndreas Gohr }elseif(preg_match('/\.gif$/',$d['file'])){ 7627bbde00SAndreas Gohr $img = @imagecreatefromgif($d['file']); 7727bbde00SAndreas Gohr } 7827bbde00SAndreas Gohr if(!$img) gfx_error('generic'); 7927bbde00SAndreas Gohr 8027bbde00SAndreas Gohr $crop = image_crop($img,$d['width'],$d['height'],$d['tlx'],$d['tly'],$d['brx'],$d['bry']); 8127bbde00SAndreas Gohr imagedestroy($img); 8227bbde00SAndreas Gohr 8327bbde00SAndreas Gohr $scale = image_scale($crop,abs($d['brx'] - $d['tlx']),abs($d['bry'] - $d['tly']),$d['ts'],$d['ts']); 8427bbde00SAndreas Gohr imagedestroy($crop); 8527bbde00SAndreas Gohr 86dc838624SAndreas Gohr imagejpeg($scale,$d['cache'],$conf['jpg_quality']); 8727bbde00SAndreas Gohr imagedestroy($scale); 88dc838624SAndreas Gohr 89dc838624SAndreas Gohr if($conf['fperm']) chmod($d['cache'], $conf['fperm']); 9027bbde00SAndreas Gohr} 9127bbde00SAndreas Gohr 92dc838624SAndreas Gohrfunction tile_im($d){ 93dc838624SAndreas Gohr global $conf; 94dc838624SAndreas Gohr 95dc838624SAndreas Gohr $cmd = $conf['im_convert']; 96dc838624SAndreas Gohr $cmd .= ' '.escapeshellarg($d['file']); 97*a82f8f5cSAndreas Gohr $cmd .= ' -crop \''.abs($d['brx'] - $d['tlx']).'x'.abs($d['bry'] - $d['tly']).'!+'.$d['tlx'].'+'.$d['tly'].'\''; 98*a82f8f5cSAndreas Gohr $cmd .= ' -background black'; 99*a82f8f5cSAndreas Gohr $cmd .= ' -extent \''.abs($d['brx'] - $d['tlx']).'x'.abs($d['bry'] - $d['tly']).'!\''; 100*a82f8f5cSAndreas Gohr $cmd .= ' -resize \''.$d['ts'].'x'.$d['ts'].'!\''; 101dc838624SAndreas Gohr 102dc838624SAndreas Gohr $cmd .= ' -quality '.$conf['jpg_quality']; 103dc838624SAndreas Gohr $cmd .= ' '.escapeshellarg($d['cache']); 104dc838624SAndreas Gohr 105dc838624SAndreas Gohr# dbg($cmd); exit; 106dc838624SAndreas Gohr 107dc838624SAndreas Gohr @exec($cmd,$out,$retval); 108dc838624SAndreas Gohr if ($retval == 0) return true; 109dc838624SAndreas Gohr gfx_error('generic'); 110dc838624SAndreas Gohr} 111dc838624SAndreas Gohr 112dc838624SAndreas Gohr 113dc838624SAndreas Gohr 11427bbde00SAndreas Gohrfunction image_scale($image,$x,$y,$w,$h){ 11527bbde00SAndreas Gohr $scale=imagecreatetruecolor($w,$h); 11627bbde00SAndreas Gohr imagecopyresampled($scale,$image,0,0,0,0,$w,$h,$x,$y); 11727bbde00SAndreas Gohr return $scale; 11827bbde00SAndreas Gohr} 11927bbde00SAndreas Gohr 12027bbde00SAndreas Gohrfunction image_crop($image,$x,$y,$left,$upper,$right,$lower) { 12127bbde00SAndreas Gohr $w=abs($right-$left); 12227bbde00SAndreas Gohr $h=abs($lower-$upper); 12327bbde00SAndreas Gohr $crop = imagecreatetruecolor($w,$h); 12427bbde00SAndreas Gohr imagecopy($crop,$image,0,0,$left,$upper,$w,$h); 12527bbde00SAndreas Gohr return $crop; 12627bbde00SAndreas Gohr} 12727bbde00SAndreas Gohr 12827bbde00SAndreas Gohrfunction gfx_error($type){ 12927bbde00SAndreas Gohr $file = dirname(__FILE__).'/gfx/'.$type.'.gif'; 13027bbde00SAndreas Gohr $time = filemtime($file); 13127bbde00SAndreas Gohr header('Content-type: image/gif'); 13227bbde00SAndreas Gohr 13327bbde00SAndreas Gohr http_conditionalRequest($time); 13427bbde00SAndreas Gohr http_sendfile($file); 13527bbde00SAndreas Gohr readfile($file); 13627bbde00SAndreas Gohr exit; 13727bbde00SAndreas Gohr} 13827bbde00SAndreas Gohr 13927bbde00SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 : 140