<?php

include_once "scripts/sb_gallery_config.php";
include_once "scripts/sb_gallery.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(validScaledExists($picture)){
    // There's already a valid scaled, so just show it.
    header("Content-Type: image/jpg");
    header("Content-Transfer-Encoding: binary");
    readfile(getScaledURL($picture));
    exit();
}
else{
    // Is it a picture?
  global $pic_extensions;
    if(preg_match($pic_extentions,$picture)){		
	// There's no valid scaled, so we need to generate it:
	// Check if GD is enabled. If not, exit with error
	global $gd_available;
	if($gd_available > 0){
	    header("Content-Type: image/jpg");
	    header("Content-Transfer-Encoding: binary");
	    createScaled($picture,true);
	    exit();
	}
	else{
	    header("Content-Type: image/gif");
	    header("Content-Transfer-Encoding: binary");
	    readfile("images/default_GDError.gif");
	    exit();
	}
    }
    else{
      global $other_extensions;
	// It's not a picture! Should a default scaled be displayed?
	if(preg_match($other_extentions,$picture)){
	  global $thumb_map;
	    foreach($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();
	}
    }
}
	
// createScaled($pic): creates a scaled for an specified image and saves it.
// $pic: path to the picture
// $output: send jpeg to output
function createScaled($pic,$output = true){
    global $data_directory;
    // Open the picture and get it's size
    if (preg_match("/\.(jpg|jpeg)/i",$pic)) {
	$image = imagecreatefromjpeg("$pic");
    } elseif (preg_match("/\.png/i",$pic)) {
		$image = imagecreatefrompng("$pic");
    } elseif (preg_match("/\.gif/i",$pic)) {
		$image = imagecreatefromgif("$pic");
    }

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

    include_once('scripts/sb_config.php');
    global $blog_config;
    read_config();
    
    // Calculate size of scaled depending on _height/_width
    //$new_height = $GLOBALS["max_picture_height"];
    //$new_width = $GLOBALS["max_picture_width"];
    $new_height = $blog_config["blog_max_pic_height"];
    $new_width = $blog_config["blog_max_pic_width"];

    if($image_x > $new_width || $image_y > $new_height){
	//decide which dimension to scale down:
	if(($image_x / $image_y) > ($new_width / $new_height)){
	    $new_height = ($new_width / $image_x) * $image_y;
	}
	else{
	    $new_width = ($new_height / $image_y) * $image_x;
	}
    }
    
    global $gd_available;
    // create empty scaled and copy(resized) the picture
    if($gd_available == 2){
	$scaled = imagecreatetruecolor($new_width, $new_height);
	imagecopyresampled($scaled, $image, 0, 0, 0, 0, $new_width, $new_height, $image_x, $image_y);
    }
    else{
	$scaled = imagecreate($new_width, $new_height); 
	imagecopyresized($scaled, $image, 0, 0, 0, 0, $new_width, $new_height, $image_x, $image_y);
    }

    global $server_scaled_caching;
    global $server_scaled_quality;
    if ($server_scaled_caching) {
	//create the neeeded subdirectories for this scaled
	$elements = explode("/", "scaled/$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 scaled
	imagejpeg($scaled, getScaledURL($pic), $server_scaled_quality);
    }
    if ($output) imagejpeg($scaled, '', $server_scaled_quality);	
    imagedestroy($image);
    imagedestroy($scaled);

}

// 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();
}

?>