PHP turns PDFs into pictures and mosaics images

Source: Internet
Author: User
Tags create directory imagemagick zts

Description

1.pdf to picture is implemented by installing PHP extension Imagick.

2. Due to a series of problems with Windows extension installation, it is recommended to develop in a Linux environment where windows can try to install.

3. Install the ImageMagick imagick for PHP step for CentOS. ImageMagick is a set of software series, mainly used for image creation, editing and conversion, etc. (there are many installation methods, this article only describes one installation method)

(a) Installation steps:

1. Download and install ImageMagick

wget FTP://MIRROR.AARNET.EDU.AU/PUB/IMAGEMAGICK/IMAGEMAGICK-6.6.8-10.TAR.GZTAR-XZVF imagemagick-6.6.8-10.tar.gz. /configure--prefix=/usr/local/imagemagickmakemake Install

2. Download and install Imagick
Address: Http://pecl.php.net/package/imagick

wget HTTP://PECL.PHP.NET/GET/IMAGICK-3.1.0RC1.TGZTAR-XZVF imagick-3.1.0rc1phpize./configure--with-php-config=/ Usr/local/php/bin/php-config--with-imagick=/usr/local/imagemagickmakemake Install

3. Manually configure the PHP.ini

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/installing Header Files:          /usr/local/php/include/php/generating imagick.so to/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626 /

At this point, the PHP extension installation is complete.

(ii) method of use

/** * pdf2png * @param $pdf pending PDF file * @param $path the path of the picture to be saved * @param $page the page to be exported-1 for all 0 for the first page 1 for the second page * @return Saved picture path and file name * Note: Here is a pit for the $pdf path and $path path in Imagick, PHP version 5 + can use relative paths. The php7+ version must use an absolute path. Therefore, it is recommended that you use absolute paths.    */function pdf2png ($pdf, $path, $page =-1) {if (!extension_loaded (' Imagick ')) {return false;    } if (!file_exists ($pdf)) {return false;    }if (!is_readable ($pdf)) {return false;    } $im = new Imagick ();    $im->setresolution (150,150);    $im->setcompressionquality (100);    if ($page ==-1) $im->readimage ($pdf); else $im->readimage ($pdf. "    [". $page."] ");        foreach ($im as $Key = + $Var) {$Var->setimageformat (' png '); $filename = $path. MD5 ($Key. Time ()).        PNG ';        if ($Var->writeimage ($filename) = = True) {$Return [] = $filename;    }}//Returns an array of conversion images, which returns a two-dimensional array, since the PDF may be multiple pages. return $Return;} /** * spliceimg * @param array $imgs PDF conversion png path * @param string $path The path of the picture to be saved * @return string to stitch multiple pictures into the path of the graph * Note: Multi-page PDF conversion to post-image stitching method */function spliceimg ($imgs = Array (), $img _path = "    ) {//Custom width $width = 1230;    Get the total height $pic _tall = 0;        foreach ($imgs as $key = + $value) {$info = getimagesize ($value);    $pic _tall + = $width/$info [0]* $info [1];    }//Create long graph $temp = Imagecreatetruecolor ($width, $pic _tall);    Assign a white background $color = Imagecolorallocate ($temp, 255,255,255);    Imagefill ($temp, 0,0, $color);    $target _img = $temp;    $source = Array ();        foreach ($imgs as $k = + $v) {$source [$k] [' source '] = Imagecreatefrompng ($v);    $source [$k] [' size '] = getimagesize ($v);    } $num = 1;    $tmp = 1; $tmpy = 2;    The spacing between pictures $count = count ($imgs); for ($i = 0; $i < $count; $i + +) {imagecopy ($target _img, $source [$i] [' source '], $tmp, $tmpy, 0, 0, $source [$i] ['        Size '][0], $source [$i] [' size '][1]);        $tmpy = $tmpy + $source [$i] [' size '][1]; Frees resource memory Imagedestroy ($source [$i] [' SourCe ']);    } $returnfile = $img _path.date (' y-m-d ');                if (!file_exists ($returnfile)) {if (!make_dir ($returnfile)) {/* Create directory Failed */return false; }} $return _imgpath = $returnfile. ' /'. MD5 (Time (). $pic _tall. ' Pdftopng ').    PNG ';    Imagepng ($target _img, $return _imgpath); return $return _imgpath;}    /** * Make_dir * @param string $folder Generate directory Address * Note: Generate directory Method */function Make_dir ($folder) {$reval = false;        if (!file_exists ($folder)) {/* If the directory does not exist, try to create the directory */@umask (0);        /* Split the directory path into arrays */Preg_match_all ('/([^\/]*) \/?/i ', $folder, $atmp); /* If the first word identifier/is treated as a physical path */$base = ($atmp [0][0] = = '/')?        ‘/‘ : ‘‘; /* Iterate through the array containing the path information */foreach ($atmp [1] as $val) {if ('! = $val) {$base                . = $val; if ('.. ' = = $val | | '. ' = = $val) {/* If the directory is. Or.                    Then directly fill/continue the next loop */$base. = '/';    Continue            }} else {continue;            } $base. = '/';  if (!file_exists ($base)) {/* attempts to create a directory and continues looping if creation fails */if (@mkdir (RTrim ($base, '/'),                    0777) {@chmod ($base, 0777);                $reval = true; }}}} else {/* path already exists.    Returns whether the path is a directory */$reval = Is_dir ($folder);    } clearstatcache ();     return $reval;}

  

Call the method, implement the function of converting PDF to picture and then stitching picture ~over~

PHP turns PDFs into pictures and mosaics images

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.