<?php
session_start();
$brcode=$_SESSION['brcode'];
include("opendb.php");
$yearval=$_REQUEST[yearvl];
$stringval=0;
$testing=$_FILES['images1']['name'];
//echo $testing;
$saveval=$_POST['subbtn'];
//echo $saveval;

	
  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_category=$_REQUEST[vehicle_category];
    $vehicle_brand=$_REQUEST[vehicle_brand];
    $vehicle_model=$_REQUEST[vehicle_model];
    $vehicle_km=$_REQUEST[vehicle_km];
    $vehicle_year=$_REQUEST[vehicle_year];
    $vehicle_specs=$_REQUEST[vehicle_specs];
    $vehicle_id=$_REQUEST[vehicle_id];
    
		$file_formats = array("jpg", "png", "gif", "bmp", "jpeg","JPG","JPEG", "PNG", "GIF", "BMP","TIF","pdf","docx");
		$imagename   = $_FILES["images1"]["name"];
		$extension = strtolower(pathinfo($imagename,PATHINFO_EXTENSION));
		
		  
		
		
		$size 		= $_FILES["images1"]["size"];
		$type 		= $_FILES["images1"]["type"];
		$error 	= $_FILES["images1"]["error"];
		
		$target_dir = "../large";
		$target_dirthumb = "../thumb";
        $large_name = '../large/'.$imagename;
		$thumb_name = 'thumb/'.$imagename;
		$newFileName = md5(time() . $brcode) . '.' . $extension;
		$thumb_name = 'thumb/'.$newFileName;
		$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 < (4096 * 1024)) 
					{
				        $finfo = finfo_open(FILEINFO_MIME_TYPE);
			            $mimetype = finfo_file($finfo, $_FILES['images1']['tmp_name']);	    
					    
					    if ($mimetype == 'image/jpg' || $mimetype == 'image/jpeg' || $mimetype == 'image/gif' || $mimetype == 'image/png') 
					{
						compressImage($_FILES['images1']['tmp_name'],$thumb_name,50);
						$stringval=1;
						echo '<img class="preview" alt="" src="'.$thumb_name.'" width=150px height=150px />';
						echo '<input type="text" name="imgflnm" id="imgflnm" value="'.$newFileName.'">';
						print "<script>alert('File Uploaded Successfully')</script>";
						
					}
					else
					{
					    
					    
					    
					    
					    	if (move_uploaded_file($_FILES["images1"]["tmp_name"], $thumb_name)) {
					    //upload_image($large_name,$source,$thumb_name,$type);	
						
			
						    $stringval=1;
							echo '<img class="preview" alt="" src="'.$thumb_name.'" width=150px height=150px />';
							echo '<input type="hidden" name="imgflnm" id="imgflnm" value="'.$newFileName.'">';
							print "<script>alert('File Uploaded Successfully')</script>";
						}else {echo "Could not move the file.";}
					}
					}
					else {echo "Your image size is bigger than 4MB.";}
				}
				else {echo "Invalid file format.";	}
			}
			else {echo "Please select image..!";}
	 	 //}
}









if($stringval=='1')
{
    //echo $testing;
//mysqli_query($db,"insert into vehicles(vehicle_category,vehicle_brand,vehicle_model,vehicle_km,vehicle_year,vehicle_specs) values('$vehicle_category','$vehicle_brand','$vehicle_model','$vehicle_km','$vehicle_year','$vehicle_specs')");
}

?>