<?php 
$loop=1; 
print $_FILES['pix']['name'][$loop]; 
function or_f($a, $b) { 
  return $a || $b; 
} 
//$editgallery holds the selected gallery key
function file_has_extension($fn, $ext) { 
  if(is_array($ext)) 
    return array_reduce(array_map(create_function('$a', 'return file_has_extension(\'' . $fn . '\', $a);'), $ext), 'or_f', false); 
  else 
    return stripos($fn, '.' . strtolower($ext)) === strlen($fn) - strlen($ext) + 1; 
} 

$image_extensions = array( 
  'png', 
  'jpg', 
  'jpeg', 
  'gif', 
  'bmp' 
); 
//echo "uploading...";
$loop="-1";while ($loop<5){$loop++; 
if ($_FILES['pix']['name'][$loop]=="") continue; 
    if($_FILES['pix']['tmp_name'][$loop] == "none") 
    { 
      echo "<b>File did not successfully upload. Check the 
            file size. File must be less than 500K.<br>"; 
      //include("picform.php"); 
      exit(); 
    } 
if(file_has_extension($_FILES['pix']['name'][$loop], $image_extensions)) 
    { 
      echo "<b>File is not a picture. Please try another 
            file.</b><br>"; 
      //include("file1.php"); 
      exit(); 
    } 
    else 
    { 
      //apply any cleanup processes to file name before saving
	  $destination = $_FILES['pix']['name'][$loop]; 
      $destination = strtolower($destination); 
	  
	  $destination = str_replace(" ", "_", $destination); 
	  $filename = $destination;
	  $destination = "../custom/gallery/" .$destination; 
	  
	  
      $rest=substr($destination,-3,3); 
      $temp_file = $_FILES['pix']['tmp_name'][$loop]; 
      move_uploaded_file($temp_file,$destination); 
      echo "<p><b>The file has successfully uploaded: $filename</b> </p>";
           // {$_FILES['pix']['name'][$loop]} 
           // ({$_FILES['pix']['size'][$loop]})</p>"; 
		//$img="http://dev.restaurant-online.biz/custom/gallery/$destination"; 
		
	//insert image record into gallery_images table
	$insert = "insert into gallery_images (gallerykey, filename, active) values ('$editgallery','$filename','1')";
	mysql_query($insert);
	
$img    = $destination; 
$width  = "100px"; 
$height = ""; 


// Get new dimensions 
list($width1, $height1) = getimagesize($img); 
if ($width =="") {$j=$height/$height1; $width  = $width1  * $j;} 
if ($height=="") {$j=$width/$width1;   $height = $height1 * $j;} 
$new_width = $width; 
$new_height = $height; 

// Resample 
$image_p = imagecreatetruecolor($width, $height); 
if ($rest=="png")     {$image = imagecreatefrompng($img);} 
elseif ($rest=="jpg") {$image = imagecreatefromjpeg($img);} 
elseif ($rest=="peg") {$image = imagecreatefromjpeg($img);} 
elseif ($rest=="gif") {$image = imagecreatefromgif($img);} 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width1, $height1); 

// Output 
//$img=preg_replace('/h.*\//','',$img); 
//$img=strtolower($img); 
$dest="../custom/gallery/thumbnails/$filename"; 
if     ($rest=="gif")imagegif($image_p,$dest); 

elseif ($rest=="png")imagepng($image_p,$dest); 

elseif ($rest=="peg")imagejpeg($image_p,$dest); 

elseif ($rest=="jpg")imagejpeg($image_p,$dest); 

    } 
  }
?>