<?php
use Intervention\Image\ImageManagerStatic as Image;

$videosDao = new \Dao\Videos();
$videosDao->getVideoPorId($conecta, filter_input(INPUT_POST, 'id'));
$videosDao->titulo = $conecta->real_escape_string(filter_input(INPUT_POST, 'titulo'));
$videosDao->subtitulo = addslashes(filter_input(INPUT_POST, 'subtitulo'));
$videosDao->slug = \Dao\Funcoes::removeCharacters($videosDao->titulo, '-');
$videosDao->data_cadastro = date('Y-m-d H:i:s');
$videosDao->usuario_cadastrou = $_SESSION['id_usuario'];
$videosDao->data_alteracao = date('Y-m-d H:i:s');
$videosDao->usuario_alterou = $_SESSION['id_usuario'];
if (!empty($_FILES['image']['name'])) {
    if ($videosDao->image != '' &&file_exists($pasta . $videosDao->image))
        unlink($pasta . $videosDao->image);
    
    $videosDao->image = \Dao\Funcoes::randFileName($_FILES['image']['name']);
    $image = Image::make($_FILES['image']['tmp_name'])->resize(800, null, function ($constraint) {
        $constraint->aspectRatio();
    });

    $image->save($pasta . $videosDao->image, 85);
}
$res = json_decode($videosDao->salvar($conecta));
if ($res->data == 'Success') 
    \Dao\Funcoes::displayMessage('Confirmação', $res->message, 'videos.php');
else 
    \Dao\Funcoes::displayMessage('Erro', $res->message, 'videos.php', 'red');