<?php

/**
 * Get data
 *
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-09-03
 */
	
	// Header
	error_reporting(0);
	
	ini_set('memory_limit', '32M');
	umask(0000);
	
	// Path to pagemap root
	$root = "../../../";
	
	// Get config
	if(!isset($_GET['thumbnail_size'])) $_GET['thumbnail_size'] = 156;
	if(!isset($_GET['thumbnail_crop_width'])) $_GET['thumbnail_crop_width'] = 156;
	if(!isset($_GET['thumbnail_crop_height'])) $_GET['thumbnail_crop_height'] = 156;
	if(!isset($_GET['thumbnail_crop'])) $_GET['thumbnail_crop'] = 1;
	if(!isset($_GET['thumbnail_rotation'])) $_GET['thumbnail_rotation'] = 0;
	if(!isset($_GET['thumbnail_rotate_cw'])) $_GET['thumbnail_rotate_cw'] = 1;
	if(!isset($_GET['thumbnail_reflection'])) $_GET['thumbnail_reflection'] = 0;
	if(!isset($_GET['thumbnail_background'])) $_GET['thumbnail_background'] = '000000';
	if(!isset($_GET['thumbnail_quality'])) $_GET['thumbnail_quality'] = 85;
	if(!isset($_GET['thumbnail_save'])) $_GET['thumbnail_save'] = 1;
	
	// File exists?
	is_file($root . $_GET["image"]) ? $_GET["image"] = $root . $_GET["image"] : die('File not found.');
	
	// Cache dir exists?
	$cachedir = substr($_GET["image"], 0, strrpos($_GET["image"], '/') + 1) . 'cache/';
	if(!is_dir($cachedir) && !isset($_GET["nocache"])) mkdir($cachedir);
	
	// Get thumbnail path
	$imageinfo = pathinfo($_GET["image"]);
	$thumbnail = $cachedir . $imageinfo['basename'];
	
	// Get format
	if(stristr(strtolower($imageinfo['basename']),'.gif')) $format = 'gif';
	if(stristr(strtolower($imageinfo['basename']),'.jpg')) $format = 'jpg';
	if(stristr(strtolower($imageinfo['basename']),'.jpeg')) $format = 'jpg';
	if(stristr(strtolower($imageinfo['basename']),'.png')) $format = 'png';
	
	// Is image format?
	if(!isset($format)) die('File is not an image.');

/**
 * Return image
 *
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-03-29
 */

	if($_GET['thumbnail_save'] == 1 && is_file($thumbnail)) {
		header('content-type: image/' . $format);
		readfile($thumbnail);
	} else {
		include_once('thumbnail.inc.php');
		
		$thumb = new Thumbnail($_GET["image"]);
		
		// Resize (double thumbnail size if it's big enough for cropping)
		if($_GET['thumbnail_crop'] == 1) {
			if($_GET['thumbnail_size'] == $_GET['thumbnail_crop_width'] || $_GET['thumbnail_size'] == $_GET['thumbnail_crop_height'])
				$_GET['thumbnail_size'] = $_GET['thumbnail_size'] * ($_GET['thumbnail_rotation'] > 0 ? 3 : 2);
		}
		
		$thumb->resize($_GET['thumbnail_size'], $_GET['thumbnail_size']);
		
		// Rotation
		if($_GET['thumbnail_rotation'] > 0) {
			$rotation = round(180 * $_GET['thumbnail_rotation'] / 100);
			$thumb->rotateImage($_GET['thumbnail_rotate_cw'] == 1 ? 'CW' : 'CCW', $rotation);
		}
		
		// Crop
		if($_GET['thumbnail_crop'] == 1) {
			$crop = ($_GET['thumbnail_rotation'] > 0)
				? array($_GET['thumbnail_crop_width'], $_GET['thumbnail_crop_height'])
				: array($_GET['thumbnail_crop_width'] / 2, $_GET['thumbnail_crop_height'] / 2);
			
			($_GET['thumbnail_crop_width'] == $_GET['thumbnail_crop_height'] && $_GET['thumbnail_rotation'] == 0)
				? $thumb->cropFromCenter($_GET['thumbnail_crop_width'])
				: $thumb->crop($crop[0], $crop[1], $_GET['thumbnail_crop_width'], $_GET['thumbnail_crop_height']);
		}
		
		// Reflection
		if($_GET['thumbnail_reflection'] > 0) {
			$thumb->createReflection(40, 40, $_GET['thumbnail_reflection'], false, '', '#' . $_GET['thumbnail_background']);
		}
		
		// Output
		$thumb->show($_GET['thumbnail_quality']);
		if($_GET['thumbnail_save'] == 1) $thumb->save($thumbnail, $_GET['thumbnail_quality']);
		exit;
	}
	
?>