Imagick is one of the popular tools for manipulating image files. Some popular languages such as PHP and Node.js have provided libraries that can be used for manipulating images based on Imagick. One of the common use cases for using Imagick is for generating a thumbnail from an image or PDF file.
In PHP, we can install the PHP Imagick module by running the following command.
apt install php-imagick
Then, we can verify the installation by running this command.
php -m | grep imagick
For example, we want to generate a thumbnail image for a PDF file in PHP. We can use the following script.
<?php
$im = new Imagick();
$im->setResolution(50, 50); // set the reading resolution before read the file
$im->readImage('file.pdf[0]'); // read the first page of the PDF file (index 0)
//$im = $im->flattenImages(); // @deprecated // handle transparency problem
$im = $im->mergeImageLayers( Imagick::LAYERMETHOD_FLATTEN );
$im->setImageFormat('png');
$im->writeImage('file.png');
If we want to set a specific size for the thumbnail. We can run the following script after flattening the image.
<?php
$imageprops = $im->getImageGeometry();
$width = $imageprops['width'];
$height = $imageprops['height'];
$baseHeight = 500;
$baseWidth = 250;
if($width > $height){
$newHeight = $baseHeight;
$newWidth = ceil($baseHeight * $width / $height);
}else{
$newWidth = $baseWidth;
$newHeight = ceil($baseWidth * $height / $width);
}
$im->resizeImage($newWidth, $newHeight, imagick::FILTER_LANCZOS, 0.9, true);
If we run the code above, PHP may throw an error that says something like:
ImagickException: not authorized `file.pdf' @ error/constitute.c/ReadImage/412
It is caused by the restriction policy of Imagick for a PDF file. A PDF file may contain a script (PostScript) that can perform harmful actions. Imagick utilizes GhostScript for interpreting scripts in a PDF file. We can configure the Imagick operation policy for specific file types by modifying the /etc/ImageMagick-6/policy.xml
file. You can read more detail about it here. Find the following declaration and modify it as you require.
<policy domain="coder" rights="none" pattern="EPS" />
<!--policy domain="coder" rights="none" pattern="PDF" /-->
<policy domain="coder" rights="none" pattern="XPS" />
<policy domain="coder" rights="read|write" pattern="PDF,PS" />
If we use PHP in a Windows environment, there are several procedures to make our program can result in the desired outcome.
Firstly, we need to download the DLL files of Imagick from the PECL site. The folder contains a lot of DLL files, not only the php_imagick.dll
file but also other files required by the operating systems. We can put the files in a subdirectory of the PHP ext
directory, for example, we put them in ext/imagick
.
Because it is not a standard directory for a PHP dynamic module, we configure the declaration of its usages using an absolute path in php.ini
.
[Imagick]
extension="C:\path\to\php\ext\imagick\php_imagick.dll"
Next step, we add a new record in the PATH
environment variable by location of the Imagick supported DLL files which is C:\path\to\php\ext\imagick
.
After the previous step, if we run the program, PHP still throws an error with a message that looks like this:
PDFDelegateFailed `The system cannot find the file specified. ' @ error/pdf.c/ReadPDFImage/801
It is caused by the absence of GhostScript support. So the last step, we need to download and install the GhostScript application from here. Then, we must add the bin
directory in the GhostScript installation folder into the PATH
environment variable. If we use a 64bit version, we need to make a copy of gswin64.exe
and rename the copy into gs.exe
. It is because Imagick will look for gs.exe
.
Comments
Post a Comment