PHP converts PDF into PNG-PHP source code using imagick

Source: Internet
Author: User
Tags imagemagick
When using mysql, you sometimes need to query records with unique fields. Although mysql provides the keyword distinct to filter out redundant duplicate records, only one record is retained, but it is often used to return the number of records that do not repeat, instead of returning all values that do not record the record. The reason is that distinct can only return its Target Field, but cannot return other fields. This problem has plagued me for a long time. if distinct cannot solve this problem, I only need to use double loop query to solve it, for a station with a large data volume

Requirement: use PHP to generate PNG images from PDF files, which are generally used for online reading and browsing.


In Windows:
1. php dll extension: https://www.php1.cn/
2. there are DLL versions used for different PHP versions. Be sure to use the same version as your version.
3. rename the downloaded DLL to php_imagick.dll (I am the downloaded php_imagick_dyn-Q16.dll) and put it to EXT in the PHP installation directory.
4. modify php. ini and add "extension = php_imagick.dll" without double quotation marks!
5. after restarting apache, write a phpinfo file and search for the "imagick" keyword. If yes, the installation is complete.
6.install ghostscript.exe at the end, or an error will be reported. : Https://www.php1.cn/
7. execute the PHP script and modify it as needed.


In Linux:
1 download ImageMagick-6.7.1-6

wget ftp://ftp.u-aizu.ac.jp/pub/graphics/image/ImageMagick/imagemagick.org/ImageMagick.tar.gz

2. decompress

# tar -xzvf ImageMagick.tar.gz

3. Compile and install

# cd  ImageMagick-6.7.1-6# ./configure -prefix=/usr/local/imagemagick -enable-lzw -with-modules && make && make install

4. check

# /usr/local/imagemagick/bin/convert -version

5. test it.

# pwd/root/test/ImageMagick-6.7.1-6# cd images/# /usr/local/imagemagick/bin/convert -sample 25%x25% wizard.jpg test.jpg

6. start to install the PHP extension of imagick (note: when installing the imagick extension, you need PHP >=5.1.3, ImageMagick >=6.2.4)

7. get imagick (latest get: https://www.php1.cn /)

# wget https://www.php1.cn/

Https://www.php1.cn/

8. decompress

# tar -zxvf imagick-3.1.0RC1.tgzapt-get install php5-dev

9. phpize

# cd imagick-3.1.0RC1# phpize

10. configure compilation

# ./configure --with-php-config=/opt/modules/php5.3.5/bin/php-config --with-imagick=/usr/local/imagemagick
# make&&make install

An error may occur. due to version upgrade and a slight change in the folder structure, let's create a soft connection:

Ln-s/usr/local/imagemagick/include/ImageMagick-6/usr/local/imagemagick/include/ImageMagick

If the following Error is reported: make: *** [imagick_file.lo] Error 1

Locate PKG_CONFIG export PKG_CONFIG_PATH =/usr/local/imagemagick/lib/pkgconfig

Configure again

Note: After compilation is successful, imagick is generated in the php extension directory. so, and then php. add "extension ="/usr/lib/php5/20090626 + lfs/imagick in ini. so ""

Pay attention to version issues: otherwise, a similar Error such as make: *** [imagick_class.lo] Error 1 will be reported.

11. restart apache

/etc/init.d/apache2 restart

12. you can also use PECL for installation.

1) sudo apt-get install libmagick-dev
2) sudo apt-get install php-pear php5-dev
3) sudo pecl install imagick

PHP code call:

function pdf2png($PDF,$Path){   if(!extension_loaded('imagick')){       returnfalse;   }   if(!file_exists($PDF)){       returnfalse;   }   $IM =new imagick();   $IM->setResolution(120,120);   $IM->setCompressionQuality(100);   $IM->readImage($PDF);   foreach($IM as $Key => $Var){       $Var->setImageFormat('png');       $Filename = $Path.'/'.md5($Key.time()).'.png';       if($Var->writeImage($Filename)==true){           $Return[]= $Filename;       }   }   return $Return;}

Reprinted please note: PHP using ImageMagick convert PDF into PNG (https://www.php1.cn /)

The above is the content of converting PDF to PNG using imagick in PHP. For more information, see PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.