PHP (thinkphp) under Linux system PDF to PNG image "go"

Source: Internet
Author: User

PHP) using Imagick to convert PDFs into pictures

Before operating, install Ghostscript on the Linux server first

650 2017-06-17 13:31:20 root CD./soft
651 2017-06-17 13:31:25 Root wget http://ghostscript.com/releases/ghostscript-8.71.tar.gz
652 2017-06-17 13:41:14 Root tar zxvf ghostscript-8.71.tar.gz
653 2017-06-17 13:41:26 Root CD ghostscript-8.71
654 2017-06-17 13:41:37 root./configure--prefix=/usr
655 2017-06-17 13:42:06 root mkdir obj
656 2017-06-17 13:42:15 Root mkdir Bin
657 2017-06-17 13:42:23 Root make all
658 2017-06-17 13:45:29 Root make Install

Then reboot:

Use Imagick;
/**
* 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.
*/
function Pdf2png ($pdf, $path, $page =-1)
{
if (!extension_loaded (' Imagick '))
{
return false;
}
if (!file_exists ($pdf))
{
echo dirname ($pdf);
echo "File not exit";
return false;
}

$im = new Imagick ();


$im->setresolution (120,120);
$im->setcompressionquality (100);
try {
if ($page ==-1) {
$im->readimage ($pdf);
}else{
$im->readimage ($pdf. " [". $page."] ");
}
} catch (\exception $e) {
Log::notice ($e->getmessage ());
}
foreach ($im as $Key = $Var)
{
$Var->setimageformat (' png ');
$filename = $path. " /". MD5 ($Key. Time ()). PNG ';
if ($Var->writeimage ($filename) = = True)
{
$Return [] = $filename;
}
}
return $Return;
}

function Cpdf2png ()
{
$path = "Public/files";//Make sure that the folder is in the current directory, because it is always used, so it is not detected
$s =self::p df2png ("Public/files/bp/36.pdf", $path);
$scount =count ($s);
for ($i =0; $i < $scount; $i + +)
{
echo "<div Align=center><font color=red>page". ($i + 1). " </font><br><a href=\ "". $s [$i]. " \ "Target=_blank></a></div><p>";
}
}

A complete reference: http://www.jb51.net/article/60337.htm this article. Thank you so much

PHP (thinkphp) under Linux system PDF to PNG image "go"

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.