<?php
    error_reporting(0);

include("./php/setup.php");



    $fileName=$_FILES['file']['name'];
    $fileTmpLoc=$_FILES['file']['tmp_name'];
    $fileType=$_FILES['file']['type'];
    $fileSize=$_FILES['file']['size'];
    $fileErrorMsg=$_FILES['file']['error'];

    if(!$fileTmpLoc) {
        echo "Error: Please select a file to upload!";
        exit();
    }
    if($fileSize>$filemax) {
        echo "Error: Maximum filesize is 256M.";
        exit();
    }

    if(move_uploaded_file($fileTmpLoc,"files/$fileName")) {
        if(mime_content_type("files/$fileName")=="image/jpeg" || mime_content_type("files/$fileName")=="image/png" || mime_content_type("files/$fileName")=="image/gif") {
            //exec("/usr/local/bin/ffmpeg -i 'files/$fileName' -vf scale=200:-1 'thumbs/$fileName.jpg' -y");
            exec("/usr/local/bin/ffmpeg -i 'files/$fileName' -vf \"scale='trunc(min(1,min(200/iw,200/ih))*iw/2)*2':'trunc(min(1,min(200/iw,200/ih))*ih/2)*2'\" 'thumbs/$fileName.jpg' -y");
        } else {
            //exec("/usr/local/bin/ffmpeg -i 'files/$fileName' -vcodec mjpeg -vframes 1 -vf scale=200:-1 -an -f rawvideo 'thumbs/$fileName.jpg' -y");
            exec("/usr/local/bin/ffmpeg -i 'files/$fileName' -vcodec mjpeg -vframes 1 -vf \"scale='trunc(min(1,min(200/iw,200/ih))*iw/2)*2':'trunc(min(1,min(200/iw,200/ih))*ih/2)*2'\" -an -f rawvideo 'thumbs/$fileName.jpg' -y");
        }


        $shortName=$fileName;
        if(strlen($shortName)>32) $shortName=substr($shortName,0,32)."...";
        echo "Success: \"$shortName\" has been uploaded.";
    } else {
        echo "Error: File could not be uploaded!";
    }
?>
