Hey Budger,
Congratulations on the project so far.
Hope this will help you get somewhat further. (I actually re-wrote this this morning, so I am not sure it is completely without typos, but the general functionality should work.
The uploaded filename you can get from the $_FILE[] array, in this case you would want to use this array as the first argument in the function.
(Place print_r($FILE); on top of your page, and you will get an overview of what is in the $FILE array)
function upload_file($file_array, $type, $destination, $new_max_width, $new_max_height)
{
switch($type)
{
case "img" :
$original_picture = $file_array['file']['name']
$tmp_picture = $file_array['file']['tmp_name']
$new_picture = $destination;
$fullPath = $original_picture;
// ****************** Test variables / files for correct input *******************************
if (!file_exists($fullPath) ) // Check wether filesource exists
{
return("File doesn't exist in temporary folder on server!");
// The script has died here
}
if( file_exists($new_picture)) // Remove existsing file
{
$delete = @unlink($new_picture);
}
// ***************** Get file details **************************
$size = GetImageSize($fullPath);
$aspectRat = (float)($size[1] / $size[0]);
// ***************** Calculate new x/y size, maintaining original aspect ratio
if( ($size[0] <= $new_max_width) && ($size[1] <= $new_max_width) ) // Image < max size
{
return;
}
else
{
$width_ratio = $size[0] / $new_max_width;
$height_ratio = $size[1] / $new_max_height;
if($width_ratio >= $height_ratio) // width needs to be reduced more
{
$newY = $new_max_width * $aspectRat;
$newX = $new_max_width;
}
else
{
$newY = $new_max_height;
$newX = $new_max_height * (1/$aspectRat);
}
}
//create the destination image resource.
$destImg = ImageCreateTrueColor($newX, $newY );
//read the source image (original), use correct function depending on format
if ($size[2] == 1)
{
$sourceImg = ImageCreateFromGIF($fullPath);
}
if ($size[2] == 2)
{
$sourceImg = ImageCreateFromJPEG($fullPath);
}
else if ($size[2] == 3)
{
$sourceImg = ImageCreateFromPNG($fullPath);
}
else
{
return("This file cannot be used. It is not a jpg, gif or png file.<br>");
die(); // This is a double-trap. Return should have ended execution fo the scriopt already
}
// **************************** Create the new output file *******************
imagecopyresampled($destImg, $sourceImg, 0, 0, 0, 0, $newX, $newY, $size[0], $size[1]);
imagejpeg($destImg, $new_picture, $quality);
// *************************** Remove the temproary file *********************
$deletedfile = @unlink($original_picture);
Happy PhP-ing,
J.