<?php	// Copyright by Mateusz Rusin C 2004 // 1.04
function scale ($what, /*$where,*/ $max_x, $max_y) 
{
	if (!$max_x || !$max_y)
		return file_get_contents ($what);
	$size = GetImageSize ($what);
	if ($size [2] != 2)
		return false;
	$width = $size [0];
	$height = $size [1];
	$coefficient_x = $max_x / $width;
	$coefficient_y = $max_y / $height;
	if (($width <= $max_x) && ($height <= $max_y)) 
	{
		$new_width = $width;
		$new_height = $height;
	} 
	else if (($coefficient_x * $height) < $max_y) 
	{
		$new_height = ceil ($coefficient_x * $height);
		$new_width = $max_x;
	} 
	else 
	{
		$new_width = ceil ($coefficient_y * $width);
		$new_height = $max_y;
	}
	$source = ImageCreateFromJpeg ($what);
/*	if ($source)
		echo "<span>Otwieram plik $what </span>";
	else
		echo "<span class=\"error\">Niepowiodło się otwarcie pliku $what </span>";*/
	$r_img = ImageCreateTrueColor ($new_width, $new_height);
	ImageCopyResampled ($r_img, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
	ob_start ();	
	if (!ImageJpeg ($r_img/*, $where*/))
		return false;
	$image = ob_get_contents ();	
	ob_end_clean();
	ImageDestroy ($source);
	ImageDestroy ($r_img);
	return $image;
}
function crop ($what, $max_x, $max_y) 
{
	if (!$max_x || !$max_y)
		return file_get_contents ($what);
	$size = GetImageSize ($what);
	if ($size [2] != 2)
		return false;
	$width = $size [0];
	$height = $size [1];
	$coefficient_x = $max_x / $width;
	$coefficient_y = $max_y / $height;
	if (($width <= $max_x) && ($height <= $max_y)) 
	{
		$new_width = $width;
		$new_height = $height;
	} 
	else if (($coefficient_x * $height) > $max_y) 
	{
		$new_height = ceil ($coefficient_x * $height);
		$new_width = $max_x;
	} 
	else 
	{
		$new_width = ceil ($coefficient_y * $width);
		$new_height = $max_y;
	}
	$source = ImageCreateFromJpeg ($what);
	$r_img = ImageCreateTrueColor ($new_width, $new_height);
	ImageCopyResampled ($r_img, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
	ImageDestroy ($source);
	$c_img = ImageCreateTrueColor ($max_x, $max_y);
	ImageCopy ($c_img, $r_img, 0, 0, ($new_width > $max_x ? (($new_width - $max_x) / 2) : 0), ($new_height > $max_y ? (($new_height - $max_y) / 2) : 0), $max_x, $max_y);
	ImageDestroy ($r_img);
	ob_start ();	
	if (!ImageJpeg ($c_img))
		return false;
	$image = ob_get_contents ();	
	ob_end_clean();
	ImageDestroy ($c_img);
	return $image;
}
//<meta http-equiv="content-Type"content="text/html; charset=iso-8859-2">
?>