<?php
	
include_once('../db.php');
include_once('pages_secure.php');

// you should use POST or GET instead of REQUEST

$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";

// some security for your fileupload
$error = "";

// make filename
$filename = strtolower($path);

// allowed types
   $whitelist = array('jpg', 'png', 'gif', 'jpeg');

// check the image up against the whitelist
   if(!in_array(end(explode('.', $filename)), $whitelist)){ 
   		$error .= "This filetype is not allowed!<br>";  
	}   

// check size of image.. if it has no size, it is not an image...

list($width, $height) = getimagesize($_FILES['product_img']['tmp_name']);
  if( ($width < 100) || ($height < 100) ){
		$error .= "The image is too small!<br>";
  }
  if( (empty($width)) || (empty($height)) ){
	$error .= "The file has no height or width, so it can not be an image!<br>";  
  }

// ************************************************ this function is better to have in a own file called functions.php and load it with include when needed *******************

function reSize($img, $thumb_width, $thumb_height, $newfilename)
{


    //Check if GD extension is loaded
    if (!extension_loaded('gd') && !extension_loaded('gd2'))
    {
        trigger_error("GD is not loaded", E_USER_WARNING);
        return false;
    }

    //Get Image size info
    list($width_orig, $height_orig, $image_type) = getimagesize($img);
   
   
   $image_type = strtolower(substr($img, strrpos($img, ".") + 1));  
    
 	// Load image from file  
 	switch ($image_type)  
	 {  
     case 'jpg':  
         $im = imagecreatefromjpeg($img);  
         break;  
     case 'jpeg':  
         $im = imagecreatefromjpeg($img);  
         break;  
     case 'png':  
         $im = imagecreatefrompng($img);  
         break;  
     case 'gif':  
         $im = imagecreatefromgif($img);  
         break;  
     default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;
 	} 
	
	
	 $max_width = $thumb_width;
     $max_height = $thumb_height;
	 
	 $width = $width_orig;
     $height = $height_orig;
	 
	  /*** calculate the aspect ratio ***/
    $aspect_ratio = (float) $height_orig / $width_orig;

    /*** calulate the thumbnail width based on the height ***/
    $thumb_height = round($thumb_width * $aspect_ratio);
   

    while($thumb_height>$max_width)
    {
        $thumb_width-=10;
        $thumb_height = round($thumb_width * $aspect_ratio);
    }
	
	$newImg = imagecreatetruecolor($thumb_width, $thumb_height);
   
    /* Check if this image is PNG or GIF, then set if Transparent*/ 
    if(($image_type == "png") OR ($image_type== "gif"))
    {
        imagealphablending($newImg, false);
        imagesavealpha($newImg,true);
        $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
        imagefilledrectangle($newImg, 0, 0, $thumb_width, $thumb_height, $transparent);
    }
    imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig);
   
    //Generate the file, and rename it to $newfilename
    switch ($image_type)
    {
        case 'jpg': imagejpeg($newImg,$newfilename);  break;
        case 'jpeg': imagejpeg($newImg,$newfilename);  break;
        case 'png': imagepng($newImg,$newfilename); break;
		case 'gif': imagegif($newImg,$newfilename); break;
        default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;
    }
 	
	imagedestroy($newImg);
	imagedestroy($im);
	
    return $newfilename;

}

//****************************************  END OF FUNCTION **************************************************


// if no errors, move the file
   if($error == ""){
	   	// if we can move the file.. 
		if(@move_uploaded_file($_FILES['product_img']['tmp_name'], $path)){
			
			// source, width, height, target
			reSize($filename, 400, 400, $filename);
			
			
			$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();
			}

	

		
		}
   }
   else{
	
	// something went wrong, echo the error..
	echo "$error";
	   
   }

?>   
<br /><br />
<img src="images/ajax-loader.gif" />
 
		