xref: /dokuwiki/lib/exe/fetch.php (revision 28d5e270891fe34f6e1fd1942a3d80633d46702c)
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
68f62ea8a1Sandi  //handle image resizing
69f62ea8a1Sandi  if((substr($MIME,0,5) == 'image') && $WIDTH){
70f62ea8a1Sandi    $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT);
71f62ea8a1Sandi  }
72f62ea8a1Sandi
73e935fb4aSAndreas Gohr  // finally send the file to the client
7483730152SBen Coburn  sendFile($FILE,$MIME,$CACHE);
75f62ea8a1Sandi
76e935fb4aSAndreas Gohr/* ------------------------------------------------------------------------ */
77f62ea8a1Sandi
78e935fb4aSAndreas Gohr/**
79e935fb4aSAndreas Gohr * Set headers and send the file to the client
80e935fb4aSAndreas Gohr *
81e935fb4aSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
8283730152SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
83e935fb4aSAndreas Gohr */
8483730152SBen Coburnfunction sendFile($file,$mime,$cache){
8583730152SBen Coburn  global $conf;
8683730152SBen Coburn  $fmtime = filemtime($file);
87e935fb4aSAndreas Gohr  // send headers
88e935fb4aSAndreas Gohr  header("Content-Type: $mime");
8983730152SBen Coburn  // smart http caching headers
9083730152SBen Coburn  if ($cache==-1) {
9183730152SBen Coburn    // cache
9283730152SBen Coburn    // cachetime or one hour
9383730152SBen Coburn    header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
9483730152SBen Coburn    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
95e935fb4aSAndreas Gohr    header('Pragma: public');
9683730152SBen Coburn  } else if ($cache>0) {
9783730152SBen Coburn    // recache
9883730152SBen Coburn    // remaining cachetime + 10 seconds so the newly recached media is used
9983730152SBen Coburn    header('Expires: '.gmdate("D, d M Y H:i:s", $fmtime+$conf['cachetime']+10).' GMT');
10083730152SBen Coburn    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($fmtime-time()+$conf['cachetime']+10, 0));
10183730152SBen Coburn    header('Pragma: public');
10283730152SBen Coburn  } else if ($cache==0) {
10383730152SBen Coburn    // nocache
10483730152SBen Coburn    header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
10583730152SBen Coburn    header('Pragma: public');
10683730152SBen Coburn  }
107ff4f5ee7SBen Coburn  //send important headers first, script stops here if '304 Not Modified' response
10883730152SBen Coburn  http_conditionalRequest($fmtime);
1099a87c72aSAndreas Gohr
110f62ea8a1Sandi
111f62ea8a1Sandi  //application mime type is downloadable
112e935fb4aSAndreas Gohr  if(substr($mime,0,11) == 'application'){
113e935fb4aSAndreas Gohr    header('Content-Disposition: attachment; filename="'.basename($file).'";');
114f62ea8a1Sandi  }
115f62ea8a1Sandi
1169a87c72aSAndreas Gohr  //use x-sendfile header to pass the delivery to compatible webservers
1179a87c72aSAndreas Gohr  if($conf['xsendfile'] == 1){
1189a87c72aSAndreas Gohr    header("X-LIGHTTPD-send-file: $file");
1199a87c72aSAndreas Gohr    exit;
1209a87c72aSAndreas Gohr  }elseif($conf['xsendfile'] == 2){
1219a87c72aSAndreas Gohr    header("X-Sendfile: $file");
1229a87c72aSAndreas Gohr    exit;
123deec6eb9Spierre.pracht  }elseif($conf['xsendfile'] == 3){
124deec6eb9Spierre.pracht    header("X-Accel-Redirect: $file");
125deec6eb9Spierre.pracht    exit;
1269a87c72aSAndreas Gohr  }
1279a87c72aSAndreas Gohr
1289a87c72aSAndreas Gohr  //support download continueing
1299a87c72aSAndreas Gohr  header('Accept-Ranges: bytes');
1309a87c72aSAndreas Gohr  list($start,$len) = http_rangeRequest(filesize($file));
1319a87c72aSAndreas Gohr
132e935fb4aSAndreas Gohr  // send file contents
133e935fb4aSAndreas Gohr  $fp = @fopen($file,"rb");
134f62ea8a1Sandi  if($fp){
135e935fb4aSAndreas Gohr    fseek($fp,$start); //seek to start of range
136e935fb4aSAndreas Gohr
137e935fb4aSAndreas Gohr    $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len;
138e935fb4aSAndreas Gohr    while (!feof($fp) && $chunk > 0) {
139d6751ba5SAndreas Gohr      @set_time_limit(30); // large files can take a lot of time
140e935fb4aSAndreas Gohr      print fread($fp, $chunk);
141615a21edSBrian Cowan      flush();
142e935fb4aSAndreas Gohr      $len -= $chunk;
143e935fb4aSAndreas Gohr      $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len;
144615a21edSBrian Cowan    }
145615a21edSBrian Cowan    fclose($fp);
146f62ea8a1Sandi  }else{
147f62ea8a1Sandi    header("HTTP/1.0 500 Internal Server Error");
148e935fb4aSAndreas Gohr    print "Could not read $file - bad permissions?";
149e935fb4aSAndreas Gohr  }
150f62ea8a1Sandi}
151f62ea8a1Sandi
152e935fb4aSAndreas Gohr/**
153e935fb4aSAndreas Gohr * Checks and sets headers to handle range requets
154e935fb4aSAndreas Gohr *
155e935fb4aSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
156e935fb4aSAndreas Gohr * @returns array The start byte and the amount of bytes to send
157e935fb4aSAndreas Gohr */
158e935fb4aSAndreas Gohrfunction http_rangeRequest($size){
159e935fb4aSAndreas Gohr  if(!isset($_SERVER['HTTP_RANGE'])){
160e935fb4aSAndreas Gohr    // no range requested - send the whole file
161e935fb4aSAndreas Gohr    header("Content-Length: $size");
162e935fb4aSAndreas Gohr    return array(0,$size);
163e935fb4aSAndreas Gohr  }
164e935fb4aSAndreas Gohr
165e935fb4aSAndreas Gohr  $t = explode('=', $_SERVER['HTTP_RANGE']);
166e935fb4aSAndreas Gohr  if (!$t[0]=='bytes') {
167e935fb4aSAndreas Gohr    // we only understand byte ranges - send the whole file
168e935fb4aSAndreas Gohr    header("Content-Length: $size");
169e935fb4aSAndreas Gohr    return array(0,$size);
170e935fb4aSAndreas Gohr  }
171e935fb4aSAndreas Gohr
172e935fb4aSAndreas Gohr  $r = explode('-', $t[1]);
173e935fb4aSAndreas Gohr  $start = (int)$r[0];
174e935fb4aSAndreas Gohr  $end = (int)$r[1];
175e935fb4aSAndreas Gohr  if (!$end) $end = $size - 1;
176e935fb4aSAndreas Gohr  if ($start > $end || $start > $size || $end > $size){
177e935fb4aSAndreas Gohr    header('HTTP/1.1 416 Requested Range Not Satisfiable');
178e935fb4aSAndreas Gohr    print 'Bad Range Request!';
179e935fb4aSAndreas Gohr    exit;
180e935fb4aSAndreas Gohr  }
181e935fb4aSAndreas Gohr
182e935fb4aSAndreas Gohr  $tot = $end - $start + 1;
183e935fb4aSAndreas Gohr  header('HTTP/1.1 206 Partial Content');
184e935fb4aSAndreas Gohr  header("Content-Range: bytes {$start}-{$end}/{$size}");
185e935fb4aSAndreas Gohr  header("Content-Length: $tot");
186e935fb4aSAndreas Gohr
187e935fb4aSAndreas Gohr  return array($start,$tot);
188e935fb4aSAndreas Gohr}
189e935fb4aSAndreas Gohr
190e935fb4aSAndreas Gohr/**
191f62ea8a1Sandi * Resizes the given image to the given size
192f62ea8a1Sandi *
193f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
194f62ea8a1Sandi */
195f62ea8a1Sandifunction get_resized($file, $ext, $w, $h=0){
196f62ea8a1Sandi  global $conf;
197f62ea8a1Sandi
198f62ea8a1Sandi  $info  = getimagesize($file);
199f62ea8a1Sandi  if(!$h) $h = round(($w * $info[1]) / $info[0]);
200f62ea8a1Sandi
2018fcc3410SAndreas Gohr  // we wont scale up to infinity
2028fcc3410SAndreas Gohr  if($w > 2000 || $h > 2000) return $file;
203f62ea8a1Sandi
204f62ea8a1Sandi  //cache
20598407a7aSandi  $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext);
206f62ea8a1Sandi  $mtime = @filemtime($local); // 0 if not exists
207f62ea8a1Sandi
20868375754SPavel Vitis  if( $mtime > filemtime($file) ||
20968375754SPavel Vitis      resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) ||
21068375754SPavel Vitis      resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){
211*28d5e270SOtto Vainio    if($conf['fperm']) chmod($local, $conf['fperm']);
212f62ea8a1Sandi    return $local;
213f62ea8a1Sandi  }
214f62ea8a1Sandi  //still here? resizing failed
215f62ea8a1Sandi  return $file;
216f62ea8a1Sandi}
217f62ea8a1Sandi
218f62ea8a1Sandi/**
219f62ea8a1Sandi * Returns the wanted cachetime in seconds
220f62ea8a1Sandi *
221f62ea8a1Sandi * Resolves named constants
222f62ea8a1Sandi *
223f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
224f62ea8a1Sandi */
225f62ea8a1Sandifunction calc_cache($cache){
226f62ea8a1Sandi  global $conf;
227f62ea8a1Sandi
228f62ea8a1Sandi  if(strtolower($cache) == 'nocache') return 0; //never cache
229f62ea8a1Sandi  if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
230f62ea8a1Sandi  return -1; //cache endless
231f62ea8a1Sandi}
232f62ea8a1Sandi
233f62ea8a1Sandi/**
234f62ea8a1Sandi * Download a remote file and return local filename
235f62ea8a1Sandi *
236f62ea8a1Sandi * returns false if download fails. Uses cached file if available and
237f62ea8a1Sandi * wanted
238f62ea8a1Sandi *
239f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
24068375754SPavel Vitis * @author  Pavel Vitis <Pavel.Vitis@seznam.cz>
241f62ea8a1Sandi */
242f62ea8a1Sandifunction get_from_URL($url,$ext,$cache){
243f62ea8a1Sandi  global $conf;
244f62ea8a1Sandi
245847b8298SAndreas Gohr  // if no cache or fetchsize just redirect
246847b8298SAndreas Gohr  if ($cache==0)           return false;
247847b8298SAndreas Gohr  if (!$conf['fetchsize']) return false;
2484f3c4962SBen Coburn
24968375754SPavel Vitis  $local = getCacheName(strtolower($url),".media.$ext");
250f62ea8a1Sandi  $mtime = @filemtime($local); // 0 if not exists
251f62ea8a1Sandi
252f62ea8a1Sandi  //decide if download needed:
2534f3c4962SBen Coburn  if( ($mtime == 0) ||                           // cache does not exist
2544f3c4962SBen Coburn      ($cache != -1 && $mtime < time()-$cache)   // 'recache' and cache has expired
25568375754SPavel Vitis    ){
256894a80ccSAndreas Gohr      if(image_download($url,$local)){
257f62ea8a1Sandi        return $local;
258f62ea8a1Sandi      }else{
259f62ea8a1Sandi        return false;
260f62ea8a1Sandi      }
261f62ea8a1Sandi  }
262f62ea8a1Sandi
263f62ea8a1Sandi  //if cache exists use it else
264f62ea8a1Sandi  if($mtime) return $local;
265f62ea8a1Sandi
266f62ea8a1Sandi  //else return false
267f62ea8a1Sandi  return false;
268f62ea8a1Sandi}
269f62ea8a1Sandi
270f62ea8a1Sandi/**
271894a80ccSAndreas Gohr * Download image files
272894a80ccSAndreas Gohr *
273894a80ccSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
274894a80ccSAndreas Gohr */
275894a80ccSAndreas Gohrfunction image_download($url,$file){
276894a80ccSAndreas Gohr  global $conf;
277894a80ccSAndreas Gohr  $http = new DokuHTTPClient();
278894a80ccSAndreas Gohr  $http->max_bodysize = $conf['fetchsize'];
279894a80ccSAndreas Gohr  $http->timeout = 25; //max. 25 sec
280894a80ccSAndreas Gohr  $http->header_regexp = '!\r\nContent-Type: image/(jpe?g|gif|png)!i';
281894a80ccSAndreas Gohr
282894a80ccSAndreas Gohr  $data = $http->get($url);
283894a80ccSAndreas Gohr  if(!$data) return false;
284894a80ccSAndreas Gohr
285894a80ccSAndreas Gohr  $fileexists = @file_exists($file);
286894a80ccSAndreas Gohr  $fp = @fopen($file,"w");
287894a80ccSAndreas Gohr  if(!$fp) return false;
288894a80ccSAndreas Gohr  fwrite($fp,$data);
289894a80ccSAndreas Gohr  fclose($fp);
290894a80ccSAndreas Gohr  if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
291d1ed0b61SAndreas Gohr
292d1ed0b61SAndreas Gohr  // check if it is really an image
293d1ed0b61SAndreas Gohr  $info = @getimagesize($file);
294d1ed0b61SAndreas Gohr  if(!$info){
295d1ed0b61SAndreas Gohr    @unlink($file);
296d1ed0b61SAndreas Gohr    return false;
297d1ed0b61SAndreas Gohr  }
298d1ed0b61SAndreas Gohr
299894a80ccSAndreas Gohr  return true;
300894a80ccSAndreas Gohr}
301894a80ccSAndreas Gohr
302894a80ccSAndreas Gohr/**
30368375754SPavel Vitis * resize images using external ImageMagick convert program
30468375754SPavel Vitis *
30568375754SPavel Vitis * @author Pavel Vitis <Pavel.Vitis@seznam.cz>
30668375754SPavel Vitis * @author Andreas Gohr <andi@splitbrain.org>
30768375754SPavel Vitis */
30868375754SPavel Vitisfunction resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
30968375754SPavel Vitis  global $conf;
31068375754SPavel Vitis
3117bc7a78eSAndreas Gohr  // check if convert is configured
3127bc7a78eSAndreas Gohr  if(!$conf['im_convert']) return false;
31368375754SPavel Vitis
31468375754SPavel Vitis  // prepare command
31568375754SPavel Vitis  $cmd  = $conf['im_convert'];
31668375754SPavel Vitis  $cmd .= ' -resize '.$to_w.'x'.$to_h.'!';
3172b03e74dSBen Coburn  if ($ext == 'jpg' || $ext == 'jpeg') {
3182b03e74dSBen Coburn      $cmd .= ' -quality '.$conf['jpg_quality'];
3192b03e74dSBen Coburn  }
32068375754SPavel Vitis  $cmd .= " $from $to";
32168375754SPavel Vitis
32268375754SPavel Vitis  @exec($cmd,$out,$retval);
32368375754SPavel Vitis  if ($retval == 0) return true;
32468375754SPavel Vitis  return false;
32568375754SPavel Vitis}
32668375754SPavel Vitis
32768375754SPavel Vitis/**
32868375754SPavel Vitis * resize images using PHP's libGD support
329f62ea8a1Sandi *
330f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org>
331e582e8b6Ss_wienecke * @author Sebastian Wienecke <s_wienecke@web.de>
332f62ea8a1Sandi */
33368375754SPavel Vitisfunction resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
334f62ea8a1Sandi  global $conf;
335f62ea8a1Sandi
336f62ea8a1Sandi  if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
337f62ea8a1Sandi
3384e406776SAndreas Gohr  // check available memory
3394e406776SAndreas Gohr  if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){
3404e406776SAndreas Gohr    return false;
3414e406776SAndreas Gohr  }
3424e406776SAndreas Gohr
343f62ea8a1Sandi  // create an image of the given filetype
344f62ea8a1Sandi  if ($ext == 'jpg' || $ext == 'jpeg'){
345f62ea8a1Sandi    if(!function_exists("imagecreatefromjpeg")) return false;
346f62ea8a1Sandi    $image = @imagecreatefromjpeg($from);
347f62ea8a1Sandi  }elseif($ext == 'png') {
348f62ea8a1Sandi    if(!function_exists("imagecreatefrompng")) return false;
349f62ea8a1Sandi    $image = @imagecreatefrompng($from);
350f62ea8a1Sandi
351f62ea8a1Sandi  }elseif($ext == 'gif') {
352f62ea8a1Sandi    if(!function_exists("imagecreatefromgif")) return false;
353f62ea8a1Sandi    $image = @imagecreatefromgif($from);
354f62ea8a1Sandi  }
355f62ea8a1Sandi  if(!$image) return false;
356f62ea8a1Sandi
357e582e8b6Ss_wienecke  if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor") && $ext != 'gif'){
358f62ea8a1Sandi    $newimg = @imagecreatetruecolor ($to_w, $to_h);
359f62ea8a1Sandi  }
360f62ea8a1Sandi  if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
361dd7bbbf4SAndreas Gohr  if(!$newimg){
362dd7bbbf4SAndreas Gohr    imagedestroy($image);
363dd7bbbf4SAndreas Gohr    return false;
364dd7bbbf4SAndreas Gohr  }
365f62ea8a1Sandi
366f62ea8a1Sandi  //keep png alpha channel if possible
367f62ea8a1Sandi  if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){
368f62ea8a1Sandi    imagealphablending($newimg, false);
369f62ea8a1Sandi    imagesavealpha($newimg,true);
370f62ea8a1Sandi  }
371f62ea8a1Sandi
372e582e8b6Ss_wienecke  //keep gif transparent color if possible
373e582e8b6Ss_wienecke  if($ext == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) {
374e582e8b6Ss_wienecke    if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
375e582e8b6Ss_wienecke      $transcolorindex = @imagecolortransparent($image);
376e582e8b6Ss_wienecke      if($transcolorindex >= 0 ) { //transparent color exists
377e582e8b6Ss_wienecke        $transcolor = @imagecolorsforindex($image, $transcolorindex);
378e582e8b6Ss_wienecke        $transcolorindex = @imagecolorallocate($newimg, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
379e582e8b6Ss_wienecke        @imagefill($newimg, 0, 0, $transcolorindex);
380e582e8b6Ss_wienecke        @imagecolortransparent($newimg, $transcolorindex);
381e582e8b6Ss_wienecke      }else{ //filling with white
382e582e8b6Ss_wienecke        $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
383e582e8b6Ss_wienecke        @imagefill($newimg, 0, 0, $whitecolorindex);
384e582e8b6Ss_wienecke      }
385e582e8b6Ss_wienecke    }else{ //filling with white
386e582e8b6Ss_wienecke      $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
387e582e8b6Ss_wienecke      @imagefill($newimg, 0, 0, $whitecolorindex);
388e582e8b6Ss_wienecke    }
389e582e8b6Ss_wienecke  }
390e582e8b6Ss_wienecke
391f62ea8a1Sandi  //try resampling first
392f62ea8a1Sandi  if(function_exists("imagecopyresampled")){
393f62ea8a1Sandi    if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
394f62ea8a1Sandi      imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
395f62ea8a1Sandi    }
396f62ea8a1Sandi  }else{
397f62ea8a1Sandi    imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
398f62ea8a1Sandi  }
399f62ea8a1Sandi
400dd7bbbf4SAndreas Gohr  $okay = false;
401f62ea8a1Sandi  if ($ext == 'jpg' || $ext == 'jpeg'){
402dd7bbbf4SAndreas Gohr    if(!function_exists('imagejpeg')){
403dd7bbbf4SAndreas Gohr      $okay = false;
404dd7bbbf4SAndreas Gohr    }else{
4052b03e74dSBen Coburn      $okay = imagejpeg($newimg, $to, $conf['jpg_quality']);
406dd7bbbf4SAndreas Gohr    }
407f62ea8a1Sandi  }elseif($ext == 'png') {
408dd7bbbf4SAndreas Gohr    if(!function_exists('imagepng')){
409dd7bbbf4SAndreas Gohr      $okay = false;
410dd7bbbf4SAndreas Gohr    }else{
411dd7bbbf4SAndreas Gohr      $okay =  imagepng($newimg, $to);
412dd7bbbf4SAndreas Gohr    }
413f62ea8a1Sandi  }elseif($ext == 'gif') {
414dd7bbbf4SAndreas Gohr    if(!function_exists('imagegif')){
415dd7bbbf4SAndreas Gohr      $okay = false;
416dd7bbbf4SAndreas Gohr    }else{
417dd7bbbf4SAndreas Gohr      $okay = imagegif($newimg, $to);
418dd7bbbf4SAndreas Gohr    }
419f62ea8a1Sandi  }
420f62ea8a1Sandi
421dd7bbbf4SAndreas Gohr  // destroy GD image ressources
422dd7bbbf4SAndreas Gohr  if($image) imagedestroy($image);
423dd7bbbf4SAndreas Gohr  if($newimg) imagedestroy($newimg);
424dd7bbbf4SAndreas Gohr
425dd7bbbf4SAndreas Gohr  return $okay;
426f62ea8a1Sandi}
427f62ea8a1Sandi
4284e406776SAndreas Gohr/**
4294e406776SAndreas Gohr * Checks if the given amount of memory is available
4304e406776SAndreas Gohr *
4314e406776SAndreas Gohr * If the memory_get_usage() function is not available the
43273038c47SAndreas Gohr * function just assumes $bytes of already allocated memory
4334e406776SAndreas Gohr *
4344e406776SAndreas Gohr * @param  int $mem  Size of memory you want to allocate in bytes
4354e406776SAndreas Gohr * @param  int $used already allocated memory (see above)
4364e406776SAndreas Gohr * @author Filip Oscadal <webmaster@illusionsoftworks.cz>
4374e406776SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
4384e406776SAndreas Gohr */
4394e406776SAndreas Gohrfunction is_mem_available($mem,$bytes=1048576){
4404e406776SAndreas Gohr  $limit = trim(ini_get('memory_limit'));
4414e406776SAndreas Gohr  if(empty($limit)) return true; // no limit set!
4424e406776SAndreas Gohr
4434e406776SAndreas Gohr  // parse limit to bytes
44473038c47SAndreas Gohr  $limit = php_to_byte($limit);
4454e406776SAndreas Gohr
4464e406776SAndreas Gohr  // get used memory if possible
4474e406776SAndreas Gohr  if(function_exists('memory_get_usage')){
4484e406776SAndreas Gohr    $used = memory_get_usage();
4494e406776SAndreas Gohr  }
4504e406776SAndreas Gohr
4514e406776SAndreas Gohr  if($used+$mem > $limit){
4524e406776SAndreas Gohr    return false;
4534e406776SAndreas Gohr  }
4544e406776SAndreas Gohr
4554e406776SAndreas Gohr  return true;
4564e406776SAndreas Gohr}
457f62ea8a1Sandi
458f62ea8a1Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
459f62ea8a1Sandi?>
460