<?php

include_once "scripts/sb_gallery_config.php";
include_once "scripts/sb_thumbnails.php";
include_once "scripts/sb_utility.php";

	
if(!isset($_REQUEST['picture'])){
    // No valid $picture parameter, exit with an error
    // A valid $picture parameter is the path to a picture.
    errorExit();
}
$picture = $_REQUEST['picture'];
	
if(validThumbnailExists($picture)){
    // There's already a valid thumb, so just show it.
    header("Content-Type: image/jpg");
    header("Content-Transfer-Encoding: binary");
    readfile(getThumbnailURL($picture));
    exit();
}
else{
  // Is it a picture?
    if(preg_match($GLOBALS['pic_extentions'],$picture)){		
	// There's no valid thumb, so we need to generate it:
	// Check if GD is enabled. If not, exit with error
	if($GLOBALS["gd_available"] > 0){
	    createThumbnail($picture);
	    header("Content-Type: image/jpg");
	    header("Content-Transfer-Encoding: binary");
	    readfile(getThumbnailURL($picture));
	    exit();
	}
	else{
	    header("Content-Type: image/gif");
	    header("Content-Transfer-Encoding: binary");
	    readfile("images/default_GDError.gif");
	    exit();
	}
    }
    else{
	// It's not a picture! Should a default thumb be displayed?
	if(preg_match($GLOBALS['other_extentions'],$picture)){
	    foreach($GLOBALS['thumb_map'] as $pat => $filename) {
		if (substr($pat,0,1)=="/" && preg_match($pat,$picture)) {
		    header("Content-Type: image/gif");
		    header("Content-Transfer-Encoding: binary");
		    readfile($filename);
		    exit();
		}
	    }
	}
	else{
	    errorExit();
	}
    }
}
	
// createThumbnail($pic): creates a thumbnail for an specified image and saves it.
// $pic: path to the picture
function createThumbnail($pic){
    global $album_directory, $data_directory, $thumb_width, $thumb_height;

    include_once('scripts/sb_config.php');

    global $blog_config;
    read_config();
    $thumb_width = $blog_config['blog_thumb_width'];
    $thumb_height = $blog_config['blog_thumb_height'];
    // Open the picture and get it's size
    if (preg_match("/\.(jpg|jpeg)/i",$pic)) {
	$image = imagecreatefromjpeg("$album_directory/$pic");
    } elseif (preg_match("/\.png/i",$pic)) {
	$image = imagecreatefrompng("$album_directory/$pic");
    } elseif (preg_match("/\.gif/i",$pic)) {
	$image = imagecreatefromgif("$album_directory/$pic");
    }

    $image_x = imagesx($image);
    $image_y = imagesy($image);

    // Calculate size of Thumbnail depending on $thumb_height/$thumb_width
    $new_height = $thumb_height;
    $new_width = $thumb_width;
    if($image_x > $thumb_width || $image_y > $thumb_height){
	//decide which dimension to scale down:
	if(($image_x / $image_y) > ($thumb_width / $thumb_height)){
	    $new_width = $thumb_width;
	    $new_height = ($thumb_width / $image_x) * $image_y;
	}
	else{
	    $new_height = $thumb_height;
	    $new_width = ($thumb_height / $image_y) * $image_x;
	}
    }
	
    // create empty thumbnail and copy(resized) the picture
    if($GLOBALS["gd_available"] == 2){
	$thumb = imagecreatetruecolor($new_width, $new_height);
	imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, $image_x, $image_y);
    }
    else{
	$thumb = imagecreate($new_width, $new_height); 
	imagecopyresized($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, $image_x, $image_y);
    }

	
    //create the neeeded subdirectories for this thumbnail
    $elements = explode("/", "thumbs/$pic");
    $cwd = getcwd();
    @chdir($data_directory);
    for($i = 0; $i < (count($elements) - 1); $i++){
	my_mkdir($elements[$i], 0777);
	@chdir($elements[$i]);
    }
    @chdir($cwd);
	
    // save the thumbnail
    imagejpeg($thumb, getThumbnailURL("$pic"), $GLOBALS["thumb_quality"]);
    imagedestroy($image);
    imagedestroy($thumb);
}

// errorExit(): exits the script showing an image indicating an error.
function errorExit(){
	header("Content-Type: image/gif");
	header("Content-Transfer-Encoding: binary");
	readfile("images/default_error.gif");
	exit();
}

?>