Kirby 3 Plugin Cloudconvert

site/config/config.kirby3-plugins.bnomei.com.php

<?php

function customConvertHook($file) {
    if($file->extension() == 'gif') {

        $file->cloudconvert(
            [
                'inputformat' => 'gif',
                'outputformat' => 'webm',
                'save' => true, // keep file at cloud to avoid another download from cloud-server
                'converteroptions' => [
                    // https://stackoverflow.com/questions/45588064/error-transparency-encoding-with-auto-alt-ref-does-not-work-when-converting-a#45588400
                    'command' => '-i {INPUTFILE} {OUTPUTFILE} -auto-alt-ref 0',
                ],
            ]
        );

        $file->cloudconvert(
            [
                'inputformat' => 'gif',
                'outputformat' => 'mp4',
            ]
        );
    }
}

return [
    'debug' => false,
    'cache' => [
        'driver' => 'memcached'
    ],

    'bnomei.cloudconvert.apikey' => function() {
        return env('CLOUDCONVERT_APIKEY');
    },
    'bnomei.thumbimageoptim.apikey' => function() {
        return env('IMAGEOPTIM_APIKEY');
    },

    'hooks' => [
        'file.create:after' => function($file) {
            customConvertHook($file);
        },
        'file.replace:after' => function($newFile, $oldFile) {
            customConvertHook($newFile);
        },
    ],

    'bnomei.securityheaders.csp' => require_once(__DIR__.'/csp.php'),
];

site/templates/cloudconvert.php

<?php
/** @var Kirby\Cms\App $kirby */
/** @var Kirby\Cms\Site $site */
/** @var Kirby\Cms\Page $page */
?><!DOCTYPE html>
<html <?= site()->attrLang() ?>
>
  <head><?php snippet('plugin-htmlhead') ?></head>

  <body>
  <?php
    $github = lapse(md5($page->id()), function() {
      $m = 'https://raw.githubusercontent.com/bnomei/kirby3-plugins.bnomei.com/master/site/';
      return [
        'config/config.kirby3-plugins.bnomei.com' => html(Kirby\Http\Remote::get(
            $m.'config/config.kirby3-plugins.bnomei.com.php'
          )->content()),
        'templates/cloudconvert' => html(Kirby\Http\Remote::get(
          $m.'templates/cloudconvert.php'
        )->content()),
      ];
    }); ?>

  <h1><?= $page->title()->html() ?></h1>

  <h2>site/config/config.kirby3-plugins.bnomei.com.php</h2>
  <pre><code data-language="php"><?= $github['config/config.kirby3-plugins.bnomei.com'] ?></code></pre>

  <h2>site/templates/cloudconvert.php</h2>
  <pre><code data-language="php"><?= $github['templates/cloudconvert'] ?></code></pre>

  <h2>synchronously docx to pdf</h2>
  <blockquote>
  <?php
    if ($fileWord = $page->file('test.docx')) {
      $filePDF = $fileWord->cloudconvert(
        [
          'inputformat' => 'docx',
          'outputformat' => 'pdf',
        ],
        str_replace('.docx', '.pdf', $fileWord->root()),
        false // wait for conversion to be done
      );
      echo $fileWord->url().'<br>';
      if ($filePDF) {
        echo $filePDF->url().'<br>';
      }
    }
  ?>
  </blockquote>

  <h2>asynchronously jpg to webp</h2>
  <blockquote>
  <?php
    if ($fileJpg = $page->file('flowers.jpg')) {
      echo $fileJpg->url().'<br>';
      $process = $fileJpg->cloudconvert(
        [
          'inputformat' => 'jpg',
          'outputformat' => 'webp',
        ],
        str_replace('.jpg', '.webp', $fileJpg->root())
      // NOTICE: no 'false' here!
      );
    }
    // NOTICE: file might not exists immediately
    if ($fileWebp = $page->file('flowers.webp')) {
      echo $fileWebp->url().'<br>';
    } else {
      echo 'does not exist yet.<br>';
    }
  ?>
  </blockquote>

  <?php snippet('footer') ?>
</body>
</html>

synchronously docx to pdf

https://kirby3-plugins.bnomei.com/media/pages/cloudconvert/122556815-1549451194/test.docx
https://kirby3-plugins.bnomei.com/media/pages/cloudconvert/-1542537038-1549456750/test.pdf

asynchronously jpg to webp

https://kirby3-plugins.bnomei.com/media/pages/cloudconvert/-450850316-1549470704/flowers.jpg
https://kirby3-plugins.bnomei.com/media/pages/cloudconvert/-35941703-1549463246/flowers.webp