Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert PDF into Pictures by PHP+ImageMagick

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how PHP+ImageMagick converts PDF into pictures. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

ImageMagick installation

ImageMagick is a free software to create, edit and synthesize pictures. It can read, convert, and write pictures in multiple formats. Image cutting, color replacement, the application of various effects, picture rotation, combination, text, lines, polygons, ellipses, curves, attached to the picture stretch rotation. ImageMagick is free software: all source code is open, free to use, copy, modify, release, it complies with the GPL license agreement, 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, mainly for image processing, but imagick is more powerful. Here is a brief description of how to install imagick in two common environments.

Installed in CentOS 7

You can install it directly using Yum in CentOS, not only installing ImageMagick, but also its two dependencies, ImageMagick-devel and ImageMagick-perl.

Yum install-y ImageMagick ImageMagick-devel ImageMagick-perl

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

/ usr/local/php74/bin/pecl install imagick

You can use pecl to download and install ImageMagick automatically, and finally add it to php.ini

Extension=imagick.so

The extension can be enabled.

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

Php-m | grep imagick

If you output imagick, 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

Find the line "Loaded Configuration File" and you will know which configuration file PHP is using. Php-I command

The effect is similar to how we use the phpinfo () function to view information about PHP.

Docker installation

The PHP installation extension in the container is recommended to use docker-php-extension-installer on Github, which is a Shell script that can help us solve the dependency problem of the extension and automatically clean up useless files after installing the extension. All we need to do is add this script to Dockerfile. Here is the official example:

FROM php:7.2-cli# downloads the docker-php-extension-installer script ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions / usr/local/bin/# from Github to add executable permissions and install the extension RUN chmod + x / usr/local/bin/install-php-extensions & &\ install-php-extensions gd xdebug imagick

In this way, the constructed image will install the required extensions.

Digression: in the domestic network environment, the timeout problem often occurs when using docker-php-extension-installer to install extensions. It is recommended to use the VPS of the public network to build an image, upload it to DockerHub or other private repositories, and then pull it to the local network. You can use a cheap cloud of conscience, or VPS like Vultr, which supports billing on time.

PDF to picture code example: / / instantiate imagick object $im = new imagick (); $im- > setResolution (150,150); $im- > setCompressionQuality (100,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 (); Code interpretation: $im- > setResolution (150150)

Used to set the resolution of the image. This function does not change the actual resolution of the image, but sets it in the Imagick object before reading or creating the image, which needs to be called before reading or creating the image.

This function takes two parameters, the landscape resolution and the portrait resolution, and the default value is 72-72. In order to maintain the aspect ratio of the image, the two parameter values should be the same, the default value of the converted image is not clear enough, it is recommended to use two or three times the value, but at the same time the size of the image will also become larger.

$im- > setCompressionQuality (100)

Set the image compression quality, the default value is 0; the input parameter value should be 1-100, for JPG format images, the smaller the value, the smaller the image volume, while the definition is lower; but for PNG pictures, this conclusion does not seem to be true, when the value is less than 90, the picture volume is larger, so in the conversion to PNG picture format, keep the default value.

$im- > readImageBlob ($fileContent)

Load the binary contents of the PDF file directly, or you can use the readImage ($filename) function to read the saved PDF file.

$im- > setImageFormat ('jpg')

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)

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

Header ("Content-type: image/png"); echo $im- > getImageBlob ()

Get the binary data of the converted image and output it to the client for download; if you need to save to a file, you can use the writeImage ($filename) function.

What are the characteristics of php 1, the execution speed is fast. 2. It has good openness and expansibility. 3. PHP supports a variety of mainstream and non-mainstream databases. Object-oriented programming: PHP provides classes and objects. 5. The update speed of the version is fast. 6. It has rich functions. 7. Scalability. 8. Comprehensive functions, including graphics processing, encoding and decoding, compressed file processing, xml analysis and so on.

This is the end of the article on "how PHP+ImageMagick converts PDF into pictures". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report