<?php


$age = 3 * 24 * 60 * 60;
header("Cache-Control: max-age=$age, must-revalidate");
header("Pragma: public");
header("Expires: " . date('r', time() + $age));
header("Vary: Accept-Encoding, User-Agent");
//session_cache_limiter("public");

function endsWith($haystack, $needle) {
    $length = strlen($needle);
    return $length > 0 ? substr($haystack, -$length) === $needle : true;
}

function createFile($file){
	$image = null;
	
	$mime = mime_content_type($file);
	
	if (endsWith(mb_strtolower($mime),"/png")){
		$image = imagecreatefrompng($file);
	}else if (endsWith(mb_strtolower($mime),"/jpg") || endsWith(mb_strtolower($mime),"/jpeg")){
		$image = imagecreatefromjpeg($file);
	}else if (endsWith(mb_strtolower($mime),"/gif")){
		$image = imagecreatefromgif($file); 
		imagepalettetotruecolor($image);
	}else if (endsWith(mb_strtolower($mime),"/webp")){
		$image = imagecreatefromwebp($file);
	}
	
	
	
	return $image;
}

function resize($file, $w, $h){
	//header("Content-Type: text/plain");
	
	$image = createFile($file);
	
	$cw = imagesx($image);
	$ch = imagesy($image);
	
	//400 x 200
	//ratio = 400 / 200 = 2
	//w = h * ratio
	//h = w / ratio
	
	$ratio = $cw / $ch;
	
	if ($cw > $ch){
		$h = $w / $ratio;
	}else{
		$w = $h * $ratio;
	}
	
	
	return imagescale($image , $w, $h);
	
	
}


$type = 'page';

if (isset($_GET['type'])){
	$type = $_GET['type'];
}

$image = urldecode((string)$_GET['name']);
$image = str_replace(array('../', '/..'), '_', $image);

$tmpImagePath = "core/tmp/imgs/";

if (is_link($image)){
	echo "link";exit;
}

switch ($type){
	case 'pages':
	case 'page': 
		$file = 'images/pages/' . $image;
	break;
	
	case 'products':
	case 'product':
		$file = 'images/products/' . $image;
	break;
	
	case 'manufacturers':
	case 'manufacturer':
		$file = 'images/manufacturers/' . $image;
	break;
	
	case 'categories':
	case 'category':
		$file = 'images/categories/' . $image;
	break;
	
	case 'payments':
	case 'payment':
		$file = 'images/payments/' . $image;
	break;
	
	case 'shop':
		$file = 'images/shop/' . $image;
	break;
		

}

$file_with_size = str_replace(".","_" . $_GET['size'] . ".",$file);
$fileParts = explode(".", $file_with_size);
$file_with_new_extension = $fileParts[0] . ".webp";

$tmpImage = $tmpImagePath . $file_with_new_extension;



if ( !is_file($file) ) {
	/*header("HTTP/1.1 404 Not Found");
	exit();*/
	$file = 'images/sv/no_picture.jpg';
}else{
	$modified = filemtime($file);
	header("Last-Modified: " . date('r', $modified));

	
	switch ($_GET['size']){
		case 'listing':
			//if (!file_exists($file_with_new_extension)){
				header('Content-Type: image/webp');
				imagewebp(resize($file,450,450), null, 95);
				exit;
			//}else{
			//	$file = $file_with_new_extension;
			//}
			
		break;
		
		case 'small':
			header('Content-Type: image/webp');
			imagewebp(resize($file,125,125), null, 100);
			exit;
		break;
		
		case 'icon':
			header('Content-Type: image/webp');
			imagewebp(resize($file,200,200), null, 100);
			exit;
		break;
		
		case 'payment':
			header('Content-Type: image/webp');
			imagewebp(resize($file,100,55), null, 100);
			exit;
		break;
			
		case 'brand':
			header('Content-Type: image/webp');
			imagewebp(resize($file,180,60), null, 90);
			exit;
		break;
		
		//case 'icon':
		case 'original':
		
		break;
		
		case 'large':
			header('Content-Type: image/webp');
			imagewebp(resize($file,1200,1200), null, 100);
			exit;
		break;
		
		case 'logo':
			header('Content-Type: image/webp');
			imagewebp(resize($file,790,150), null, 100);
			exit;
		break;
		
		default: 
			/*
			if (!file_exists($tmpImage)){
				imagewebp(createFile($file), $tmpImage, 100);
				
			}
			
			if (file_exists($tmpImage)){
				header('Content-Type: image/webp');
				imagewebp(createFile($tmpImage), null, 100);
			}else{
				header('Content-Type: image/webp');
				imagewebp(createFile($file), null, 100);
			}*/
			
			header('Content-Type: image/webp');
			imagewebp(createFile($file), null, 100);
			
			exit;
		break;
		
	}
}

$modified = filemtime($file);
  
header("Last-Modified: " . date('r', $modified));
header("Content-type: " . mime_content_type($file));
header("Content-Length: " . filesize($file));
header("Content-Disposition: inline; filename=".$name." ");
readfile($file);



?>