title: Saving Each PDF Page to an Image Using Imagick
body: <p>I have the following php function below that's converting a local PDF file into images. In short, I want each PDF page to be converted to a separate image.</p>
  
  <p>The function converts the PDF to an image - but only the last page. I want <strong><em>every</em></strong> page of the PDF to be converted to a image and numbered. Not just the last page of the PDF.</p>
  
  <p>Currently, this function converts the <strong><em>last</em></strong> page of <code>example.pdf</code> to <code>example-0.jpg</code>. Issue I'm sure lies within the <code>for</code> method. What am I missing?</p>
  
  <pre><code>$file_name = 'example.pdf'; // using just for this example, I pull $file_name from another function
  
  function _create_preview_images($file_name) {
  
      // Strip document extension
      $file_name = basename($file_name, '.pdf');
  
      // Convert this document
      // Each page to single image
      $img = new imagick('uploads/'.$file_name.'.pdf');
  
      // Set background color and flatten
      // Prevents black background on objects with transparency
      $img-&gt;setImageBackgroundColor('white');
      $img = $img-&gt;flattenImages();
  
      // Set image resolution
      // Determine num of pages
      $img-&gt;setResolution(300,300);
      $num_pages = $img-&gt;getNumberImages();
  
      // Compress Image Quality
      $img-&gt;setImageCompressionQuality(100);
  
      // Convert PDF pages to images
      for($i = 0;$i &lt; $num_pages; $i++) {         
  
          // Set iterator postion
          $img-&gt;setIteratorIndex($i);
  
          // Set image format
          $img-&gt;setImageFormat('jpeg');
  
          // Write Images to temp 'upload' folder     
          $img-&gt;writeImage('uploads/'.$file_name.'-'.$i.'.jpg');
      }
  
      $img-&gt;destroy();
  }
  </code></pre>