<?
// $breedte is nieuwe breedte
// $hoogte is nieuwe hoogte
// $imgFILE waar het plaatje van gemaakt wordt, het orgineel dus (bijv $_FILES['image']['tmp_name'])
// $imgDIR waar het plaatje naartoe verhuisd als ie gemaakt is
// $imgNAME nieuwe naam voor plaatje (bijv. mysql_insert_id () )
// $imgEXTENSION jpg of voor thumbs _th.jpg etc.
// $quality getal tussen de 0 en 100 waarbij 100 beste kwaliteit is

function create_new_JPGimage ($breedte, $hoogte, $imgFILE, $imgDIR, $imgNAME, $imgEXTENSION, $quality)
{					
//temp_photo voor thumbnail maken
$photo = imagecreatefromjpeg ($imgFILE);
	
//afmetingen bepalen
$source_x = imagesx($photo); 
$source_y = imagesy($photo); 

$target_x = $breedte;
$target_y = $hoogte;

//als foto te hoog is
if (($source_x/$target_x) < ($source_y/$target_y))
	{
	//echo "te hoog";
	$from_y = ceil(($source_y - ($target_y * $source_x / $target_x))/2);
	$from_x = '0';
	$source_used_y = ceil(($target_y * $source_x / $target_x)); 
	$source_used_x = $source_x;
	}

//als foto te breed is
if (($source_y/$target_y) < ($source_x/$target_x))
	{
	//echo "te breed";
	$from_x = ceil(($source_x - ($target_x * $source_y / $target_y))/2);
	$from_y = '0';
	$source_used_x = ceil(($target_x * $source_y / $target_y)); 
	$source_used_y = $source_y;
	}

//als verhoudingen gelijk zijn	
if (($source_x/$target_x) == ($source_y/$target_y))
	{
	$from_x = '0';
	$from_y = '0';
	$source_used_x = $source_x;
	$source_used_y = $source_y;
	}



//nieuwe image maken en wegschrijven naar dir

$create_blank = imagecreatetruecolor ($target_x, $target_y);

imagecopyresampled ($create_blank, $photo, 0, 0, $from_x, $from_y, $target_x, $target_y, $source_used_x, $source_used_y);

$new_filename = $imgDIR.$imgNAME.$imgEXTENSION;
				
ImageJpeg($create_blank,$new_filename,$quality);

Imagedestroy($photo);
}
?>