title: Creating PDF thumbnail in PHP and caching it
body: <p>I'm using a generic PHP based CMS, i wanted to create a script which read the pdf created a thumbnail and cached it. There were lots of different answers, and i did have a fair few problems with different versions of imagick, but this is script which worked for me.</p>
  
  <p>some people might find it useful and maybe someone could advice me if it is optimised?</p>
  
  <pre><code>&lt;?php
  
  $loc = *the file location*;
  $pdf = *the file name*;
  $format = "jpg";
  $dest = "$loc$pdf.$format";
  
  if (file_exists($dest))
          {
          $im = new imagick();
          $im-&gt;readImage($dest);
          header( "Content-Type: image/jpg" );
          echo $im;
          exit;
          }
  else
      {
          $im = new imagick($loc.$pdf.'[0]');
          $im-&gt;setImageFormat($format);
  
          $width = $im-&gt;getImageheight();
          $im-&gt;cropImage($width, $width, 0, 0);
          $im-&gt;scaleImage(110, 167, true);
  
          $im-&gt;writeImage($dest);
  
          header( "Content-Type: image/jpg" );
          echo $im;
          exit;
      }
  
  ?&gt;
  </code></pre>