<?php
//=================================================================
// KOOBI R7 - CMS - PORTALSOFTWARE(c) DREAM4(r) 
// HTTP://WWW.DREAM4.DE
//=================================================================
// ACHTUNG!
// DIESER PROGRAMMCODE IST URHERRECHTLICH GESCHUETZT
// DIE URHEBERRECHTE LIEGEN BEI DEN JEWEILIGEN AUTOREN.
// AENDERUNGEN IN DIESER DATEI ERFOLGEN AUF EIGENE GEFAHR!
// ES IST NICHT GESTATTET, DEN COPYRIGHTHINWEIS ZU AENDERN ODER ZU
// ENTFERNEN. NUR WENN SIE EINE COPYRIGHTHINWEIS-ENTFERNUNG BESTELLT
// HABEN, DUERFEN SIE DIESEN ENTFERNEN.
//================================================================
// BEI FRAGEN WENDEN SIE SICH BITTE AN: KONTAKT@DREAM4.DE
//=================================================================
define("THUMB", 1);
$inc = substr(dirname(__FILE__), 0, -6) . "";
$dir = getcwd();

if(defined('BASE_DIR')) exit;
define("BASE_DIR", $inc);

include_once("$inc/config/db.config.php");
include_once("$inc/class/Database.Class.php");
include_once(BASE_DIR . '/functions/Func.Session.php');
session_start();
session_name('SID');
include_once("$inc/functions/Func.Globals.php");
define("PREFIX", $config['dbprefix']);

$GLOBALS['db'] = new DB($config['dbhost'], $config['dbuser'], $config['dbpass'], $config['dbname']);

$sql = $GLOBALS['db']->Query("SELECT thumb_width_big,Wasserzeichen,WasserzeichenKomp,Wasserzeichen_Position,Wasserzeichen_Bild,thumb_width_middle,thumb_width_norm,thumb_width_small,thumb_quality FROM " . $config['dbprefix'] . "_shop_einstellungen");
$row = $sql->fetchrow();

$pref['thumbw'] = ($row->thumb_width_norm !='')? $row->thumb_width_norm : 140;
$pref['thumbw_big'] = ($row->thumb_width_big !='')? $row->thumb_width_big : 450;
$pref['thumbw_small'] = ($row->thumb_width_small!='') ? $row->thumb_width_small : 50;
$pref['thumbw_middle'] = ($row->thumb_width_middle!='') ? $row->thumb_width_middle : 75;
$pref['thumbcomp'] = ($row->thumb_quality!='') ? $row->thumb_quality : 90;
$pref['thumbpos'] = ($row->Wasserzeichen_Position!='') ? $row->Wasserzeichen_Position : 'bottom';
$pref['watermark'] = $row->Wasserzeichen_Bild;
$shop_watermark = $row->Wasserzeichen;

$thumb_w = $pref['thumbw_middle'];

$_REQUEST['smode'] = (isset($_REQUEST['smode'])) ? $_REQUEST['smode'] : "";
switch($_REQUEST['smode'])
{
	case '1' : 
		$thumb_w = $pref['thumbw_big']; 
		$thumb_name = 'big/';
	break;
	
	case '2' : 
		$thumb_w = $pref['thumbw']; 
		$thumb_name = 'normal/';
	break;
	
	case '3' :
	case 'small' : 
		$thumb_w = $pref['thumbw_small']; 
		$thumb_name = 'small/';
	break;
	
	case 'xsmall' : 
		$thumb_w = 20; 
		$thumb_name = 'xsmall/';
	break;
	
	case '4' : 
		$thumb_w = $pref['thumbw_middle']; 
		$thumb_name = 'medium/';
	break;
	
	case '5' : 
		$thumb_w = $pref['thumbw_middle'] / 1.3; 
		$thumb_name = 'medium/';
	break;
	
	case '' : 
	default : 
		$thumb_w = $pref['thumbw']; 
		$thumb_name = 'normal/';
	break;
}

if(!is_file(BASE_DIR . "/uploads/shop/icons/" . $_REQUEST['img']))
{
	(isset($_REQUEST['shopadmin']) && $_REQUEST['shopadmin']==1) ? readfile(BASE_DIR . 'uploads/noimage_small.gif') : readfile(BASE_DIR . 'uploads/noimage.gif');
	exit;
}

$end = substr($_REQUEST['img'], -4);

// Erzeugung der Bild - Id
// Existiert das Bild im Cache, wird es einfach ausgelesen
$uid = md5($thumb_w) . '_' . addslashes($_REQUEST['img']);
$uid = $thumb_name . $_REQUEST['img'];

if(is_file(BASE_DIR . '/uploads/shop/icons_thumbnails/' .$uid))
{
	readfile(BASE_DIR . '/uploads/shop/icons_thumbnails/' .$uid);
	exit;
} 

	
switch($end)
{
	case '.jpg':
	case 'jpeg':
	default:
	$q = BASE_DIR . "/uploads/shop/icons/" . $_REQUEST['img'];
	$quelle = imagecreatefromjpeg($q);
	header ("Content-type: image/jpeg");
	break;
	
	case '.png':
	$q = BASE_DIR . "/uploads/shop/icons/" . $_REQUEST['img'];
	$quelle = @imagecreatefrompng($q);
	header ("Content-type: image/png");
	break;

	case '.gif':
	$q = BASE_DIR . "/uploads/shop/icons/" . $_REQUEST['img'];
	$quelle = @imagecreatefromgif($q);
	header ("Content-type: image/gif");
	break;
}

$dir = getcwd();
$w = $thumb_w;

if (!is_file(BASE_DIR . "/uploads/shop/icons/" . $_REQUEST['img']))
{
	$_REQUEST['img'] = 'noimage.gif';
} 

$sz = $thumb_w;
$im_width = imageSX($quelle);
$im_height = imageSY($quelle);
$factor = $sz / $im_width;

$size_orig = @getimagesize($q);
if(@$_REQUEST['smode'] == 1 && !empty($size_orig[0]) && $size_orig[0] < $thumb_w)
{
	$sz = $size_orig[0];
	$im_width = $size_orig[0];
	$im_height = $size_orig[1];
	$factor = $sz / $im_width;
	$resize_watermark = 1;
}

$w = $sz;
$h = $im_height * $factor;

/*
if (function_exists('imagecreatetruecolor'))
{
	$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));
*/
if (function_exists('imagecreatetruecolor'))
{
    $ziel = imagecreatetruecolor($w, $h);
	if($end=='.png' && function_exists('imagesavealpha'))
	{
		imagealphablending($ziel, false);
        $colorTransparent = imagecolorallocatealpha($ziel, 0, 0, 0, 127);
        imagefill($ziel, 0, 0, $colorTransparent);
        imagesavealpha($ziel, true);
	} elseif($end=='.gif') {
		$trnprt_indx = imagecolortransparent($quelle);
		if ($trnprt_indx >= 0)
		{
			$trnprt_color = imagecolorsforindex($quelle, $trnprt_indx);
			$trnprt_indx = imagecolorallocate($ziel, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
			imagefill($ziel, 0, 0, $trnprt_indx);
			imagecolortransparent($ziel, $trnprt_indx);
		}
	} else {
		$ziel_bg = imagecolorallocate($ziel,255,255,255);
		imagefill($ziel , 0, 0, $ziel_bg);
	}
} else {
    $ziel = imagecreate($w, $h);
} 

if(function_exists('imagecopyresampled'))
{
	@imagecopyresampled($ziel, $quelle, 0, 0, 0, 0, $w, $h, imagesx($quelle), imagesy($quelle));
} else {
	@imagecopyresized($ziel, $quelle, 0, 0, 0, 0, $w, $h, imagesx($quelle), imagesy($quelle));
}

// Wasserzeichen einfuegen
if($_REQUEST['smode'] == 1 && function_exists('imagecreatefrompng') && $shop_watermark==1 && file_exists(BASE_DIR . '/uploads/shop/' . $pref['watermark']) && $pref['watermark']!='')
{
	$insert = imagecreatefrompng(BASE_DIR . '/uploads/shop/' . $pref['watermark']);
	$size_watermark = @getimagesize(BASE_DIR . '/uploads/shop/' . $pref['watermark']);
	$resize_w = (@$size_watermark[0] >= @$size_orig[0]) ? 1 : 1;
	
	imagecolortransparent($insert, imagecolorexact($insert, 255, 0, 255));
	
	$insert_x = imagesx($insert) / $resize_w;
	$insert_y = imagesy($insert) / $resize_w;
	
	$dest_x = imagesx($ziel) - $insert_x - 0;
	$dest_y = imagesy($ziel) - $insert_y - 0;
	
	$x_pos = (imagesx($ziel)/2)-($insert_x/2);
	$y_pos = (imagesy($ziel)/2)-($insert_y/2);
	
	if($pref['thumbpos'] == 'middle')
		imagecopymerge($ziel, $insert, $x_pos, $y_pos, 0, 0, $insert_x, $insert_y,$row->WasserzeichenKomp);
	else
		imagecopymerge($ziel, $insert, $dest_x, $dest_y, 0, 0, $insert_x, $insert_y,$row->WasserzeichenKomp);
}

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

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

$a = ob_get_contents();
ob_end_clean();
$fp = fopen(BASE_DIR . "/uploads/shop/icons_thumbnails/".$uid, "wb+");
fwrite($fp, $a);
fclose($fp);
echo $a;
exit();
?>