Created
November 11, 2011 06:22
-
-
Save fasthold/1357342 to your computer and use it in GitHub Desktop.
利用Imagick将pdf转为图片
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PDF2PNG | |
* @param $pdf 待处理的PDF文件 | |
* @param $path 待保存的图片路径 | |
* @param $page 待导出的页面 -1为全部 0为第一页 1为第二页 | |
* @return 保存好的图片路径和文件名 | |
*/ | |
function pdf2png($pdf,$path,$page=-1) | |
{ | |
if(!extension_loaded('imagick')) | |
{ | |
die('Install Imagick first.'); | |
return false; | |
} | |
if(!file_exists($pdf)) | |
{ | |
return false; | |
} | |
$im = new Imagick(); | |
$im->setResolution(120,120); | |
$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 $Return; | |
} | |
$path="thumb";//请确保当前目录下有这个文件夹,由于一直要用,所以就不加检测了 | |
$s=pdf2png(__DIR__."/1.pdf",$path,0); | |
$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><img border=3 height=120 width=90 src=\"".$s[$i]."\"></a></div><p>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment