<?php
include('../../dao/auto_load.php');

use Intervention\Image\ImageManagerStatic as Image;

$conecta = new \Conn\MySqlConnect();

$id = filter_input(INPUT_POST, 'id');

$pasta = '../../../uploads/galerias/' . $id . '/';

if (!file_exists($pasta))
    mkdir($pasta, 0777);

$imagesDao = new \Dao\GaleriasImages();
$imagesDao->getImagensPorIdGaleria($conecta, $id);

$imagesDao->id = 0;
$imagesDao->fk_id_galeria = $id;
$imagesDao->image = \Dao\Funcoes::randFileName($_FILES['file']['name']);
$imagesDao->ordem = \Dao\Funcoes::getUltimoId($conecta, 'galerias_images', 'id');

$imagesDao->salvar($conecta);

$image = Image::make($_FILES['file']['tmp_name'])->resize(800, null, function ($constraint) {
    $constraint->aspectRatio();
});

$image->save($pasta . $imagesDao->image, 85);