<?php
require_once __DIR__ . '/common.php';

// This service expects POST data with specific parameters
$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;

$inputfiles = "";

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}";
    }
}

$gif = shell_exec("convert {$inputfiles} -resize {$maxh}x{$maxv}\> -layers Optimize gif:-");

echo base64_encode($gif);
exit;
?>