// Place in document within an if stmnt that fires if upload form submitted
// $img_max_width = set to max desired width
// $img_max_height = set to max desired height
// $img_dir = set to path to upload file to NO trailing slash!!!
// $resize = Default is 0, Set to 1 to resize uploaded files
// $thumbnail = Default is 0, Set to 1 to create thumbnails
function file_uploader($img_max_width, $img_max_height, $img_th_max_width, $img_th_max_height, $img_dir, $resize = 0, $thumbnail = 0 ) {
$image_name = date ('Ymd-His'); // Create unique name using date/time
$extension = explode ('.', $_FILES['file_to_upload']['name']); // Get the file extension from the uploaded file
$filename = $image_name .'.'. strtolower ($extension[1]); // Build the LARGE image's filename
// If the file was uploaded to the temp directory, move to the user specified directory
if (move_uploaded_file ($_FILES['file_to_upload']['tmp_name'], "$img_dir/$filename")) {
// If the resize option was set, resize the uploaded image to make a final version
if ($resize == 1) {
$imagemagick_path = "/usr/bin/convert"; // Set the path to ImageMagick
$size = getimagesize ($img_dir ."/". $filename); // Get the uploaded images dimensions
if ($size[0] > $size[1]) { // Is a Wide Image
$image_width = $img_max_width;
$image_height = (int)($img_max_width * $size[1] / $size[0]);
$photo_orientation = "h";
} else { // Is a Tall Image
$image_width = (int)($img_max_height * $size[0] / $size[1]);
$image_height = $img_max_height;
$photo_orientation = "v";
}
exec ("$imagemagick_path -geometry " . "{$image_width}x{$image_height} " . "$img_dir/$filename $img_dir/$filename");
// If the thumbnail option was set, resize the uploaded image to make a final version
if ($thumbnail == 1) {
// Build the SMALL image's filename by appending "_th" to its name
$filename_th = $image_name .'_th.'. strtolower ($extension[1]);
if ($size[0] > $size[1]) { // Is a Wide Image
$th_width = $img_th_max_width;
$th_height = (int)($img_th_max_width * $size[1] / $size[0]);
} else { // Is a Tall Image
$th_width = (int)($img_th_max_height * $size[0] / $size[1]);
$th_height = $img_th_max_height;
}
exec ("$imagemagick_path -geometry " . "{$th_width}x{$th_height} " . "$img_dir/$filename $img_dir/$filename_th");
}
}
return TRUE;
} else {
return FALSE;
}
}