<?php
include('session.php');

$portfolio_images_dir = "../../uploads/portfolio/";

?>
<!doctype html>
<html lang="en">

<head>
<title>LaktusDev | Home</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="description" content="">
<meta name="author" content="LaktusDev, design by: laktusdev.com">

<link rel="icon" href="favicon.ico" type="image/x-icon">
<!-- VENDOR CSS -->
<link rel="stylesheet" href="assets/vendor/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/vendor/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="assets/vendor/animate-css/vivify.min.css">

<link rel="stylesheet" href="assets/vendor/chartist/css/chartist.min.css">
<link rel="stylesheet" href="assets/vendor/chartist-plugin-tooltip/chartist-plugin-tooltip.css">
<link rel="stylesheet" href="assets/vendor/c3/c3.min.css"/>
<link rel="stylesheet" href="assets/vendor/toastr/toastr.min.css">
<link rel="stylesheet" href="assets/vendor/jvectormap/jquery-jvectormap-2.0.3.min.css"/>
<link rel="stylesheet" href="assets/vendor/sweetalert/sweetalert.css"/>

<!-- MAIN CSS -->
<link rel="stylesheet" href="assets/css/mooli.min.css">

</head>
<body>

<?php

	// initialization
	$result_final = "";
	$counter = 0;

	// List of our known photo types
	$known_photo_types = array( 
						'image/pjpeg' => 'jpg',
						'image/jpeg' => 'jpg',
						'image/gif' => 'gif',
						'image/bmp' => 'bmp',
						'image/x-png' => 'png'
					);
	
	// GD Function List
	$gd_function_suffix = array( 
						'image/pjpeg' => 'JPEG',
						'image/jpeg' => 'JPEG',
						'image/gif' => 'GIF',
						'image/bmp' => 'WBMP',
						'image/x-png' => 'PNG'
					);

	// Fetch the photo array sent by preupload.php
	$photos_uploaded = $_FILES['slika'];

	// Fetch the photo caption array
	$state_photo_title = $_POST['opis'];

	while( $counter <= count($photos_uploaded) )
	{
		if($photos_uploaded['size'][$counter] > 0)
		{
			if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
			{
				$result_final .= "File ".($counter+1)." is not a photo<br />";
			}
			else
			{
				mysqli_query($leld, "INSERT INTO le_portfolio_photos(`slika`, `opis`) VALUES('0', '".addslashes($state_photo_title[$counter])."')" );
				$new_id = mysqli_insert_id($leld);
				$filetype = $photos_uploaded['type'][$counter];
				$extention = $known_photo_types[$filetype];
				$state_slika = 'portfolio';
				$filename = strtolower($state_slika."_".$new_id.".".$extention);

				mysqli_query($leld, "UPDATE le_portfolio_photos SET slika='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" );

				// Store the orignal file
				copy($photos_uploaded['tmp_name'][$counter], $portfolio_images_dir."/".$filename);

				// Let's get the Thumbnail size
				$size = GetImageSize( $portfolio_images_dir."/".$filename );
				// First Create Thumbnail!
				// Thumbnail Settings
						$Config_tbwidth_wide = 120; // width of wide image
						$Config_tbheight_wide = 112; // height of wide image
						$Config_tbwidth_tall = 112; // width of tall image
						$Config_tbheight_tall = 120; // height of tall image
				// The Code
						if($size[0] > $size[1]){
							$thumbnail_width = $Config_tbwidth_wide;
							$thumbnail_height = (int)($Config_tbwidth_wide * $size[1] / $size[0]);
							if($thumbnail_height > $Config_tbheight_wide){
								$thumbnail_height = $Config_tbheight_wide;
								$thumbnail_width = (int)($Config_tbheight_wide * $size[0] / $size[1]);
							}
						}else{
							$thumbnail_width = (int)($Config_tbheight_tall * $size[0] / $size[1]);
							$thumbnail_height = $Config_tbheight_tall;
							if($thumbnail_width > $Config_tbwidth_tall){
								$thumbnail_width = $Config_tbwidth_tall;
								$thumbnail_height = (int)($Config_tbwidth_tall * $size[1] / $size[0]);
							}
						}
				
				// Build Thumbnail with GD 2.x.x, you can use the other described methods too
				$function_suffix = $gd_function_suffix[$filetype];
				$function_to_read = "ImageCreateFrom".$function_suffix;
				$function_to_write = "Image".$function_suffix;
				// Read the source file
				$source_handle = $function_to_read ( $portfolio_images_dir."/".$filename );
				if($source_handle){
				// Let's create a blank image for the thumbnail
				$destination_handle = ImageCreateTrueColor ( $thumbnail_width, $thumbnail_height );
				// Now we resize it
				ImageCopyResampled( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] );
				}
				// Let's save the thumbnail
				$function_to_write( $destination_handle, $portfolio_images_dir."/tb_".$filename, 100 );
				ImageDestroy($destination_handle );
				// .................................................................................
				// This is the new code that checks for width and solves our problems
				// .................................................................................
				// Lets resize the image if its width is greater than 500 pixels
				// Resized image settings
				if ($size[0] > '200'){
						$Config_width_wide = 480; // width of wide image
						$Config_height_wide = 448; // height of wide image
						$Config_width_tall = 448; // width of tall image
						$Config_height_tall = 480; // height of tall image
				// The Code
						if($size[0] > $size[1]){
							$image_width = $Config_width_wide;
							$image_height = (int)($Config_width_wide * $size[1] / $size[0]);
							if($image_height > $Config_height_wide){
								$image_height = $Config_height_wide;
								$image_width = (int)($Config_height_wide * $size[0] / $size[1]);
							}
						}else{
							$image_width = (int)($Config_height_tall * $size[0] / $size[1]);
							$image_height = $Config_height_tall;
							if($image_width > $Config_width_tall){
								$image_width = $Config_width_tall;
								$image_height = (int)($Config_width_tall * $size[1] / $size[0]);
							}
						}
				
				// Build image with GD 2.x.x, you can use the other described methods too
				$function_suffix = $gd_function_suffix[$filetype];
				$function_to_read = "ImageCreateFrom".$function_suffix;
				$function_to_write = "Image".$function_suffix;
				// Read the source file
				$source_handle = $function_to_read ( $portfolio_images_dir."/".$filename );
				if($source_handle){
				// Let's create a blank image for the image
				$destination_handle = ImageCreateTrueColor ( $image_width, $image_height );
				// Now we resize it
				ImageCopyResampled( $destination_handle, $source_handle, 0, 0, 0, 0, $image_width, $image_height, $size[0], $size[1] );
				}
				// Let's save the image
				
				$function_to_write( $destination_handle, $portfolio_images_dir."/".$filename, 100 );
				ImageDestroy($destination_handle );
				}				

				$result_final .= "
				<div class='sweet-overlay' tabindex='-1' style='opacity: 1.02; display: block;'></div>
<div class='sweet-alert showSweetAlert visible' data-custom-class='' data-has-cancel-button='false' data-has-confirm-button='true' data-allow-outside-click='false' data-has-done-function='false' data-animation='pop' data-timer='null' style='display: block; margin-top: -165px;'><div class='sa-icon sa-error' style='display: none;'>
      <span class='sa-x-mark'>
        <span class='sa-line sa-left'></span>
        <span class='sa-line sa-right'></span>
      </span>
    </div><div class='sa-icon sa-warning' style='display: none;'>
      <span class='sa-body'></span>
      <span class='sa-dot'></span>
    </div><div class='sa-icon sa-info' style='display: none;'></div><div class='sa-icon sa-success animate' style='display: block;'>
      <span class='sa-line sa-tip animateSuccessTip'></span>
      <span class='sa-line sa-long animateSuccessLong'></span>

      <div class='sa-placeholder'></div>
      <div class='sa-fix'></div>
    </div><div class='sa-icon sa-custom' style='display: none; background-image: url(&quot;assets/images/sm/avatar1.jpg&quot;); width: 80px; height: 80px;'></div><h4>Portfolio dodat!</h4>
    <p style='display: block;'></p>
    <fieldset>
      <input type='text' tabindex='3' placeholder=''>
      <div class='sa-input-error'></div>
    </fieldset><div class='sa-error-container'>
      <div class='icon'>!</div>
      <p>Not valid!</p>
    </div><div class='sa-button-container'>
      <button class='cancel' tabindex='2' style='display: none; box-shadow: none;'>Cancel</button>
      <div class='sa-confirm-button-container'>
        <script>setTimeout(\"location.href = 'portfolio.php';\",2000);</script>
		<div class='la-ball-fall'>
          <div></div>
          <div></div>
          <div></div>
        </div>
      </div>
    </div></div>
				";
			}
		}
	$counter++;
	}

	// Print Result
echo 

	$result_final

?>

<!-- Javascript -->
<script src="assets/bundles/libscripts.bundle.js"></script>    
<script src="assets/bundles/vendorscripts.bundle.js"></script>

<!-- Vedor js file and create bundle with grunt  --> 
<script src="assets/vendor/sweetalert/sweetalert.min.js"></script><!-- SweetAlert Plugin Js -->

<!-- Project core js file minify with grunt --> 
<script src="assets/bundles/mainscripts.bundle.js"></script>
<script src="../js/pages/forms/editors.js"></script>
</body>
</html>