<?php

    $root=$_SERVER['DOCUMENT_ROOT'];

    $path="";
    $hash="";
    $idx="";
    if(isset($_GET['p'])) {
        $path=stripslashes(htmlspecialchars($_GET['p']));
        $hash=md5($path);
        $idx=stripslashes(htmlspecialchars($_GET['i']));
    }
    $path="s/".$path;

    $arr=array(
        "path" => $path,
        "thumb" => "thumbs/".$hash.".jpg",
        "idx" => $idx
    );
    $fullpath=$root."/".$arr['path'];
    $fullthumb=$root."/".$arr['thumb'];
    //echo "do file: ".$root.$path."<br />";
    //echo "output: ".$root."thumbs/".$hash.".jpg"."<br />";
    //echo "idx: ".$idx."<br />";

    //echo $fullpath;
    //exit;
    //if($hash!="d41d8cd98f00b204e9800998ecf8427e") { // empty
    //
    //}

    if(file_exists($fullpath)) {
        $mime=mime_content_type("$fullpath");
        $size=512;

        if($mime=="image/jpeg" || $mime=="image/png" || $mime=="image/gif") {
            //exec("/usr/local/bin/ffmpeg -i 'files/$fileName' -vf scale=200:-1 'thumbs/$fileName.jpg' -y");
            exec("ffmpeg -i '$fullpath' -vf \"scale='trunc(min(1,min($size/iw,$size/ih))*iw/2)*2':'trunc(min(1,min($size/iw,$size/ih))*ih/2)*2'\" '$fullthumb' -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("ffmpeg -i '$fullpath' -vcodec mjpeg -vframes 1 -vf \"scale='trunc(min(1,min($size/iw,$size/ih))*iw/2)*2':'trunc(min(1,min($size/iw,$size/ih))*ih/2)*2'\" -an -f rawvideo '$fullthumb' -y");
        }

        
        echo json_encode($arr,JSON_FORCE_OBJECT);
    }

?>
