Valid for Web Hosting, Hosting for WordPress, and Managed Dedicated Servers.

ImageMagick is a powerful collection of command line tools that can be used to edit images. This allows you to automate tasks such as converting, scaling, or modifying images directly in your webspace. This is ideal for processing uploads in content management systems, or dynamically adapting images using scripts.

In this article, we will introduce you to the most common use cases. You will learn:

  • How to carry out basic image processing (converting, scaling) via SSH.

  • How to enable PDF support for your webspace, which is disabled by default for security reasons.
  • How to manage PDF conversion securely with PHP scripts.

Requirement

Basic image editing via SSH

The central tool of ImageMagick is theconvert command. You can use this command to edit images directly in the terminal.

Here are some practical application examples:
 

Convert image format

ImageMagick can read and write images in over 200 formats, including PNG, JPEG, JPEG 2000, GIF, TIFF, DPX, EXR, WebP, PostScript, PDF, and SVG

To convert an image to another format, use the convert command according to the scheme convert <source file> <destination file>. The format to which the file is converted is determined by the file extension of the target file. 

For example, to copy the image example.jpg in JPEG format to PNG format, enter the command as follows:

convert example.jpg example.png


It works the same way the other way round (PNG → JPEG):

convert example.png example.jpg

 

Scale image (change resolution)

You can also use ImageMagick to change the resolution and therefore the size of an image. To do this, use the convert command together with the resize parameter.

For example, to halve the resolution of the image file example.png, use the following command:

convert -resize 50% example.png

The following command scales the image so that it fits into a rectangle of 480x95 pixels, whereby the original aspect ratio is retained. The result is therefore a maximum width of 480 pixels and a maximum height of 95 pixels.

convert -resize 480x95 example.png

By default, ImageMagick retains the aspect ratio of the image to avoid distortion. However, if you want to force the image to the exact target size, even if this leads to distortions, add an exclamation mark (!) directly after the size specification.

convert -resize 480x95! example.png

It is also possible to scale images directly when converting them to another format. Here is an example of the combination of format conversion (JPEG → PNG) and scaling by 50%.

convert example.jpg -resize 50% example.png

 

 

Note

Please note in connection with scaling that the resolution in pixels is meant here, and not the display size on the screen.

  • If you reduce the resolution by 50%, an image with 1000 x 1000 pixels becomes an image with 500 x 500 pixels. Instead of one million pixels, you then only have 250,000 pixels, i.e. a quarter of the pixels of the original image.

  • If you compare the original with the scaled version directly next to each other on the screen, the scaled image only occupies a quarter of the area of the original image. In other words: in this case, the original image is four times as large as the scaled image.

Activate PDF support

For security reasons, the processing of PDF files is deactivated in the standard configuration of ImageMagick. However, you can enable this function specifically for your webspace by creating your own configuration file:

  • Create the following subdirectory in the main directory (/) of your web space. You can do this with an FTP client or directly via SSH with the following command:

    mkdir -p ~/.config/ImageMagick

  • Create a file with the name policy.xml in this new directory. You can use the nano editor via SSH, for example:

    nano ~/.config/ImageMagick/policy.xml

  • Copy the following XML code completely into the policy.xml file. This code overwrites the standard policies and allows read and write access for formats such as PDF, PS and EPS.

    <?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>
    ]>
    <policymap>
       <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="area" value="128MB"/>
       <policy domain="resource" name="disk" value="1GiB"/>
       <policy domain="resource" name="thread" value="2"/>
       <policy domain="delegate" rights="none" pattern="URL" />
       <policy domain="delegate" rights="none" pattern="HTTPS" />
       <policy domain="delegate" rights="none" pattern="HTTP" />
       <policy domain="path" rights="none" pattern="@*"/>
       <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>

  • Save the policy.xml file. If you have created the file locally, upload it via SFTP to the ~/.config/ImageMagick directory.

PDF support for ImageMagick is now active. The changes will take effect immediately.

Using PDF conversion in PHP scripts

If you want to call ImageMagick from a PHP script to process PDFs, an additional step is required. PHP scripts do not know the path to your policy.xml file by default. You must make it known via the HOME environment variable.

The absolute path to your webspace directory (document root) is required. You can find out how to determine this in the article Determining the absolute path (document root) of your webspace.

The following sample PHP script shows how to set the environment variable, and then convert a PDF file into a JPG file.

<?php

// Replace this path with the absolute path to your webspace directory.
$home_path = "/homepages/12/d123456789/htdocs";

// Sets the HOME environment variable so that ImageMagick finds the policy.xml.
putenv("HOME=" . $home_path);

// Define the full path to the files and the convert tool.
$pdf_file = $home_path . "/document.pdf";
$jpg_file = $home_path . "/preview.jpg";
$convert_path = "/usr/bin/convert";

// Assemble the command and execute it.
// The addition [0] ensures that only the first page of the PDF is converted.
$command = $convert_path . " " . escapeshellarg($pdf_file . "[0]") . " " . escapeshellarg($jpg_file);
$output = shell_exec($command);

// Optional: Display output or error (for debugging)
if ($output) {
   echo "Error output: " . $output;
} else {
   echo "Conversion started successfully.";
}

?>

Please Note

The shell_exec() function executes shell commands directly on the server. Always make sure that no unsanitized user input (e.g. file names from an upload form) is entered directly into the command in order to avoid security vulnerabilities such as command injection. Use functions such as escapeshellarg() to secure file paths.