php turns pdf into picture and splices picture

Keywords: PHP Windows Linux CentOS

explain:

1.pdf to image by installing php extension imagick realization.

2. Due to a series of problems in Windows Extension installation, it is recommended to develop in linux environment, and windows members can try to install.

3. Install ImageMagick imagick for php for Centos. ImageMagick is a set of software series, mainly used for the creation, editing and transformation of pictures (there are many installation methods, this article only introduces one installation method)

(1) Installation steps:

1. Download and install ImageMagick

wget ftp://mirror.aarnet.edu.au/pub/imagemagick/ImageMagick-6.6.8-10.tar.gz

tar -xzvf ImageMagick-6.6.8-10.tar.gz
./configure --prefix=/usr/local/imagemagick
make
make install

2. Download and install imageck
Address: http://pecl.php.net/package/imagick

wget http://pecl.php.net/get/imagick-3.1.0RC1.tgz

tar -xzvf imagick-3.1.0RC1
phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-imagick=/usr/local/imagemagick
make
make install

3. Manual configuration php.ini

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ 
Installing header files:          /usr/local/php/include/php/

//generate imagick.so To / usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

At this point, the php extension installation is complete.

 

(2) How to use

/**
 * PDF2PNG
 * @param $pdf  Pending PDF files   
 * @param $path Image path to be saved
 * @param $page Page to be exported - 1 is all 0 is the first page 1 is the second page
 * @return      Saved image path and file name
 * Note: here is the pit. For the $pdf path and $path path path in the imageck, the relative path can be used for PHP version 5 +. The php7 + version must use an absolute path. Therefore, we suggest that we use absolute path.
 */
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;
        }
    }
    //Return the array of converted pictures. Since the pdf may have multiple pages, the two-dimensional array is returned here.
    return $Return;
}


/**
 * Spliceimg
 * @param array  $imgs pdf Convert png path  
 * @param string $path Image path to be saved
 * @return string  The path of splicing multiple pictures into a picture
 * Note: multi page pdf converted to image post splicing method
 */
function Spliceimg($imgs = array(),$img_path = '')
{
    //Custom width
    $width = 1230;
    //Get total height
    $pic_tall = 0;
    foreach ($imgs as $key => $value) {
        $info = getimagesize($value);
        $pic_tall += $width/$info[0]*$info[1];
    }
    // Create a long chart
    $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; //Space 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];
        //Free resource memory
        imagedestroy($source[$i]['source']);
    }
    $returnfile = $img_path.date('Y-m-d');
    if (!file_exists($returnfile))
	{
		if (!make_dir($returnfile))
		{
		    /* Failed to create directory */
                    return false;
                }
    }
    $return_imgpath = $returnfile.'/'.md5(time().$pic_tall.'pdftopng').'.png';
    imagepng($target_img,$return_imgpath);
    return $return_imgpath;
}


/**
 * make_dir
 * @param string $folder Build directory address
 * Note: Catalog generation method
 */
function make_dir($folder)
{
    $reval = false;
    if (!file_exists($folder))
    {
        /* If the directory does not exist, try to create it */
        @umask(0);
        /* Split directory paths into arrays */
        preg_match_all('/([^\/]*)\/?/i', $folder, $atmp);
        /* Treat as a physical path if the first character is / */
        $base = ($atmp[0][0] == '/') ? '/' : '';
        /* Traversing an array containing path information */
        foreach ($atmp[1] AS $val)
        {
            if ('' != $val)
            {
                $base .= $val;
                if ('..' == $val || '.' == $val)
                {
                    /* If the directory is. Or */
                    $base .= '/';
                    continue;
                }
            }
            else
            {
                continue;
            }
            $base .= '/';
            if (!file_exists($base))
            {
                /* Attempt to create directory, continue loop if creation fails */
                if (@mkdir(rtrim($base, '/'), 0777))
                {
                    @chmod($base, 0777);
                    $reval = true;
                }
            }
        }
    }
    else
    {
        /* The path already exists. Return whether the path is a directory */
        $reval = is_dir($folder);
    }
    clearstatcache();
    return $reval;
}    

  

Call the method to realize the function of transforming pdf into pictures and then splicing pictures~

Posted by stennchau on Thu, 21 May 2020 09:13:32 -0700