<?php
date_default_timezone_set("Asia/Kolkata"); 
$saveval=$_REQUEST['subbtn'];
include("opendb.php");
if($saveval=='Submit')
{ 
    $entrydt=date("Y-m-d h:i:s");
    
    $regno=$_REQUEST['regno'];
    
    $customer_name=$_REQUEST['customer_name'];
    $purps=$_REQUEST['purpos'];
    $remarks=$_REQUEST['customer_address'];
    $contactperson=$_REQUEST['tocontact'];
    $regno=strtoupper($regno);
    $purps=strtoupper($purps);
    $contactperson=strtoupper($contactperson);
    
   
    
  
    
        

    function upload_image($target,$source,$thumb_name,$type){
      //print "<script>alert('$source'); </script>";
	//@move_uploaded_file($source, $target);
	@move_uploaded_file($source, $thumb_name);
	list($width, $height) = getimagesize($thumb_name) ; 
	$modwidth = 528; 
	$diff = $width / $modwidth;
    $modheight = (int)($height / $diff); 
	$tn = imagecreatetruecolor($modwidth, $modheight) ; 
	if($type==1) $image = imagecreatefromgif($thumb_name) ; 
	if($type==2) $image = imagecreatefromjpeg($thumb_name); 
	if($type==3) $image = imagecreatefrompng($thumb_name) ; 
	@imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); 
	if($type==1) imagegif($tn, $thumb_name, 100);
	if($type==2) imagejpeg($tn, $thumb_name, 100);	
	if($type==3) imagepng($tn, $thumb_name, 100);
 }
    function compressImage($source, $destination, $quality) 
	{
		$info = getimagesize($source);
		if ($info['mime'] == 'image/jpeg')
		{
			$image = imagecreatefromjpeg($source);
		}
		elseif ($info['mime'] == 'image/gif')
		{
			$image = imagecreatefromgif($source);
		}
		elseif ($info['mime'] == 'image/png')
		{
			$image = imagecreatefrompng($source);
		}
		imagejpeg($image, $destination, $quality);
    } 
    if($_POST['image_form_submit'] == 2)
    {	
  
        $vehicle_id=$_REQUEST[vehicle_id];
        $vehicle_id=trim($vehicle_id);
	    $file_formats = array("jpg", "png", "gif", "bmp", "jpeg","JPG","JPEG", "PNG", "GIF", "BMP","TIF","pdf","docx");
	    $image_name   = $_FILES["images1"]["name"];
	    $extension = strtolower(pathinfo($image_name,PATHINFO_EXTENSION));
	
		$vechicleid=$inv;
	    $vechicleid1=$vechicleid.'_1';
		$imagename = $vechicleid1.".$extension";
		
		$size 		= $_FILES["images1"]["size"];
		$type 		= $_FILES["images1"]["type"];
		$error 	= $_FILES["images1"]["error"];
		if($extension=='gif')
		{	
			$type = 1;
		}
		if(($extension=='jpg')|| ($extension=='jpeg'))
		{
			$type = 2;
		}
		if($extension=='png') 
		{
			$type = 3;
		}
		$target_dir = "../large";
		$target_dirthumb = "thumb";
        $large_name = '../large/'.$imagename;
		$thumb_name = 'thumb/'.$imagename;
		$tmp_name 	= $_FILES['images1']['tmp_name'];
		$source = $_FILES['images1']['tmp_name'];
		if(file_exists($thumb_name))
	    {
			print "<script>alert('File Allready Uploaded; Image will be replaced.'); </script>";
	    }
		//else
		//{		
		if (strlen($thumb_name)) 
		{
			if (in_array($extension, $file_formats)) 
			{ 
				if ($size < (2048 * 1024)) 
				{
					    
			        $filename = $imagename;         
                        // Valid extension
                    $valid_ext = array('png','jpeg','jpg');
                        // Location
                    $location = "thumb/".$filename;
                        // file extension
                    $file_extension = pathinfo($location, PATHINFO_EXTENSION);
                    $file_extension = strtolower($file_extension);
                        // Check extension
                    if(in_array($file_extension,$valid_ext))
			        {  
				        // Compress Image
                        compressImage($_FILES['images1']['tmp_name'],$location,80);
                            $proceed1 = "true";	
                    }
			        else
			        {
                        echo "Invalid file type.";
                    }	
					    //upload_image($large_name,$source,$thumb_name,$type);
				}
				else 
				{
				    $filename = $imagename;         
                        // Valid extension
                    $valid_ext = array('png','jpeg','jpg');
                        // Location
                    $location = "thumb/".$filename;
                        // file extension
                    $file_extension = pathinfo($location, PATHINFO_EXTENSION);
                    $file_extension = strtolower($file_extension);
                        // Check extension
                    if(in_array($file_extension,$valid_ext))
			        {  
				        // Compress Image
                            //upload_image($large_name,$source,$thumb_name,$type);
                        compressImage($_FILES['images1']['tmp_name'],$location,80);
                            $proceed1 = "true";
                    }
			        else
			        {
                        echo "Invalid file type.";
                    }
					    //echo "Your image size is bigger than 2MB.";
				}
				if($proceed1=='true')
				{
			        
                        
                        mysqli_query($db,"update vehentry set filepath1='$thumb_name' where id='$inv'");
                    
				        $stringval=1;
						//echo '<img class="preview" alt="" src="'.$thumb_name.'" width=150px height=150px />';
						print "<script>alert('File Uploaded Successfully')</script>";
					}
					else 
					{
					    echo "Could not move the file.";
					    
					}
					
				}
				else {echo "Invalid file format.";	}
			}
			else {echo "Please select image..!";}
	 	 //}

print "<script> alert('Data has been saved');document.location='cshrec_16022021.php' </script>";
}
}
 
?>