Convert PDF to JPEG with PHP and ImageMagick

前端 未结 5 521
执念已碎
执念已碎 2020-11-27 04:55

I\'m using a litte script to convert PDF to JPG. That works but the quality is very poor.

The script:

$im = new imagick( \'document.pdf[ 0]\' ); 
$im         


        
5条回答
  •  有刺的猬
    2020-11-27 05:07

    Click here for more details. Try this:

    HTML

    
    
      
    
        

    PHP

    $pdfAbsolutePath = __DIR__."/test.pdf";
    
    if (move_uploaded_file($_FILES['templateDoc']["tmp_name"], $pdfAbsolutePath)) {
    
          $im             = new imagick($pdfAbsolutePath);
    
          $noOfPagesInPDF = $im->getNumberImages(); 
    
          if ($noOfPagesInPDF) { 
    
              for ($i = 0; $i < $noOfPagesInPDF; $i++) { 
    
                  $url = $pdfAbsolutePath.'['.$i.']'; 
    
                  $image = new Imagick($url);
    
                  $image->setImageFormat("jpg"); 
    
                  $image->writeImage(__DIR__."/".($i+1).'-'.rand().'.jpg'); 
    
              }
    
              echo "All pages of PDF is converted to images";
    
          }
          echo "PDF doesn't have any pages";
    
    }
    

提交回复
热议问题