<?php 
	function delete_image($dir, $field_name)
	{	
	$dh = opendir($dir);
	while (($file = readdir($dh)) !== false) {
	$split=explode('-',$file);
		if($split[0]==$field_name)
		{
			$myFile = $dir.$file;
			echo $split[0];
			unlink($myFile);
		}
	}
	}


function ImageUpload($destnation,$filename,$codeupname,$sizeWidth='400', $Givenheight='0')
 {
 	if ($_FILES[$filename]['name'][0] !="")
 	{
		  $unique_name = str_replace(' ','',$codeupname);
		 
 		if($_FILES[$filename]["error"][0] > 0)
 		{
   			return -1;		// file error 
 		}
 		else
  		{
 			$img_type = explode(".",$_FILES[$filename]["name"][0]);
 		  	$imgtype  = strtoupper($img_type[1]);
 			$uploadedfile =  str_replace(' ','',$_FILES[$filename]['tmp_name'][0]);
 			if($imgtype == 'JPG'||$imgtype == 'jpg'||$imgtype == 'jpeg' || $imgtype == 'JPEG')
 			{
 				$src = imagecreatefromjpeg($uploadedfile);
 			}
 			else if($imgtype == 'PNG'||$imgtype == 'png')
 			{
 				$src = imagecreatefrompng($uploadedfile);
 			}
 			else if($imgtype == 'GIF'||$imgtype == 'gif')
 			{
 				$src = imagecreatefromgif($uploadedfile);
 			}

			else if($imgtype == 'BMP'||$imgtype == 'bmp')
 			{
 				$src = imagecreatefromwbmp($uploadedfile);
 			}

 			list($width,$height)=getimagesize($uploadedfile);
			 
 			  $newwidth  = $sizeWidth;
 			if($Givenheight != '0')
			{
				$newheight = $Givenheight;
			}
			else
			{
				$newheight = ($height/$width)*$newwidth;
			}
			$tmp=imagecreatetruecolor($newwidth,$newheight);
			 
 			imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
 		  
  			 $image_path = $destnation.'/'.time().'.'.$imgtype;
			$image_path1 = $destnation.'/'.time().'.'.$imgtype; 
  			imagejpeg($tmp,$image_path ,90);
 			imagedestroy($src);
 			imagedestroy($tmp);  
			return $image_path1;  
		}
 	}
 	else
 	{
 		return 0; // no file persent 
 	}

}
?>