0

I am trying to create a jpg preview of a pdf file as outlined in this question: How do I convert a PDF document to a preview image in PHP?

The relevant code to do so is:

$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;

I am running this on my localhost on a pdf file but get the error:

Fatal error: Uncaught exception 'ImagickException' with message 'Imagick::__construct(): HTTP request failed! HTTP/1.0 400 Bad Request '

The error is being triggered here:

$im = new imagick(build_url('uploads/files/'.$file_data['file_name'].'[0]'));

This is the same as the first line in the example code above, and I am providing the full url path to the pdf file, when I echo that it provides the correct path

http://oursite.localhost:8888/uploads/files/file_name.pdf[0]

Does anyone know what is causing this error? Thank you!

Community
  • 1
  • 1
Dan
  • 9,391
  • 5
  • 41
  • 73
  • If you use a relative path? `'./uploads/file/'.$file_data['file_name'].'[0]'`? – Damien Pirsy Jun 06 '14 at 20:03
  • Thanks for the suggestion...getting "no such file or directory" when I try that. However, I just discovered this http://stackoverflow.com/questions/9783216/convert-postscript-delegate-failed so I'm going to try that – Dan Jun 06 '14 at 20:05
  • first of all your telling imagick to load it from a URL... not the local filesystem... the URL you are requesting is getting an HTTP error from apache saying bad request most likely becuase [0] is not liked with mod security... get rid of the build_url – xxcezz Jun 06 '14 at 20:06
  • Show your function `build_url()` it seems to build a wrong url – nl-x Jun 06 '14 at 20:10
  • `build_url()` creates our urls and it works correctly. When I echo `build_url('uploads/files/'.$file_data['file_name'].'[0]');` I get `http://oursite.localhost:8888/uploads/files/file_name.pdf[0]` – Dan Jun 06 '14 at 20:12
  • @Dan you missed the point... you are telling imagemagick to not load from the local filesystem and rather a URL... and the URL it is requesting... is getting a 400 bad request error... this means pasting http://oursite.localhost:8888/uploads/files/file_name.pdf[0] into a browser should cause a 400 bad request error – xxcezz Jun 06 '14 at 20:13
  • When I use relative path I still get an error, I get "no such file or directory" error – Dan Jun 06 '14 at 20:15
  • @dan what happens in your browser if you go to http://oursite.localhost:8888/uploads/files/file_name.pdf[0] ? – nl-x Jun 06 '14 at 20:16
  • @dan of course you will get 'no such file or directory' if it includes the http:// in the path name. We will need to see the file structure to even help period – xxcezz Jun 06 '14 at 20:17
  • I noticed that with imagick readimage function the suffix [0] creates fatal error in some pdf. – Davide Aug 17 '15 at 14:30

1 Answers1

5

ImageMagick is quite bad as HTTP client. Pull down your image first, then feed it into ImageMagick as blob

Use following:

 $image = file_get_contents(build_url('uploads/files/'.$file_data['file_name'].'[0]'));
 if ($image !== false)
   {
     $im = new imagick();
     $im->readImageBlob($image);
   }
 else
   {
     echo "Uh-oh... Cannot load image from URL!";
   }
Vladimir Bashkirtsev
  • 1,334
  • 11
  • 24
  • Crank up **error_reporting** - **file_get_contents()** will provide you human readable error message in plain English if something went wrong while pulling the image in – Vladimir Bashkirtsev Jun 06 '14 at 20:23