Convert PDF files to images

Install the extension

imagick

  • Download: PECL :: Package :: Imagick (php.net)
  • Install: copy php_imagick. DLL to the configured extension directory (default is ext folder in PHP root).
  • Setup: in the php.ini file, add extension=php_imagick.dll

ImageMagick

  • Download: ImageMagick — Download
  • Install: The CORE_RL_ will be installed in the directory after the installation.dllThese files are copied to the PHP root directory

Ghostscript

  • Download: Ghostscript

Restart after the expansion is complete

Code sample

Public function pdf2img($pdf_path_folder, $PDf_name, $new_type = 'png') { $pdf_path = $pdf_path_folder . $pdf_name; $file_name_no_suff = str_replace(strrchr($pdf_name, "."),"",$pdf_name); / / file name (no suffix) $$new_type new_type = = = 'JPG' | | $new_type = = 'PNG'? $new_type : 'jpg'; $img_path = $pdf_path_folder . $file_name_no_suff . '.' .$new_type; $img_front; $img_front; $img_front = $pdf_path_folder . $file_name_no_suff . '_fimg.' .$new_type; $im = new \Imagick(); $im -> setResolution(300, 300); $im -> readImage($pdf_path); $im -> setImageFormat($new_type); $im -> setImageCompression(\Imagick::COMPRESSION_JPEG); $im -> setImageCompressionQuality(100); $im -> writeImage($img_front); $im -> clear(); $im -> destroy(); $img_front; $readImage = new \Imagick(); $readImage -> readImage($img_front); $blankPage = new \Imagick(); $blankPage -> newPseudoImage($readImage->getImageWidth(), $readImage->getImageHeight(), "canvas:white"); $blankPage -> compositeImage($readImage, Imagick::COMPOSITE_ATOP, 0, 0); $blankPage = $blankPage->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN); $blankPage -> writeImage($img_path); $blankPage -> destroy(); } catch (Exception $e) { var_dump(iconv("gbk",'utf-8',$e ->getMessage())); } return $img_path; }Copy the code