<?php
include_once('../db.php');
include_once('pages_secure.php');

$cat_id= mysql_real_escape_string($_REQUEST['cat_id']);
$product_name = mysql_real_escape_string($_REQUEST['product_name']);
$article_number = mysql_real_escape_string($_REQUEST['article_number']);
$product_size = mysql_real_escape_string($_REQUEST['product_size']);
$product_color = mysql_real_escape_string($_REQUEST['product_color']);
$filename = $_FILES['product_img']['name'];
$path= "../images/products/$filename";

$fileuploaded = move_uploaded_file($_FILES['product_img']['tmp_name'], $path);
if($fileuploaded){
//gd
$src = imagecreatefromjpeg($path);
//list($width,$height)=getimagesize($path);
$imagesize = getimagesize($path);
$width = $imagesize[0];
$height = $imagesize[1];

$target_wh=400;

if ($width > $height) { 
$percentage = ($target_wh / $width); 
} else { 
$percentage = ($target_wh / $height); 
} 


$newwidth = round($width * $percentage); 
$newheight = round($height * $percentage);

//$newwidth=200;
//$newheight=200;

$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
$smallfilename = "../images/products/detail_".$filename;
imagejpeg($tmp,$smallfilename,100);

// New Image 400 width
$target_wh=190;

if ($width > $height) { 
$percentage = ($target_wh / $width); 
} else { 
$percentage = ($target_wh / $height); 
} 


$newwidth = round($width * $percentage); 
$newheight = round($height * $percentage);

//$newwidth=200;
//$newheight=200;

$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
$smallfilename = "../images/products/thumb_".$filename;
imagejpeg($tmp,$smallfilename,100);

imagedestroy($src);
imagedestroy($tmp);

}

$query= mysql_query("INSERT into products (cat_id, product_img ,product_name, article_number ,product_size, product_color) values ('$cat_id' , '$filename', '$product_name', '$article_number', '$product_size', '$product_color')");
if($query) {
	echo "Product Added Successfully.". header("Refresh: 1 ".$_SERVER['HTTP_REFERER']);;
} else {
	echo "Error! ".mysql_error();
}
?>
<br /><br />
<img src="images/ajax-loader.gif" />
