If you (or your client) hosts their websites in Ionos then you may have stumbled upon the problem that the imagick library is not present. Bummer. Especially if you need to automatically convert the first page of an uploaded PDF file to an image and display it somewhere… for example as a preview of the pdf.
Here I come to help you with this problem.
First you need to access your ionos webspace through ssh. Install a service like putty and then login using the admin ftp credentials that IONOS gave you. To paste the credentials quickly use the keyboard shift + INS. For me right clicking on the putty window to paste or simply cntl+v didin’t work.
So when you are logged in shift+ins these commands one after the other waiting for them to finish without errors:
- wget http://www.imagemagick.org/download/ImageMagick.tar.gz
- tar xfvz ImageMagick.tar.gz
- ls (lists the directories inside the root folder of your webspace. Find something like ImageMagick-7.1.1-38
- cd ImageMagick-7.1.1-38/
- get the absolute path by typing pwd. The result should be sth like
- /kunden/homepages/30/d339922114/htdocs/ImageMagick-7.1.1-38
- ./configure –prefix=/kunden/homepages/30/d339922114/htdocs/ImageMagick-7.1.1-38 use the path that you got from the pwd command
- make
- make install
You have succesfully installed the library in the root folder of your Ionos webspace. Through ftp or through Ionos you can delete the tar file that has been downloaded during the process.
Now, in my case I used the acf plugin to create a file field that outputs an array (not url, but array).
In my template File, where I wanted to show the preview of the pdf I inserted this code
<?php // Assuming the field name is 'download_link'
$pdf = get_field('download_link'); // ACF returns an array for the file field
if($pdf) {
$pdf_path = get_attached_file($pdf['id']); // Get the full path of the uploaded PDF
// Define paths
$uploaded_pdf = $pdf_path; // Path to the uploaded PDF file
$output_image = wp_upload_dir()['path'] . '/' . basename($uploaded_pdf, '.pdf') . '-page1.jpg'; // Define the output image path
putenv("HOME=/homepages/30/d339922114/htdocs");
// Build the command for Imagick
$cmd = '/usr/bin/convert ' . escapeshellarg($uploaded_pdf) . '[0] ' . escapeshellarg($output_image);
// Run the command
shell_exec($cmd);
// Define the URL of the generated image
$image_url = wp_upload_dir()['url'] . '/' . basename($uploaded_pdf, '.pdf') . '-page1.jpg';
// Output the image in your template
if (file_exists($output_image)) {
echo '<img src="' . esc_url($image_url) . '" alt="PDF First Page">';
} else {
echo 'Failed to generate image from PDF.';
}
}
?>
Attention: Correct the putenv(“HOME=/homepages/30/d339922114/htdocs”); part with the absolute path that you got during the imagick installation. Now, if you upload a pdf through the corresponding ACF field the magic will happen. You can thank me later.