xref: /dokuwiki/lib/exe/fetch.php (revision d52a56e1b845c0c3835ff216f1d54a636e33bba9)
1f62ea8a1Sandi<?php
2f62ea8a1Sandi/**
3f62ea8a1Sandi * DokuWiki media passthrough file
4f62ea8a1Sandi *
5f62ea8a1Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6f62ea8a1Sandi * @author     Andreas Gohr <andi@splitbrain.org>
7f62ea8a1Sandi */
8f62ea8a1Sandi
9d0a27cb0SAndreas Gohr  if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
103138b5c7SAndreas Gohr  define('DOKU_DISABLE_GZIP_OUTPUT', 1);
11f62ea8a1Sandi  require_once(DOKU_INC.'inc/init.php');
12f62ea8a1Sandi  require_once(DOKU_INC.'inc/common.php');
13f62ea8a1Sandi  require_once(DOKU_INC.'inc/pageutils.php');
14f62ea8a1Sandi  require_once(DOKU_INC.'inc/confutils.php');
15f62ea8a1Sandi  require_once(DOKU_INC.'inc/auth.php');
168746e727Sandi  //close sesseion
178746e727Sandi  session_write_close();
18e935fb4aSAndreas Gohr  if(!defined('CHUNK_SIZE')) define('CHUNK_SIZE',16*1024);
198746e727Sandi
20f62ea8a1Sandi  $mimetypes = getMimeTypes();
21f62ea8a1Sandi
22f62ea8a1Sandi  //get input
2302b0b681SAndreas Gohr  $MEDIA  = stripctl(getID('media',false)); // no cleaning except control chars - maybe external
24f62ea8a1Sandi  $CACHE  = calc_cache($_REQUEST['cache']);
258fcc3410SAndreas Gohr  $WIDTH  = (int) $_REQUEST['w'];
268fcc3410SAndreas Gohr  $HEIGHT = (int) $_REQUEST['h'];
27f62ea8a1Sandi  list($EXT,$MIME) = mimetype($MEDIA);
28f62ea8a1Sandi  if($EXT === false){
29f62ea8a1Sandi    $EXT  = 'unknown';
30f62ea8a1Sandi    $MIME = 'application/octet-stream';
31f62ea8a1Sandi  }
32f62ea8a1Sandi
33f62ea8a1Sandi  //media to local file
34d1ed0b61SAndreas Gohr  if(preg_match('#^(https?)://#i',$MEDIA)){
35d1ed0b61SAndreas Gohr    //handle external images
36d1ed0b61SAndreas Gohr    if(strncmp($MIME,'image/',6) == 0) $FILE = get_from_URL($MEDIA,$EXT,$CACHE);
37f62ea8a1Sandi    if(!$FILE){
38f62ea8a1Sandi      //download failed - redirect to original URL
39f62ea8a1Sandi      header('Location: '.$MEDIA);
40f62ea8a1Sandi      exit;
41f62ea8a1Sandi    }
42f62ea8a1Sandi  }else{
43f62ea8a1Sandi    $MEDIA = cleanID($MEDIA);
44f62ea8a1Sandi    if(empty($MEDIA)){
45f62ea8a1Sandi      header("HTTP/1.0 400 Bad Request");
46f62ea8a1Sandi      print 'Bad request';
47f62ea8a1Sandi      exit;
48f62ea8a1Sandi    }
49f62ea8a1Sandi
50f62ea8a1Sandi    //check permissions (namespace only)
51f62ea8a1Sandi    if(auth_quickaclcheck(getNS($MEDIA).':X') < AUTH_READ){
52f62ea8a1Sandi      header("HTTP/1.0 401 Unauthorized");
53f62ea8a1Sandi      //fixme add some image for imagefiles
54f62ea8a1Sandi      print 'Unauthorized';
55f62ea8a1Sandi      exit;
56f62ea8a1Sandi    }
57f62ea8a1Sandi    $FILE  = mediaFN($MEDIA);
58f62ea8a1Sandi  }
59f62ea8a1Sandi
60f62ea8a1Sandi  //check file existance
61f62ea8a1Sandi  if(!@file_exists($FILE)){
62f62ea8a1Sandi    header("HTTP/1.0 404 Not Found");
63f62ea8a1Sandi    //FIXME add some default broken image
64f62ea8a1Sandi    print 'Not Found';
65f62ea8a1Sandi    exit;
66f62ea8a1Sandi  }
67f62ea8a1Sandi
6820bc86cfSAndreas Gohr  //handle image resizing/cropping
69f62ea8a1Sandi  if((substr($MIME,0,5) == 'image') && $WIDTH){
70*d52a56e1SAndreas Gohr    if($HEIGHT){
7120bc86cfSAndreas Gohr        $FILE = get_cropped($FILE,$EXT,$WIDTH,$HEIGHT);
7220bc86cfSAndreas Gohr    }else{
73f62ea8a1Sandi        $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT);
74f62ea8a1Sandi    }
7520bc86cfSAndreas Gohr  }
76f62ea8a1Sandi
77e935fb4aSAndreas Gohr  // finally send the file to the client
7883730152SBen Coburn  sendFile($FILE,$MIME,$CACHE);
79f62ea8a1Sandi
80e935fb4aSAndreas Gohr/* ------------------------------------------------------------------------ */
81f62ea8a1Sandi
82e935fb4aSAndreas Gohr/**
83e935fb4aSAndreas Gohr * Set headers and send the file to the client
84e935fb4aSAndreas Gohr *
85e935fb4aSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
8683730152SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
87e935fb4aSAndreas Gohr */
8883730152SBen Coburnfunction sendFile($file,$mime,$cache){
8983730152SBen Coburn  global $conf;
9083730152SBen Coburn  $fmtime = filemtime($file);
91e935fb4aSAndreas Gohr  // send headers
92e935fb4aSAndreas Gohr  header("Content-Type: $mime");
9383730152SBen Coburn  // smart http caching headers
9483730152SBen Coburn  if ($cache==-1) {
9583730152SBen Coburn    // cache
9683730152SBen Coburn    // cachetime or one hour
9783730152SBen Coburn    header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
9883730152SBen Coburn    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
99e935fb4aSAndreas Gohr    header('Pragma: public');
10083730152SBen Coburn  } else if ($cache>0) {
10183730152SBen Coburn    // recache
10283730152SBen Coburn    // remaining cachetime + 10 seconds so the newly recached media is used
10383730152SBen Coburn    header('Expires: '.gmdate("D, d M Y H:i:s", $fmtime+$conf['cachetime']+10).' GMT');
10483730152SBen Coburn    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($fmtime-time()+$conf['cachetime']+10, 0));
10583730152SBen Coburn    header('Pragma: public');
10683730152SBen Coburn  } else if ($cache==0) {
10783730152SBen Coburn    // nocache
10883730152SBen Coburn    header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
10983730152SBen Coburn    header('Pragma: public');
11083730152SBen Coburn  }
111ff4f5ee7SBen Coburn  //send important headers first, script stops here if '304 Not Modified' response
11283730152SBen Coburn  http_conditionalRequest($fmtime);
1139a87c72aSAndreas Gohr
114f62ea8a1Sandi
115f62ea8a1Sandi  //application mime type is downloadable
116e935fb4aSAndreas Gohr  if(substr($mime,0,11) == 'application'){
117e935fb4aSAndreas Gohr    header('Content-Disposition: attachment; filename="'.basename($file).'";');
118f62ea8a1Sandi  }
119f62ea8a1Sandi
1209a87c72aSAndreas Gohr  //use x-sendfile header to pass the delivery to compatible webservers
1219a87c72aSAndreas Gohr  if($conf['xsendfile'] == 1){
1229a87c72aSAndreas Gohr    header("X-LIGHTTPD-send-file: $file");
1239a87c72aSAndreas Gohr    exit;
1249a87c72aSAndreas Gohr  }elseif($conf['xsendfile'] == 2){
1259a87c72aSAndreas Gohr    header("X-Sendfile: $file");
1269a87c72aSAndreas Gohr    exit;
127deec6eb9Spierre.pracht  }elseif($conf['xsendfile'] == 3){
128deec6eb9Spierre.pracht    header("X-Accel-Redirect: $file");
129deec6eb9Spierre.pracht    exit;
1309a87c72aSAndreas Gohr  }
1319a87c72aSAndreas Gohr
1329a87c72aSAndreas Gohr  //support download continueing
1339a87c72aSAndreas Gohr  header('Accept-Ranges: bytes');
1349a87c72aSAndreas Gohr  list($start,$len) = http_rangeRequest(filesize($file));
1359a87c72aSAndreas Gohr
136e935fb4aSAndreas Gohr  // send file contents
137e935fb4aSAndreas Gohr  $fp = @fopen($file,"rb");
138f62ea8a1Sandi  if($fp){
139e935fb4aSAndreas Gohr    fseek($fp,$start); //seek to start of range
140e935fb4aSAndreas Gohr
141e935fb4aSAndreas Gohr    $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len;
142e935fb4aSAndreas Gohr    while (!feof($fp) && $chunk > 0) {
143d6751ba5SAndreas Gohr      @set_time_limit(30); // large files can take a lot of time
144e935fb4aSAndreas Gohr      print fread($fp, $chunk);
145615a21edSBrian Cowan      flush();
146e935fb4aSAndreas Gohr      $len -= $chunk;
147e935fb4aSAndreas Gohr      $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len;
148615a21edSBrian Cowan    }
149615a21edSBrian Cowan    fclose($fp);
150f62ea8a1Sandi  }else{
151f62ea8a1Sandi    header("HTTP/1.0 500 Internal Server Error");
152e935fb4aSAndreas Gohr    print "Could not read $file - bad permissions?";
153e935fb4aSAndreas Gohr  }
154f62ea8a1Sandi}
155f62ea8a1Sandi
156e935fb4aSAndreas Gohr/**
157e935fb4aSAndreas Gohr * Checks and sets headers to handle range requets
158e935fb4aSAndreas Gohr *
159e935fb4aSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
160e935fb4aSAndreas Gohr * @returns array The start byte and the amount of bytes to send
161e935fb4aSAndreas Gohr */
162e935fb4aSAndreas Gohrfunction http_rangeRequest($size){
163e935fb4aSAndreas Gohr  if(!isset($_SERVER['HTTP_RANGE'])){
164e935fb4aSAndreas Gohr    // no range requested - send the whole file
165e935fb4aSAndreas Gohr    header("Content-Length: $size");
166e935fb4aSAndreas Gohr    return array(0,$size);
167e935fb4aSAndreas Gohr  }
168e935fb4aSAndreas Gohr
169e935fb4aSAndreas Gohr  $t = explode('=', $_SERVER['HTTP_RANGE']);
170e935fb4aSAndreas Gohr  if (!$t[0]=='bytes') {
171e935fb4aSAndreas Gohr    // we only understand byte ranges - send the whole file
172e935fb4aSAndreas Gohr    header("Content-Length: $size");
173e935fb4aSAndreas Gohr    return array(0,$size);
174e935fb4aSAndreas Gohr  }
175e935fb4aSAndreas Gohr
176e935fb4aSAndreas Gohr  $r = explode('-', $t[1]);
177e935fb4aSAndreas Gohr  $start = (int)$r[0];
178e935fb4aSAndreas Gohr  $end = (int)$r[1];
179e935fb4aSAndreas Gohr  if (!$end) $end = $size - 1;
180e935fb4aSAndreas Gohr  if ($start > $end || $start > $size || $end > $size){
181e935fb4aSAndreas Gohr    header('HTTP/1.1 416 Requested Range Not Satisfiable');
182e935fb4aSAndreas Gohr    print 'Bad Range Request!';
183e935fb4aSAndreas Gohr    exit;
184e935fb4aSAndreas Gohr  }
185e935fb4aSAndreas Gohr
186e935fb4aSAndreas Gohr  $tot = $end - $start + 1;
187e935fb4aSAndreas Gohr  header('HTTP/1.1 206 Partial Content');
188e935fb4aSAndreas Gohr  header("Content-Range: bytes {$start}-{$end}/{$size}");
189e935fb4aSAndreas Gohr  header("Content-Length: $tot");
190e935fb4aSAndreas Gohr
191e935fb4aSAndreas Gohr  return array($start,$tot);
192e935fb4aSAndreas Gohr}
193e935fb4aSAndreas Gohr
194e935fb4aSAndreas Gohr/**
195f62ea8a1Sandi * Resizes the given image to the given size
196f62ea8a1Sandi *
197f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
198f62ea8a1Sandi */
199f62ea8a1Sandifunction get_resized($file, $ext, $w, $h=0){
200f62ea8a1Sandi  global $conf;
201f62ea8a1Sandi
202f62ea8a1Sandi  $info  = getimagesize($file);
203f62ea8a1Sandi  if(!$h) $h = round(($w * $info[1]) / $info[0]);
204f62ea8a1Sandi
2058fcc3410SAndreas Gohr  // we wont scale up to infinity
2068fcc3410SAndreas Gohr  if($w > 2000 || $h > 2000) return $file;
207f62ea8a1Sandi
208f62ea8a1Sandi  //cache
20998407a7aSandi  $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext);
210f62ea8a1Sandi  $mtime = @filemtime($local); // 0 if not exists
211f62ea8a1Sandi
21268375754SPavel Vitis  if( $mtime > filemtime($file) ||
21368375754SPavel Vitis      resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) ||
21468375754SPavel Vitis      resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){
21528d5e270SOtto Vainio    if($conf['fperm']) chmod($local, $conf['fperm']);
216f62ea8a1Sandi    return $local;
217f62ea8a1Sandi  }
218f62ea8a1Sandi  //still here? resizing failed
219f62ea8a1Sandi  return $file;
220f62ea8a1Sandi}
221f62ea8a1Sandi
222f62ea8a1Sandi/**
22320bc86cfSAndreas Gohr * Crops the given image to the wanted ratio, then calls get_resized to scale it
22420bc86cfSAndreas Gohr * to the wanted size
22520bc86cfSAndreas Gohr *
226a7ead82dSAndreas Gohr * Crops are centered horizontally but prefer the upper third of an vertical
227a7ead82dSAndreas Gohr * image because most pics are more interesting in that area (rule of thirds)
228a7ead82dSAndreas Gohr *
22920bc86cfSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
23020bc86cfSAndreas Gohr */
23120bc86cfSAndreas Gohrfunction get_cropped($file, $ext, $w, $h=0){
23220bc86cfSAndreas Gohr  global $conf;
23320bc86cfSAndreas Gohr
23420bc86cfSAndreas Gohr  if(!$h) $h = $w;
23520bc86cfSAndreas Gohr  $info = getimagesize($file); //get original size
23620bc86cfSAndreas Gohr
23720bc86cfSAndreas Gohr  // calculate crop size
23820bc86cfSAndreas Gohr  $fr = $info[0]/$info[1];
23920bc86cfSAndreas Gohr  $tr = $w/$h;
24020bc86cfSAndreas Gohr  if($tr >= 1){
24120bc86cfSAndreas Gohr    if($tr > $fr){
24220bc86cfSAndreas Gohr        $cw = $info[0];
24320bc86cfSAndreas Gohr        $ch = (int) $info[0]/$tr;
24420bc86cfSAndreas Gohr    }else{
24520bc86cfSAndreas Gohr        $cw = (int) $info[1]*$tr;
24620bc86cfSAndreas Gohr        $ch = $info[1];
24720bc86cfSAndreas Gohr    }
24820bc86cfSAndreas Gohr  }else{
24920bc86cfSAndreas Gohr    if($tr < $fr){
25020bc86cfSAndreas Gohr        $cw = (int) $info[1]*$tr;
25120bc86cfSAndreas Gohr        $ch = $info[1];
25220bc86cfSAndreas Gohr    }else{
25320bc86cfSAndreas Gohr        $cw = $info[0];
25420bc86cfSAndreas Gohr        $ch = (int) $info[0]/$tr;
25520bc86cfSAndreas Gohr    }
25620bc86cfSAndreas Gohr  }
25720bc86cfSAndreas Gohr  // calculate crop offset
25820bc86cfSAndreas Gohr  $cx = (int) ($info[0]-$cw)/2;
259a7ead82dSAndreas Gohr  $cy = (int) ($info[1]-$ch)/3;
26020bc86cfSAndreas Gohr
26120bc86cfSAndreas Gohr  //cache
26220bc86cfSAndreas Gohr  $local = getCacheName($file,'.media.'.$cw.'x'.$ch.'.crop.'.$ext);
26320bc86cfSAndreas Gohr  $mtime = @filemtime($local); // 0 if not exists
26420bc86cfSAndreas Gohr
26520bc86cfSAndreas Gohr  if( $mtime > filemtime($file) ||
26620bc86cfSAndreas Gohr      crop_imageIM($ext,$file,$info[0],$info[1],$local,$cw,$ch,$cx,$cy) ||
26720bc86cfSAndreas Gohr      resize_imageGD($ext,$file,$cw,$ch,$local,$cw,$ch,$cx,$cy) ){
26820bc86cfSAndreas Gohr    if($conf['fperm']) chmod($local, $conf['fperm']);
26920bc86cfSAndreas Gohr    return get_resized($local,$ext, $w, $h);
27020bc86cfSAndreas Gohr  }
27120bc86cfSAndreas Gohr
27220bc86cfSAndreas Gohr  //still here? cropping failed
27320bc86cfSAndreas Gohr  return get_resized($file,$ext, $w, $h);
27420bc86cfSAndreas Gohr}
27520bc86cfSAndreas Gohr
27620bc86cfSAndreas Gohr
27720bc86cfSAndreas Gohr/**
278f62ea8a1Sandi * Returns the wanted cachetime in seconds
279f62ea8a1Sandi *
280f62ea8a1Sandi * Resolves named constants
281f62ea8a1Sandi *
282f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
283f62ea8a1Sandi */
284f62ea8a1Sandifunction calc_cache($cache){
285f62ea8a1Sandi  global $conf;
286f62ea8a1Sandi
287f62ea8a1Sandi  if(strtolower($cache) == 'nocache') return 0; //never cache
288f62ea8a1Sandi  if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
289f62ea8a1Sandi  return -1; //cache endless
290f62ea8a1Sandi}
291f62ea8a1Sandi
292f62ea8a1Sandi/**
293f62ea8a1Sandi * Download a remote file and return local filename
294f62ea8a1Sandi *
295f62ea8a1Sandi * returns false if download fails. Uses cached file if available and
296f62ea8a1Sandi * wanted
297f62ea8a1Sandi *
298f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
29968375754SPavel Vitis * @author  Pavel Vitis <Pavel.Vitis@seznam.cz>
300f62ea8a1Sandi */
301f62ea8a1Sandifunction get_from_URL($url,$ext,$cache){
302f62ea8a1Sandi  global $conf;
303f62ea8a1Sandi
304847b8298SAndreas Gohr  // if no cache or fetchsize just redirect
305847b8298SAndreas Gohr  if ($cache==0)           return false;
306847b8298SAndreas Gohr  if (!$conf['fetchsize']) return false;
3074f3c4962SBen Coburn
30868375754SPavel Vitis  $local = getCacheName(strtolower($url),".media.$ext");
309f62ea8a1Sandi  $mtime = @filemtime($local); // 0 if not exists
310f62ea8a1Sandi
311f62ea8a1Sandi  //decide if download needed:
3124f3c4962SBen Coburn  if( ($mtime == 0) ||                           // cache does not exist
3134f3c4962SBen Coburn      ($cache != -1 && $mtime < time()-$cache)   // 'recache' and cache has expired
31468375754SPavel Vitis    ){
315894a80ccSAndreas Gohr      if(image_download($url,$local)){
316f62ea8a1Sandi        return $local;
317f62ea8a1Sandi      }else{
318f62ea8a1Sandi        return false;
319f62ea8a1Sandi      }
320f62ea8a1Sandi  }
321f62ea8a1Sandi
322f62ea8a1Sandi  //if cache exists use it else
323f62ea8a1Sandi  if($mtime) return $local;
324f62ea8a1Sandi
325f62ea8a1Sandi  //else return false
326f62ea8a1Sandi  return false;
327f62ea8a1Sandi}
328f62ea8a1Sandi
329f62ea8a1Sandi/**
330894a80ccSAndreas Gohr * Download image files
331894a80ccSAndreas Gohr *
332894a80ccSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
333894a80ccSAndreas Gohr */
334894a80ccSAndreas Gohrfunction image_download($url,$file){
335894a80ccSAndreas Gohr  global $conf;
336894a80ccSAndreas Gohr  $http = new DokuHTTPClient();
337894a80ccSAndreas Gohr  $http->max_bodysize = $conf['fetchsize'];
338894a80ccSAndreas Gohr  $http->timeout = 25; //max. 25 sec
339894a80ccSAndreas Gohr  $http->header_regexp = '!\r\nContent-Type: image/(jpe?g|gif|png)!i';
340894a80ccSAndreas Gohr
341894a80ccSAndreas Gohr  $data = $http->get($url);
342894a80ccSAndreas Gohr  if(!$data) return false;
343894a80ccSAndreas Gohr
344894a80ccSAndreas Gohr  $fileexists = @file_exists($file);
345894a80ccSAndreas Gohr  $fp = @fopen($file,"w");
346894a80ccSAndreas Gohr  if(!$fp) return false;
347894a80ccSAndreas Gohr  fwrite($fp,$data);
348894a80ccSAndreas Gohr  fclose($fp);
349894a80ccSAndreas Gohr  if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
350d1ed0b61SAndreas Gohr
351d1ed0b61SAndreas Gohr  // check if it is really an image
352d1ed0b61SAndreas Gohr  $info = @getimagesize($file);
353d1ed0b61SAndreas Gohr  if(!$info){
354d1ed0b61SAndreas Gohr    @unlink($file);
355d1ed0b61SAndreas Gohr    return false;
356d1ed0b61SAndreas Gohr  }
357d1ed0b61SAndreas Gohr
358894a80ccSAndreas Gohr  return true;
359894a80ccSAndreas Gohr}
360894a80ccSAndreas Gohr
361894a80ccSAndreas Gohr/**
36268375754SPavel Vitis * resize images using external ImageMagick convert program
36368375754SPavel Vitis *
36468375754SPavel Vitis * @author Pavel Vitis <Pavel.Vitis@seznam.cz>
36568375754SPavel Vitis * @author Andreas Gohr <andi@splitbrain.org>
36668375754SPavel Vitis */
36768375754SPavel Vitisfunction resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
36868375754SPavel Vitis  global $conf;
36968375754SPavel Vitis
3707bc7a78eSAndreas Gohr  // check if convert is configured
3717bc7a78eSAndreas Gohr  if(!$conf['im_convert']) return false;
37268375754SPavel Vitis
37368375754SPavel Vitis  // prepare command
37468375754SPavel Vitis  $cmd  = $conf['im_convert'];
37568375754SPavel Vitis  $cmd .= ' -resize '.$to_w.'x'.$to_h.'!';
3762b03e74dSBen Coburn  if ($ext == 'jpg' || $ext == 'jpeg') {
3772b03e74dSBen Coburn      $cmd .= ' -quality '.$conf['jpg_quality'];
3782b03e74dSBen Coburn  }
37968375754SPavel Vitis  $cmd .= " $from $to";
38068375754SPavel Vitis
38168375754SPavel Vitis  @exec($cmd,$out,$retval);
38268375754SPavel Vitis  if ($retval == 0) return true;
38368375754SPavel Vitis  return false;
38468375754SPavel Vitis}
38568375754SPavel Vitis
38668375754SPavel Vitis/**
38720bc86cfSAndreas Gohr * crop images using external ImageMagick convert program
38820bc86cfSAndreas Gohr *
38920bc86cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
39020bc86cfSAndreas Gohr */
39120bc86cfSAndreas Gohrfunction crop_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x,$ofs_y){
39220bc86cfSAndreas Gohr  global $conf;
393681eb61bSAndreas Gohr
39420bc86cfSAndreas Gohr  // check if convert is configured
39520bc86cfSAndreas Gohr  if(!$conf['im_convert']) return false;
39620bc86cfSAndreas Gohr
39720bc86cfSAndreas Gohr  // prepare command
39820bc86cfSAndreas Gohr  $cmd  = $conf['im_convert'];
39920bc86cfSAndreas Gohr  $cmd .= ' -crop '.$to_w.'x'.$to_h.'+'.$ofs_x.'+'.$ofs_y;
40020bc86cfSAndreas Gohr  if ($ext == 'jpg' || $ext == 'jpeg') {
40120bc86cfSAndreas Gohr      $cmd .= ' -quality '.$conf['jpg_quality'];
40220bc86cfSAndreas Gohr  }
40320bc86cfSAndreas Gohr  $cmd .= " $from $to";
40420bc86cfSAndreas Gohr
40520bc86cfSAndreas Gohr  @exec($cmd,$out,$retval);
40620bc86cfSAndreas Gohr  if ($retval == 0) return true;
40720bc86cfSAndreas Gohr  return false;
40820bc86cfSAndreas Gohr}
40920bc86cfSAndreas Gohr
41020bc86cfSAndreas Gohr/**
41120bc86cfSAndreas Gohr * resize or crop images using PHP's libGD support
412f62ea8a1Sandi *
413f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org>
414e582e8b6Ss_wienecke * @author Sebastian Wienecke <s_wienecke@web.de>
415f62ea8a1Sandi */
41620bc86cfSAndreas Gohrfunction resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x=0,$ofs_y=0){
417f62ea8a1Sandi  global $conf;
418f62ea8a1Sandi
419f62ea8a1Sandi  if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
420f62ea8a1Sandi
4214e406776SAndreas Gohr  // check available memory
4224e406776SAndreas Gohr  if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){
4234e406776SAndreas Gohr    return false;
4244e406776SAndreas Gohr  }
4254e406776SAndreas Gohr
426f62ea8a1Sandi  // create an image of the given filetype
427f62ea8a1Sandi  if ($ext == 'jpg' || $ext == 'jpeg'){
428f62ea8a1Sandi    if(!function_exists("imagecreatefromjpeg")) return false;
429f62ea8a1Sandi    $image = @imagecreatefromjpeg($from);
430f62ea8a1Sandi  }elseif($ext == 'png') {
431f62ea8a1Sandi    if(!function_exists("imagecreatefrompng")) return false;
432f62ea8a1Sandi    $image = @imagecreatefrompng($from);
433f62ea8a1Sandi
434f62ea8a1Sandi  }elseif($ext == 'gif') {
435f62ea8a1Sandi    if(!function_exists("imagecreatefromgif")) return false;
436f62ea8a1Sandi    $image = @imagecreatefromgif($from);
437f62ea8a1Sandi  }
438f62ea8a1Sandi  if(!$image) return false;
439f62ea8a1Sandi
440e582e8b6Ss_wienecke  if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor") && $ext != 'gif'){
441f62ea8a1Sandi    $newimg = @imagecreatetruecolor ($to_w, $to_h);
442f62ea8a1Sandi  }
443f62ea8a1Sandi  if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
444dd7bbbf4SAndreas Gohr  if(!$newimg){
445dd7bbbf4SAndreas Gohr    imagedestroy($image);
446dd7bbbf4SAndreas Gohr    return false;
447dd7bbbf4SAndreas Gohr  }
448f62ea8a1Sandi
449f62ea8a1Sandi  //keep png alpha channel if possible
450f62ea8a1Sandi  if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){
451f62ea8a1Sandi    imagealphablending($newimg, false);
452f62ea8a1Sandi    imagesavealpha($newimg,true);
453f62ea8a1Sandi  }
454f62ea8a1Sandi
455e582e8b6Ss_wienecke  //keep gif transparent color if possible
456e582e8b6Ss_wienecke  if($ext == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) {
457e582e8b6Ss_wienecke    if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
458e582e8b6Ss_wienecke      $transcolorindex = @imagecolortransparent($image);
459e582e8b6Ss_wienecke      if($transcolorindex >= 0 ) { //transparent color exists
460e582e8b6Ss_wienecke        $transcolor = @imagecolorsforindex($image, $transcolorindex);
461e582e8b6Ss_wienecke        $transcolorindex = @imagecolorallocate($newimg, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
462e582e8b6Ss_wienecke        @imagefill($newimg, 0, 0, $transcolorindex);
463e582e8b6Ss_wienecke        @imagecolortransparent($newimg, $transcolorindex);
464e582e8b6Ss_wienecke      }else{ //filling with white
465e582e8b6Ss_wienecke        $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
466e582e8b6Ss_wienecke        @imagefill($newimg, 0, 0, $whitecolorindex);
467e582e8b6Ss_wienecke      }
468e582e8b6Ss_wienecke    }else{ //filling with white
469e582e8b6Ss_wienecke      $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
470e582e8b6Ss_wienecke      @imagefill($newimg, 0, 0, $whitecolorindex);
471e582e8b6Ss_wienecke    }
472e582e8b6Ss_wienecke  }
473e582e8b6Ss_wienecke
474f62ea8a1Sandi  //try resampling first
475f62ea8a1Sandi  if(function_exists("imagecopyresampled")){
47620bc86cfSAndreas Gohr    if(!@imagecopyresampled($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h)) {
47720bc86cfSAndreas Gohr      imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
478f62ea8a1Sandi    }
479f62ea8a1Sandi  }else{
48020bc86cfSAndreas Gohr    imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
481f62ea8a1Sandi  }
482f62ea8a1Sandi
483dd7bbbf4SAndreas Gohr  $okay = false;
484f62ea8a1Sandi  if ($ext == 'jpg' || $ext == 'jpeg'){
485dd7bbbf4SAndreas Gohr    if(!function_exists('imagejpeg')){
486dd7bbbf4SAndreas Gohr      $okay = false;
487dd7bbbf4SAndreas Gohr    }else{
4882b03e74dSBen Coburn      $okay = imagejpeg($newimg, $to, $conf['jpg_quality']);
489dd7bbbf4SAndreas Gohr    }
490f62ea8a1Sandi  }elseif($ext == 'png') {
491dd7bbbf4SAndreas Gohr    if(!function_exists('imagepng')){
492dd7bbbf4SAndreas Gohr      $okay = false;
493dd7bbbf4SAndreas Gohr    }else{
494dd7bbbf4SAndreas Gohr      $okay =  imagepng($newimg, $to);
495dd7bbbf4SAndreas Gohr    }
496f62ea8a1Sandi  }elseif($ext == 'gif') {
497dd7bbbf4SAndreas Gohr    if(!function_exists('imagegif')){
498dd7bbbf4SAndreas Gohr      $okay = false;
499dd7bbbf4SAndreas Gohr    }else{
500dd7bbbf4SAndreas Gohr      $okay = imagegif($newimg, $to);
501dd7bbbf4SAndreas Gohr    }
502f62ea8a1Sandi  }
503f62ea8a1Sandi
504dd7bbbf4SAndreas Gohr  // destroy GD image ressources
505dd7bbbf4SAndreas Gohr  if($image) imagedestroy($image);
506dd7bbbf4SAndreas Gohr  if($newimg) imagedestroy($newimg);
507dd7bbbf4SAndreas Gohr
508dd7bbbf4SAndreas Gohr  return $okay;
509f62ea8a1Sandi}
510f62ea8a1Sandi
5114e406776SAndreas Gohr/**
5124e406776SAndreas Gohr * Checks if the given amount of memory is available
5134e406776SAndreas Gohr *
5144e406776SAndreas Gohr * If the memory_get_usage() function is not available the
51573038c47SAndreas Gohr * function just assumes $bytes of already allocated memory
5164e406776SAndreas Gohr *
5174e406776SAndreas Gohr * @param  int $mem  Size of memory you want to allocate in bytes
5184e406776SAndreas Gohr * @param  int $used already allocated memory (see above)
5194e406776SAndreas Gohr * @author Filip Oscadal <webmaster@illusionsoftworks.cz>
5204e406776SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5214e406776SAndreas Gohr */
5224e406776SAndreas Gohrfunction is_mem_available($mem,$bytes=1048576){
5234e406776SAndreas Gohr  $limit = trim(ini_get('memory_limit'));
5244e406776SAndreas Gohr  if(empty($limit)) return true; // no limit set!
5254e406776SAndreas Gohr
5264e406776SAndreas Gohr  // parse limit to bytes
52773038c47SAndreas Gohr  $limit = php_to_byte($limit);
5284e406776SAndreas Gohr
5294e406776SAndreas Gohr  // get used memory if possible
5304e406776SAndreas Gohr  if(function_exists('memory_get_usage')){
5314e406776SAndreas Gohr    $used = memory_get_usage();
5324e406776SAndreas Gohr  }
5334e406776SAndreas Gohr
5344e406776SAndreas Gohr  if($used+$mem > $limit){
5354e406776SAndreas Gohr    return false;
5364e406776SAndreas Gohr  }
5374e406776SAndreas Gohr
5384e406776SAndreas Gohr  return true;
5394e406776SAndreas Gohr}
540f62ea8a1Sandi
541f62ea8a1Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
542f62ea8a1Sandi?>
543