<?php
//error_reporting(E_ALL);
ini_set('display_errors', 'off');
// Create an array containing file paths, resource var (initialized with imagecreatefromXXX), 
// image URLs or even binary code from image files.
// All sorted in order to appear.

//$array=json_decode($_POST['camarray']);

// Use an array containing file paths, resource vars (initialized with imagecreatefromXXX), 
// image URLs or binary image data.

//$elements = $_POST['data'];
$elements = $_POST['data'];
$numimages = $_POST['images'];
$delay = $_POST['delay'];
$lastdelay = $_POST['lastdelay'];
$maxh = $_POST['maxh'];
$maxv = $_POST['maxv'];

if (! is_numeric($maxh)) { 
$maxh = 500;

}

if (! is_numeric($maxv)) {
$maxv = 400;

}



$numimages = $numimages - 1;
//$elements = json_decode($elements);


$inputfiles = "";

//foreach ($elements as &$value) {
//    $newin = $value. ":";
//    $inputfiles = $inputfiles.$newin;
//}
//$inputfiles = rtrim($inputfiles, ":");
//echo $inputfiles;
//echo "ffmpeg -i concat:{$inputfiles} output.avi 2>&1";
//echo shell_exec("ffmpeg -i concat:{$inputfiles} output.avi 2>&1");
foreach ($elements as $value) {
    if ($value != $elements[array_key_last($elements)]) {
    $inputfiles = $inputfiles . " -delay {$delay} {$value}";
    }
    if ($value == $elements[array_key_last($elements)]) {
    $inputfiles = $inputfiles . " -delay {$lastdelay} {$value}";
    }
}

//echo "convert -delay 20 {$inputfiles} output.gif 2>&1";
$gif = shell_exec("convert {$inputfiles} -resize {$maxh}x{$maxv}\> -layers Optimize gif:-");







echo base64_encode($gif);
exit;






?>
