<?php
//<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
//sigurnost:
if (!defined('SIGURICA')) die('Nedozvoljen pristup!');



//check if image
$greska=1;
if ($_FILES['new_slika']['type'] == "image/pjpeg") {
	$slika_tip = 'jpg';
	$greska = 0;
	$img = imagecreatefromjpeg($_FILES['new_slika']['tmp_name']);
}
if ($_FILES['new_slika']['type'] == "image/jpeg") {
	$slika_tip = 'jpg';
	$greska = 0;
	$img = imagecreatefromjpeg($_FILES['new_slika']['tmp_name']);
}
if ($_FILES['new_slika']['type'] == "image/gif") {
	$slika_tip = 'gif';
	$greska = 0;
	$img = imagecreatefromgif($_FILES['new_slika']['tmp_name']);
}
if ($_FILES['new_slika']['type'] == "image/png") {
	$slika_tip = 'png';
	$greska = 0;
	$img = imagecreatefrompng($_FILES['new_slika']['tmp_name']);
}
if ($greska==0) {
		
	//generalni filename
	list($mday,$month,$year) = preg_split("( )",date("d m Y")); //za kreaciju imena slike u "func_img.php"
	$swatch = date("B");
	srand((double)microtime() * 1000000);
	$swatch .= rand(5, 16725);		
	//Get Image size info
    $width=imagesx($img);
	$height=imagesy($img);
	
	//IKONA
	//filename za thumbnail	
	$q_ikona_ime = $year.'_'.$month.'_'.$mday.'_'.$id_par.'_'.$swatch.'_i.'.$slika_tip;
	$putanja =$parametar_slike_direktorij.$q_ikona_ime;
	//kreiraj thumbnail
	$max_width=50;
	$max_height=33;	
	
	$x_ratio = $max_width / $width;
    $y_ratio = $max_height / $height;

    if( ($width <= $max_width) && ($height <= $max_height) ){
        $tn_width = $width;
        $tn_height = $height;
	}elseif (($x_ratio * $height) < $max_height){
         $tn_height = ceil($x_ratio * $height);
         $tn_width = $max_width;
	}else{
         $tn_width = ceil($y_ratio * $width);
         $tn_height = $max_height;
    }
	
	$q_ikona_sirina=$tn_width; //nova sirina za ikonu
	$q_ikona_visina=$tn_height;//nova visina za ikonu
	
	$newimg = imagecreatetruecolor($tn_width, $tn_height);
	if(($slika_tip == 'png') OR ($slika_tip=='gif'))
    {
        imagealphablending($newimg, false);
        imagesavealpha($newimg,true);
        $transparent = imagecolorallocatealpha($newimg, 255, 255, 255, 127);
        imagefilledrectangle($newimg, 0, 0, $tn_width, $tn_height, $transparent);
    }
    imagecopyresampled($newimg,$img,0,0,0,0,$tn_width, $tn_height,$width,$height);
	//spremi thumbnail
	switch ($slika_tip)
    {
        case 'gif': imagegif($newimg, $putanja); break;
        case 'jpg': imagejpeg($newimg, $putanja, 80);  break; // best quality
        case 'png': imagepng($newimg, $putanja, 0); break; // no compression
    }
	
	
	//THUMBNAIL
	//filename za thumbnail	
	$q_thumb_ime = $year.'_'.$month.'_'.$mday.'_'.$id_par.'_'.$swatch.'_t.'.$slika_tip;
	$putanja =$parametar_slike_direktorij.$q_thumb_ime;
	//kreiraj thumbnail
	$max_width=$q_thumb_sirina;
	$max_height=$q_thumb_visina;
	
	
	$x_ratio = $max_width / $width;
    $y_ratio = $max_height / $height;

    if( ($width <= $max_width) && ($height <= $max_height) ){
        $tn_width = $width;
        $tn_height = $height;
		$create_big='ne';
	}elseif (($x_ratio * $height) < $max_height){
         $tn_height = ceil($x_ratio * $height);
         $tn_width = $max_width;
	}else{
         $tn_width = ceil($y_ratio * $width);
         $tn_height = $max_height;
    }
	
	$q_thumb_sirina=$tn_width; //nova sirina za thumb
	$q_thumb_visina=$tn_height;//nova visina za thumb
	
	$newimg = imagecreatetruecolor($tn_width, $tn_height);
	if(($slika_tip == 'png') OR ($slika_tip=='gif'))
    {
        imagealphablending($newimg, false);
        imagesavealpha($newimg,true);
        $transparent = imagecolorallocatealpha($newimg, 255, 255, 255, 127);
        imagefilledrectangle($newimg, 0, 0, $tn_width, $tn_height, $transparent);
    }
    imagecopyresampled($newimg,$img,0,0,0,0,$tn_width, $tn_height,$width,$height);
	//spremi thumbnail
	switch ($slika_tip)
    {
        case 'gif': imagegif($newimg, $putanja); break;
        case 'jpg': imagejpeg($newimg, $putanja, 80);  break; // best quality
        case 'png': imagepng($newimg, $putanja, 0); break; // no compression
    }
	
	
	
	
	//UVECANA
	if ($create_big=='ne') {
		$q_big_sirina=0; //nova sirina za big
		$q_big_visina=0;//nova visina za big
		$q_big_ime='';//novo ime za big
	} else {
		//filename za uvecanu
		$q_big_ime = $year.'_'.$month.'_'.$mday.'_'.$id_par.'_'.$swatch.'_b.'.$slika_tip;
		$putanja =$parametar_slike_direktorij.$q_big_ime;
		
		//kreiraj uvecanu
		$max_width=$q_big_sirina;
		$max_height=$q_big_visina;
		
		$x_ratio = $max_width / $width;
		$y_ratio = $max_height / $height;
	
		if( ($width <= $max_width) && ($height <= $max_height) ){
			$tn_width = $width;
			$tn_height = $height;
		}elseif (($x_ratio * $height) < $max_height){
			 $tn_height = ceil($x_ratio * $height);
			 $tn_width = $max_width;
		}else{
			 $tn_width = ceil($y_ratio * $width);
			 $tn_height = $max_height;
		}
		
		$q_big_sirina=$tn_width; //nova sirina za uvecanu
		$q_big_visina=$tn_height;//nova visina za uvecanu
		
		$newimg = imagecreatetruecolor($tn_width, $tn_height);
		if(($slika_tip == 'png') OR ($slika_tip=='gif'))
		{
			imagealphablending($newimg, false);
			imagesavealpha($newimg,true);
			$transparent = imagecolorallocatealpha($newimg, 255, 255, 255, 127);
			imagefilledrectangle($newimg, 0, 0, $tn_width, $tn_height, $transparent);
		}
		imagecopyresampled($newimg,$img,0,0,0,0,$tn_width, $tn_height,$width,$height);
		//spremi uvecanu
		switch ($slika_tip)
		{
			case 'gif': imagegif($newimg, $putanja); break;
			case 'jpg': imagejpeg($newimg, $putanja, 80);  break; // best quality
			case 'png': imagepng($newimg, $putanja, 0); break; // no compression
		}
		
	}
} else {
	$error.='File koji ste uploadali nije slika!';
	$q_ikona_ime='';
	$q_ikona_sirina=0;
	$q_ikona_visina=0;
	$q_big_ime='';
	$q_thumb_ime='';
	$q_thumb_sirina=0;
	$q_thumb_visina=0;
	$q_big_ime='';
	$q_big_sirina=0;
	$q_big_visina=0;
}
?>
