6

I want to convert .pdf file to .png file using Imagemagick php API.

we can do this from shell using this:

$convert sample.pdf sample_image.png

we can issue this command using php exec() function but due to some reason (security) I disabled the execution of shell commands using php.

so now tell me the solution that how can i convert my .pdf file to .png file without using the php exec() function?

There is another discussion about this here but it's not very clear.

Peeyush
  • 4,728
  • 16
  • 64
  • 92

2 Answers2

21

you must have installed php5-imagick

$myurl = 'filename.pdf['.$pagenumber.']';
$image = new Imagick($myurl);
$image->setResolution( 300, 300 );
$image->setImageFormat( "png" );
$image->writeImage('newfilename.png');
Luis Melgratti
  • 11,881
  • 3
  • 30
  • 32
  • This works great for me, except it throws an exception in writeImage. header("Content-type image/png"); echo $image; solves the problem. – Jeff Hines Dec 23 '11 at 00:05
  • I have no Idea anymore. ImageMagick 6.8.8-9 is installed but I don't get any image from my pdf. If I change `$myurl = 'filename.pdf['.$pagenumber.']'` to an image I get a new image with the name `$image->writeImage('newfilename.png');` as expected. But I get nothing from my pdf. Maybe someone has any Idea what could go wrong. – Yves Nov 21 '14 at 17:41
  • You need to set the resolution before you read the image. Just FYI. – Nik Aug 07 '19 at 12:53
  • setResolution() must be used before the file read, hence use readImage() to get the file after Imagic initialization in the order `$image = new Imagick(); $image->setResolution( 30, 30); $image->readImage($myurl); ` – Clain Dsilva Sep 17 '21 at 11:55
1

but due to some reason(security) i disabled the execution of shell commands using php

You'll either need to re-enable the execution of shell commands, or install the ImageMagick PHP extension. See here on how to install it.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • ImageMagick PHP extension is already installed but i need some simple example code for the solution of my problem,so please explain it with a PHP code example. – Peeyush Sep 30 '10 at 17:39