<?php
/**
 * Create a thumbnail
 *
 * @author Brett @ Mr PHP
 */
 
// define allowed image sizes
$sizes = array(
	'40x40',
	'50x50',
	'70x70',
	'72x72',
	'90x90',
	'100x100',
	'120x120',
	'150x150',
	'200x200',
	'360x240',
	'480x280',
	'800x220',
	'800x320',
	'960x560'
);

// ensure there was a thumb in the URL
if (!$_GET['thumb']) {
	error('no thumb');
}

// get the thumbnail from the URL
$thumb = strip_tags(htmlspecialchars($_GET['thumb']));

// get the image and size
$thumb_array = explode('/',$thumb);
##print_r($thumb_array);
##echo '<br>';
$size = array_shift($thumb_array);
##echo $size.'<br>';
$image = '../../'.implode('/',$thumb_array);

list($width,$height) = explode('x',$size);
##echo $width.'<br>';
##echo $height.'<br>';

// ensure the size is valid
if (!in_array($size,$sizes)) {
	error('invalid size');
}

// ensure the image file exists
if (!file_exists($image)) {
	##echo '<img src="'.$image.'" /><br>';
	error('no source image');
}

// generate the thumbnail
require($_SERVER['DOCUMENT_ROOT'] . '/pthmb_my/phpthumb.class.my');
$phpThumb = new phpThumb();
$phpThumb->setSourceFilename($image);
$phpThumb->setParameter('w',$width);
$phpThumb->setParameter('h',$height);

$phpThumb->setParameter('config_imagemagick_path', 'D:\SERVER_\\TOOLKITS\\GraphicsMagick-1.3.29-Q8-x64\\gm.exe');
//$phpThumb->setParameter('config_imagemagick_path', 'D:\SERVER_\\ImageMagick-6.8.8-Q8-x64\\convert.exe');
//$phpThumb->setParameter('imagemagick_path','D:\\SERVER_\\ImageMagick-6.8.8-Q8-x64\\convert.exe');
//$phpThumb->config_imagemagick_path = 'D:\\SERVER_\\ImageMagick-6.8.8-Q8-x64\\convert.exe';
$phpThumb->setParameter('config_prefer_imagemagick', true);
$phpThumb->setParameter('interlace',true);

$phpThumb->setParameter('f',substr($thumb,-3,3)); // set the output format
$phpThumb->setParameter('zc', 1); // zoom crop
$phpThumb->setParameter('q', 90); // quality
//$phpThumb->setParameter('far','C'); // scale outside
//$phpThumb->setParameter('bg','FFFFFF'); // scale outside
$phpThumb->setParameter('bg','000000'); // scale outside
if(isset($_GET['b']) && $_GET['b'] == 'y') {
$phpThumb->setParameter('fltr','blur|3');
}
if(isset($_GET['g']) && $_GET['g'] == 'y') {
$phpThumb->setParameter('fltr','gray');
}

if (!$phpThumb->GenerateThumbnail()) {
	error('cannot generate thumbnail');
}
##
##print_r($phpThumb);
##
// make the directory to put the image
if (!MakeDirectory(dirname($thumb),true)) {
        error('cannot create directory');
}

// write the file
if (!$phpThumb->RenderToFile($thumb)) {
	error('cannot save thumbnail');
}

// redirect to the thumb
// note: you need the '?new' or IE wont do a redirect
##header('Location: '.dirname($_SERVER['SCRIPT_NAME']).'/'.$thumb.'?new');
header('Location: http://my.aegean.gr/web/cache/thumbs/'.$thumb.'?new');

// basic error handling
function error($error) {
	/*header('Location: http://my.aegean.gr/web/');*/
	header("HTTP/1.0 404 Not Found");
	echo '<h1>Not Found</h1>';
	echo '<p>The image you requested could not be found.</p>';
	echo "<p>An error was triggered: <b>$error</b></p>";
	
	exit();
}
//recursive dir function
function mkpath($path, $mode){
    is_dir(dirname($path)) || mkpath(dirname($path), $mode);
    return is_dir($path) || @mkdir($path,0777,$mode);
}

//recursive dir function 2
function MakeDirectory($path, $mode = 0777)
{
  if (is_dir($path) || @mkdir($path,$mode)) return TRUE;
  if (!MakeDirectory(dirname($path),$mode)) return FALSE;
  return @mkdir($path,$mode);
}
?>
