<?php
chdir('..');
require_once ('api/Simpla.php');

$width = isset($_GET['w']) ? $_GET['w'] : 241;
$height = isset($_GET['h']) ? $_GET['h'] : 160;
$crop = isset($_GET['cr']) ? (int)$_GET['cr'] : 1;
$filename = 'files/categories/' . $_GET['file'];
$filename = str_replace('%2F', '/', $filename);
//die($filename);
//if(isset($_GET['cr'])){
	
	$file = Img::get($filename, array('width' => $width, 'height' => $height, 'crop' => $crop));

	header('Content-type: image');
    echo file_get_contents($_SERVER['DOCUMENT_ROOT'] . $file);
    exit;
	
	
//}












//die($filename);



$ext = mb_strtolower(substr(strrchr($filename, '.'), 1));
if($ext != 'png'){
	

//$token = $_GET['token'];
$filename = str_replace('%2F', '/', $filename);

if (substr($filename, 0, 6) == 'http:/')
    $filename = 'http://' . substr($filename, 6);

$simpla = new Simpla();
//if (!$simpla->config->check_token($filename, $token)){ exit('bad token'); }

$donefilename = $simpla->image->add_resize_params($filename, $width, $height, $set_watermark);



if (is_readable($simpla->config->categories_images_dir . $donefilename))
    { $resized_filename = $simpla->config->categories_images_dir . $donefilename;}
else
   {$resized_filename = $simpla->image->resizecat($filename);}
    
    
///echo $resized_filename.'---88';
///exit();

$resized_filename = ltrim($resized_filename,'/');

$name = substr($filename, 0, -strlen(strrchr ($filename, ".")));

if (is_readable($resized_filename) && $name != 'audi' && $name != 'dodge')
{
    header('Content-type: image');
    echo file_get_contents($_SERVER['DOCUMENT_ROOT'].'/'.$resized_filename);
    exit;
}

exit;
}









$filename = $_GET['file'];
$ext = mb_strtolower(substr(strrchr($filename, '.'), 1));
$name = substr($filename, 0, -strlen(strrchr ($filename, ".")));

$resized_filename = 'files/categories/'.$name.'.'.$width.'x'.$height.'.' . $ext;

if(file_exists($_SERVER['DOCUMENT_ROOT'].'/'.$resized_filename)) echoimg($_SERVER['DOCUMENT_ROOT'].'/'.$resized_filename);

if($ext == 'png'){
	
	if(!class_exists('SimpleImage')) include $_SERVER['DOCUMENT_ROOT'].'/api/SimpleImage.php';
	
	$img = new SimpleImage($_SERVER['DOCUMENT_ROOT'].'/files/categories/'.$filename);
	$img->best_fit($width, $height)->save($_SERVER['DOCUMENT_ROOT'].'/'.$resized_filename, 9);
	
	echoimg($_SERVER['DOCUMENT_ROOT'].'/'.$resized_filename);
	
}


$info = getimagesize($_SERVER['DOCUMENT_ROOT'].'/files/categories/'.$filename);
$default = 'h';
			$info = array(
            	'width'  => $info[0],
            	'height' => $info[1],
        	);

if($info['width'] < $width) echoimg($_SERVER['DOCUMENT_ROOT'].'/files/categories/'.$filename);




$image = '';

if ($ext == 'gif') $image = imagecreatefromgif($_SERVER['DOCUMENT_ROOT'].'/files/categories/'.$filename);
if ($ext == 'png') $image = imagecreatefrompng($_SERVER['DOCUMENT_ROOT'].'/files/categories/'.$filename);
if(!$image) $image = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'].'/files/categories/'.$filename);



		$xpos = 0;
		$ypos = 0;
		$scale = 1;

		$scale_w = $width / $info['width'];
		$scale_h = $height / $info['height'];

		if ($default == 'w') {
			$scale = $scale_w;
		} elseif ($default == 'h'){
			$scale = $scale_h;
		} else {
			$scale = min($scale_w, $scale_h);
		}

		if ($scale == 1 && $scale_h == $scale_w) {
			echoimg($_SERVER['DOCUMENT_ROOT'].'/files/categories/'.$filename);
		}

	//	$new_width = (int)($info['width'] * $scale);
	//	$new_height = (int)($info['height'] * $scale);
	
		$new_width = $width;
		$new_height = (int)($info['height']/($info['width']/$width)); 
	
	//print_r($info); echo $new_height.'x'.$new_width; exit;
				
    	$xpos = (int)(($width - $new_width) / 2);
   		$ypos = (int)(($height - $new_height) / 2);
        		        
       	$image_old = $image;
        $image = imagecreatetruecolor($width, $height);
		/*	
		if ($ext == 'png') {		
			imagealphablending($this->image, false);
			imagesavealpha($this->image, true);
			$background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
			imagecolortransparent($this->image, $background);
		} else {
			$background = imagecolorallocate($this->image, 255, 255, 255);
		}
		*/
		
		if($ext == 'png'){
			imagealphablending($image, false);
			imagesavealpha($image,true);
			
			$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
      		imagefilledrectangle($image, 0, 0, $new_width, $new_height, $transparent);
			
			imagecopyresampled($image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $info['width'], $info['height']);
			
			imagealphablending($image, false);
    		imagesavealpha($image,true);
			
			imagepng($image, $_SERVER['DOCUMENT_ROOT'].'/'.$resized_filename, 9);
		}else{
		
			$background = imagecolorallocate($image, 28, 30, 44);
			imagefilledrectangle($image, 0, 0, $width, $height, $background);
		
	        imagecopyresampled($image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $info['width'], $info['height']);
	        imagedestroy($image_old);
	        imagejpeg($image, $_SERVER['DOCUMENT_ROOT'].'/'.$resized_filename, 80);
        }
        imagedestroy($image);

echoimg($_SERVER['DOCUMENT_ROOT'].'/'.$resized_filename);

function echoimg($src){
	header('Content-type: image');
    echo file_get_contents($src);
    exit;
}