title: Convert .HEIC sequence to animated .GIF using Imagick
body: <p>I've managed to install Imagemagick 7 on my server and set-up the appropriate PHP module in order to work with HEIC and WebP files using Imagick.</p>
  
  <p>I can convert these files easily to other formats like PNG or JPEG without any problems.</p>
  
  <p>Now, I try to convert a HEIC sequence (<a href="https://github.com/alexcorvi/heic2any/blob/master/demo/3.heic?raw=true" rel="nofollow noreferrer">original file</a>) to GIF, but the result is imperfect.</p>
  
  <p>First off all, it seems, that Imagick isn't able to determine the delay between every image. This results in a lightning fast animation. So for now, I've hardcoded a delay of 40 ticks using <a href="https://www.php.net/manual/imagick.setimagedelay.php" rel="nofollow noreferrer">Imagick::setImageDelay</a>. I'm not sure, if I'm missing something, or if Imagick/Imagemagick isn't able to do this on HEIC sequences.</p>
  
  <p>The next problem I'm experiencing is, that Imagick can't handle the original image dimensions properly. To get around this, I have to get the original image's dimensions and perform <a href="https://www.php.net/manual/imagick.cropimage.php" rel="nofollow noreferrer">Imagick::cropImage</a> and <a href="https://www.php.net/manual/imagick.thumbnailimage.php" rel="nofollow noreferrer">Imagick::thumbnailImage</a> on every frame, before I finalize it with <a href="https://www.php.net/manual/de/imagick.setimagepage.php" rel="nofollow noreferrer">Imagick::setImagePage</a>.</p>
  
  <p>However, my biggest problem for now is, that the result has a broken, first frame:</p>
  
  <p><a href="https://i.stack.imgur.com/lE9Cj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lE9Cj.png" alt="broken first frame"></a></p>
  
  <p>So the final (broken) result looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/dQadT.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dQadT.gif" alt="final broken result"></a></p>
  
  <p>While the expected result (generated by <a href="https://alexcorvi.github.io/heic2any/" rel="nofollow noreferrer">heic2any</a>) is:</p>
  
  <p><a href="https://i.stack.imgur.com/UQXZx.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UQXZx.gif" alt="heic2any result"></a></p>
  
  <p>The (simplified) code I am using:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  if (!in_array('HEIC', \Imagick::queryFormats("HEI*"))) {
      throw new \Exception('Unsupported format');
  }
  
  $sourceFile = __DIR__ . '/3.heic';
  $targetFile = __DIR__ . '/3.gif';
  
  if (!file_exists($sourceFile)) {
      try {
          $content = file_get_contents('https://github.com/alexcorvi/heic2any/blob/master/demo/' . basename($sourceFile) . '?raw=true');
  
          if ($content === false) {
              throw new \Exception('Unable to download source file.');
          }
  
          file_put_contents($sourceFile, $content);
      } catch (\Exception $e) {
          throw new \Exception('Unable to download source file: ' . $e-&gt;getMessage());
      }
  
      if (!file_exists($sourceFile)) {
          throw new \Exception('Cannot find source file.');
      }
  }
  
  $im = new \Imagick($sourceFile);
  $width = $im-&gt;getImageWidth();
  $height = $im-&gt;getImageHeight();
  $im = $im-&gt;coalesceImages();
  
  foreach ($im as $frame) {
      $im-&gt;setImageDelay(40);
  
      $frame-&gt;cropImage($width, $height, 0, 0);
      $frame-&gt;thumbnailImage($width, $height);
      $frame-&gt;setImagePage($width, $height, 0, 0);
  }
  
  $im = $im-&gt;deconstructImages();
  $im-&gt;writeImages($targetFile, true);
  
  echo 'Size: ' . $width . 'x' . $height . '&lt;br /&gt;Result:&lt;br /&gt;&lt;br /&gt;&lt;img src="' . basename($targetFile) . '?v=' . time() . '" alt="" /&gt;';
  </code></pre>
  
  <p>So my questions are:</p>
  
  <ul>
  <li>Where does the 1st frame come from?</li>
  <li>How can I avoid it?</li>
  <li>Is there anything to optimize the conversion (code)?</li>
  </ul>
  
  <p>UPDATE:</p>
  
  <p>There's another .HEIC sequence to play with (<a href="https://github.com/alexcorvi/heic2any/blob/master/demo/15.heic?raw=true" rel="nofollow noreferrer">click</a>) and this one can be converted without problems:</p>
  
  <p><a href="https://i.stack.imgur.com/r8uOM.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r8uOM.gif" alt="result"></a></p>
  
  <p>UPDATE:</p>
  
  <p>Even conversion services like <a href="https://cloudconvert.com/heic-to-gif" rel="nofollow noreferrer">cloudconvert</a> can't handle the image correctly and producing GIF animations like this:</p>
  
  <p><a href="https://i.stack.imgur.com/7eUMR.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7eUMR.gif" alt="whoops"></a></p>