<?php
chdir('..');
	require_once('api/Simpla.php');
	$simpla = new Simpla();
	$simpla->db->connect();
require_once $_SERVER['DOCUMENT_ROOT'].'/feedback/simple_image.php';

//print_r($_FILES);
//print_r($_POST);

$mtype = exif_imagetype($_FILES['img']['tmp_name']);
if($mtype && $mtype > 3) die;

$ext = substr(strrchr($_FILES['img']['name'], '.'), 1);

$size = getimagesize($_FILES['img']['tmp_name']);

$name = generateName() . '.' . $ext;

$path = $_SERVER['DOCUMENT_ROOT'].'/feedback/images/'.$name;

if($size[0] > 1024 || $size[1] > 1024){
	
	$img = new SimpleImage($_FILES['img']['tmp_name']);	
	$img->best_fit(1024, 1024)->save( $path );
	
}else{
	move_uploaded_file($_FILES['img']['tmp_name'], $path);
}

$simpla->db->query("INSERT INTO `__feedback_images` SET `feedback_id`='".(int)$_POST['id']."', `name`='$name' ");


	function generateName($length = 32){
	  $chars = 'abdefhiknrstyzABDEFGHKNQRSTYZ23456789';
	  $numChars = strlen($chars);
	  $string = '';
	  for ($i = 0; $i < $length; $i++) {
	    $string .= substr($chars, rand(1, $numChars) - 1, 1);
	  }
	  return md5($string.uniqid());
	}

