简体   繁体   中英

Convert a PDF to jpeg on the fly with ImageMagick

I'm trying to convert the first page of a PHP generated PDF to an image, and have done so with the following code:

exec("convert http://####.com/tcpdf/examples/example_009.php[0] -resize 100 sample.jpeg");

However I don't want to save the image, I'm looking for a way of including the command in a PHP script in place of an image, eg: <img src="display_image_script.php?pdf=dynamic_pdf.php">

Is there a way to get ImageMagick to return the image within the PHP page using header('Content-Type: image/jpeg') ?

Untested, but try:

header('Content-type: image/jpeg');
passthru("convert somePdfFile.pdf jpeg:-");

You need the passthru to stream the binary back to the browser and the jpeg:- in the command string converts pdf to jpeg and returns the jpeg binary on stdout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM