Repairing image conversation with ImageMagick

Since Version 4.7, WordPress will create a screenshot of the first page of any uploaded PDF file. In order for that to work, ImageMagick and the PHP extension must be installed on the server.

On an Ubuntu 18 webserver I had some WordPress site runnig, the conversion was not working, even though both components were installed. In debugging the issue, I simply tried to do a conversation on the terminal:

convert test-pdf.pdf test-pdf.png

Running the command, I got the following error message:

convert: not authorized `test-pdf.pdf' @ error/constitute.c/ReadImage/412.
convert: no images defined `test-pdf.png' @ error/convert.c/ConvertImageCommand/3210.

After some research I found a solution for the issue. ImageMagick added a security policy some versions ago, which will limit the file types that are allowed to be converted. Unfortunately, the PDF extension is one of those, who can not be converted on Ubuntu 18 by default. But you can simply customize the file /etc/ImageMagick-6/policy.xml which defines the security policies:

<policymap>
  <!-- ... -->
  <policy domain="path" rights="none" pattern="@*" />
  <!-- disable ghostscript format types -->
  <policy domain="coder" rights="none" pattern="PS" />
  <policy domain="coder" rights="none" pattern="EPS" />
  <policy domain="coder" rights="none" pattern="PDF" />
  <policy domain="coder" rights="none" pattern="XPS" />
</policymap>

You can either comment the line with the PDF extension using an XML comment or change the rights to read for PDF:

<policymap>
  <!-- ... -->
  <policy domain="path" rights="none" pattern="@*" />
  <!-- disable ghostscript format types -->
  <policy domain="coder" rights="none" pattern="PS" />
  <policy domain="coder" rights="none" pattern="EPS" />
  <policy domain="coder" rights="read" pattern="PDF" />
  <policy domain="coder" rights="none" pattern="XPS" />
</policymap>

Now you can try again to convert file on the terminal. If the convertion was successful, the comment should not give you any (error) response. If that was successful, upload another PDF into the media library. The screenshot of the first page should now also work there again.

Posted by

Bernhard is a full time web developer who likes to write WordPress plugins in his free time and is an active member of the WP Meetups in Berlin and Potsdam.

Leave a Reply

Your email address will not be published. Required fields are marked *