Process PDF as Image with ImageMagick on Ionos Managed Server

In a former post about handling wkHTMLtoPDF on Ionos Managed Server/Webhosting I described a way how to make this all work again, after they updated their systems.

With that update, not only wkHTMLtoPDF broke, but also the handling of PDF files as image file with ImageMagick to create thumbnails of PDF files or something.

I finally found out how to fix it:

In your webroot (where you use ImageMagick with PHP), usually $_SERVER['DOCUMENT_ROOT'], create the folder .config/ImageMagick and put a file called policy.xml with the following content in there:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policymap [
  <!ELEMENT policymap (policy)+>
  <!ATTLIST policymap xmlns CDATA #FIXED ''>
  <!ELEMENT policy EMPTY>
  <!ATTLIST policy xmlns CDATA #FIXED '' domain NMTOKEN #REQUIRED
    name NMTOKEN #IMPLIED pattern CDATA #IMPLIED rights NMTOKEN #IMPLIED
    stealth NMTOKEN #IMPLIED value CDATA #IMPLIED>

]>

<!--
  Configure ImageMagick policies.
  Derived from /etc/ImageMagick-6/policy.xml.
-->

<policymap>
  <!-- <policy domain="system" name="shred" value="2"/> -->
  <!-- <policy domain="system" name="precision" value="6"/> -->
  <!-- <policy domain="system" name="memory-map" value="anonymous"/> -->
  <!-- <policy domain="system" name="max-memory-request" value="256MiB"/> -->
  <!-- <policy domain="resource" name="temporary-path" value="/tmp"/> -->
  <policy domain="resource" name="memory" value="256MiB"/>
  <policy domain="resource" name="map" value="512MiB"/>
  <policy domain="resource" name="width" value="16KP"/>
  <policy domain="resource" name="height" value="16KP"/>
  <!-- <policy domain="resource" name="list-length" value="128"/> -->
  <policy domain="resource" name="area" value="128MB"/>
  <policy domain="resource" name="disk" value="1GiB"/>
  <!-- <policy domain="resource" name="file" value="768"/> -->
  <policy domain="resource" name="thread" value="2"/>
  <!-- <policy domain="resource" name="throttle" value="0"/> -->
  <!-- <policy domain="resource" name="time" value="3600"/> -->
  <!-- <policy domain="coder" rights="none" pattern="MVG" /> -->
  <!-- <policy domain="module" rights="none" pattern="{PS,PDF,XPS}" /> -->
  <!-- <policy domain="delegate" rights="none" pattern="HTTPS" /> -->
  <!-- <policy domain="path" rights="none" pattern="@*" /> -->
  <!-- <policy domain="cache" name="memory-map" value="anonymous"/> -->
  <!-- <policy domain="cache" name="synchronize" value="True"/> -->
  <!-- <policy domain="cache" name="shared-secret" value="passphrase" stealth="true"/> -->
  <!-- <policy domain="system" name="pixel-cache-memory" value="anonymous"/> -->
  <!-- <policy domain="system" name="shred" value="2"/> -->
  <!-- <policy domain="system" name="precision" value="6"/> -->
  <!-- not needed due to the need to use explicitly by mvg: -->
  <!-- <policy domain="delegate" rights="none" pattern="MVG" /> -->
  <!-- use curl -->
  <policy domain="delegate" rights="none" pattern="URL" />
  <policy domain="delegate" rights="none" pattern="HTTPS" />
  <policy domain="delegate" rights="none" pattern="HTTP" />
  <!-- in order to avoid to get image with password text -->
  <policy domain="path" rights="none" pattern="@*"/>
  <!-- enable processing on data types PDF, PNG -->
  <policy domain="coder" rights="read | write" pattern="{PS,PS2,PS3,EPS,XPS,PDF,PNG}" />
  <policy domain="module" rights="read | write" pattern="{PS,PS2,PS3,EPS,XPS,PDF,PNG}" />
</policymap>

In your script, before you use ImageMagick set the environment variable HOME and it should work again.

putenv("HOME=/homepages/12/d123456789/htdocs"); // $_SERVER['DOCUMENT_ROOT']
// or
putenv("HOME=" . $_SERVER['DOCUMENT_ROOT']);

$cmd = '/usr/bin/convert /homepages/12/d123456789/htdocs/document.pdf /homepages/12/d123456789/htdocs/document.jpg';
shell_exec($cmd);

This is the German article in the Ionos Help Center that describes it in depth.