[PHP+ImageMagick] Convert PDF to image (detailed steps)

藏色散人
Release: 2023-04-10 21:20:01
forward
5877 people have browsed it

ImageMagick installation

ImageMagick is a free software for creating, editing, and synthesizing pictures. It can read, convert, and write images in multiple formats. Picture cutting, color replacement, application of various effects, picture rotation, combination, text, straight lines, polygons, ellipses, curves, extension and rotation attached to pictures. ImageMagick is free software: all source code is open and can be used, copied, modified, and distributed freely. It complies with the GPL license agreement and can run on most operating systems. Most of the functions of ImageMagick come from command line tools.

To use ImageMagick in PHP, you need to install the imagick extension. imagick is similar to the gd extension and is mainly used for image processing. But imagick is more powerful. The following is a brief introduction to the installation methods of imagick in two common environments.

Installation in CentOS 7

You can use Yum to install directly in CentOS. In addition to installing ImageMagick, you also need to Install its two dependencies ImageMagick-devel and ImageMagick-perl.

yum install -y ImageMagick ImageMagick-devel ImageMagick-perl
Copy after login

Then use pecl to install the extension. Find pecl in the PHP installation directory. For example, PHP is installed in the /usr/local/php74 directory, then pecl is usually in /usr/local /php74/bin In the target, execute the command:

/usr/local/php74/bin/pecl install imagick
Copy after login

to use pecl to automatically download and install ImageMagick, and finally in php.ini Add

extension=imagick.so
Copy after login
to

to enable the extension.

If you need to check whether the extension is installed successfully, you can execute the command

php -m|grep imagick
Copy after login

If imagick is output, it means the extension is installed successfully.


Digression: If you don’t know which php.ini configuration file PHP uses, you can execute the following command

php74 -i|grep ini
Copy after login

Find the line "Loaded Configuration File" and you will know which configuration file PHP uses. The php -i command
is similar to the function we use phpinfo() to view PHP related information.


Docker installation

To install extensions for PHP in the container, it is recommended to use docker-php-extension-installer on Github. This is a Shell script that can Help us solve the extension dependency problem, and automatically clear useless files after installing the extension. We only need to add this script to the Dockerfile. The following is the official example:

FROM php:7.2-cli
# 从Github上下载docker-php-extension-installer脚本
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
# 添加可执行权限并安装扩展
RUN chmod +x /usr/local/bin/install-php-extensions && \
    install-php-extensions gd xdebug imagick
Copy after login

The image built in this way will have the required extensions installed.


Digression: In the domestic network environment, timeout problems often occur when using docker-php-extension-installer to install extensions. It is recommended to use the external network. Build the image on your VPS, upload it to DockerHub or other private warehouses, and then pull it to the local network for use. You can use a cheap conscience cloud, or a VPS like Vultr that supports time-based billing.


PDF to image

Code example:

// 实例化imagick对象
$im = new imagick();
$im->setResolution(150, 150);
$im->setCompressionQuality(100);
$im->readImageBlob($fileContent);
$im->setImageFormat('jpg');
$im->setImageBackgroundColor('white');
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
$im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);

header("Content-type: image/jpeg");
echo $im->getImageBlob();
Copy after login

Code interpretation:

$im->setResolution(150, 150);
Copy after login

Used to set the resolution of the image. This function does not change the actual resolution of the image, it just sets it in the Imagick object before reading or creating the image. This function needs to be called before reading the image or creating the image.
This function receives two parameters, namely horizontal resolution and vertical resolution. The default value is 72*72. In order to maintain the aspect ratio of the image, the values ​​of these two parameters should be the same. The image converted by the default value is not clear enough. It is recommended to use double or triple the value, but the size of the image will also become larger.


$im->setCompressionQuality(100);
Copy after login

Set the compression quality of the image. The default value is 0; the parameter value passed in should be 1-100. For JPG format pictures, the smaller the value, the smaller the image volume and the sharpness. Lower; but for PNG images, this conclusion does not seem to hold. When the value is less than 90, the image size will be larger, so when converting to PNG image format, just keep the default value.


$im->readImageBlob($fileContent);
Copy after login

Directly load the binary content of the PDF file, or you can use the readImage($filename) function to read the saved PDF file.


$im->setImageFormat('jpg');
Copy after login

Set the format of the image to be generated, such as jpg,png, etc.,


$im->setImageBackgroundColor('white');
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
$im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
Copy after login

Set the image background color is white, remove the alpha channel of the image, and merge all images into one layer. If you do not perform these operations, the background of the converted image will be black, as shown below:

[PHP+ImageMagick] Convert PDF to image (detailed steps)


header("Content-type: image/png");
echo $im->getImageBlob();
Copy after login

获取转换生成图像的二进制数据,输出到客户端供下载;如果需要保存到文件,可以使用writeImage($filename)函数。

推荐:《PHP视频教程

The above is the detailed content of [PHP+ImageMagick] Convert PDF to image (detailed steps). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template