<?php
// ======================================================================== //
//   KOOBI - CMS                                                            //
//   2003-2005 © DREAM4 - HTTP://WWW.DREAM4.DE                              //
//   REMOVING THIS INFO WITHOUT PERMISSION BREAKS YOUR LICENSE.             //
//   PLEASE NOTE: KOOBI IS NO FREEWARE! ANY PART OF THE SOFTWARE IS © BY    //
//   THEIR OWNER.                                                           //
//                                                                          //
//   DREAM4 - AM MICHELSGRUND 7 - 69469 WEINHEIM - GERMANY                  //    
//   TEL. +49(0) 6201 - 980442 / FAX. +49(0) 6201 - 64809                   //
// ======================================================================== //
define("THUMB", 1);
$inc = substr(dirname(__FILE__), 0, -7) . "";
$dir = getcwd();
define("BASEDIR", $inc);
include("$inc/inc/init.php");

$_REQUEST['img_id'] = str_replace(array(':','/','\''),'',$_REQUEST['img_id']);

$sql = new dbc("SELECT galwidth,galheight,nofactor,thumbcomp FROM " . PREFIX . "_settings");
$row = $sql->fetchrow();

$pref['thumbw'] = !$row->galwidth ? 120 : $row->galwidth;
$pref['thumbh'] = !$row->galheight ? 90 : $row->galheight ;
$pref['nofactor'] = $row->nofactor;
$pref['thumbcomp'] = !$row->thumbcomp ? 90 : $row->thumbcomp;

switch($_REQUEST['smode']){
	case 1 :
    $thumb_w = $pref['thumbw'] / 2.2;
    $thumb_h = $pref['thumbh'] / 2.2;
	break;
	
	case 2 :
	$thumb_w = $pref['thumbw'] / 1.8;
    $thumb_h = $pref['thumbh'] / 1.8;
	break;
	
	default:
	$thumb_w = $pref['thumbw'];
    $thumb_h = $pref['thumbh'];
	break;
}

	$w = $thumb_w;
	$h = $thumb_h;
	
if ($_REQUEST['type'] == "inline") {
    header ("Content-type: image/jpeg");
	$end = ".jpg";
	$q = BASEDIR . "/uploads/inlineshots/" . $_REQUEST['img_id']. "";
    $quelle = imagecreatefromjpeg(BASEDIR . "/uploads/inlineshots/" . $_REQUEST['img_id'] . "");
	$uid = md5($thumb_w . $h . $q);

	if(file_exists(BASEDIR . '/temp/1/'.$uid)){
		header ("Content-type: image/jpeg");
		readfile(BASEDIR . '/temp/1/'.$uid);
		exit();
	}

} else {
    $sql = new dbc("SELECT path FROM " . $dbprefix . "_gallery_items WHERE id=$_REQUEST[img_id] ");
    $row = $sql->fetchrow();
   
    if (!$row) $row->path = "nopic.jpg";
    $end = substr($row->path, -4);
	
	$q = BASEDIR . "/uploads/galerie/" . $row->path . "";
	$uid = md5($thumb_w . $h . $q);   
	
	if (!file_exists(BASEDIR . "/uploads/galerie/" . $row->path . "")) $_REQUEST['file'] = "dummy.jpg";

if(file_exists(BASEDIR . '/temp/1/'.$uid)){
	if($end == '.jpg' || $end == 'jpeg') header ("Content-type: image/jpeg");
	if($end == '.png') header ("Content-type: image/png");
	readfile(BASEDIR . '/temp/1/'.$uid);
	exit();
}

switch($end){
	case '.jpg':
	case 'jpeg':
	$q = BASEDIR . "/uploads/galerie/" . $row->path . "";
	$quelle = imagecreatefromjpeg($q);
	header ("Content-type: image/jpeg");
	break;
	
	case '.png':
	$q = BASEDIR . "/uploads/galerie/" . $row->path . "";
	$quelle = @imagecreatefrompng($q);
	header ("Content-type: image/png");
	break;

	default:
	$quelle = $q;
	header ("Content-type: image/gif");
	break;
}
	
	
} 

$sz = $thumb_w;
$sz2 = $thumb_h;

$im_width = imageSX($quelle);
$im_height = imageSY($quelle);

if ($pref['nofactor'] != 1) {
    $factor = $sz / $im_width;
    $w = $sz;
    $h = $im_height * $factor;
} 


if (gdversion() >= 2) {
    $ziel = imagecreatetruecolor($w, $h);
	$ziel_bg = imagecolorallocate($ziel,255,255,255);
	imagefill($ziel , 0, 0, $ziel_bg);
} else {
    $ziel = imagecreate($w, $h);
} 

@imagecopyresampled($ziel, $quelle, 0, 0, 0, 0, $w, $h, imagesx($quelle), imagesy($quelle));

ob_start();
switch($end){
	case '.jpg':
	case 'jpeg':
	$bild = @imagejpeg($ziel, "", $pref['thumbcomp']);
	break;

	case '.png':
	$bild = @imagepng($ziel, "", $pref['thumbcomp']);
	break;
	
	default:
	$bild = $quelle;
	break;
}

$a = ob_get_contents();
ob_end_clean();
$fp = fopen(BASEDIR . "/temp/1/".$uid, "wb+");
fwrite($fp, $a);
fclose($fp);
echo $a;
exit();
?>