PHP中使用imagick实现把PDF转成图片
2023-06-13 09:15:42 时间
PHPManual里,对imagick的描述,真的是简洁,每个成员函数,点击打开就看到如下文本:
Warning
Thisfunctioniscurrentlynotdocumented;onlyitsargumentlistisavailable.
刚才解决了PHP加载问题后,对图片的处理相当方便,网上随便找了一段:
<?php
Header("Content-type:image/jpeg");
/**
*缩小图片尺寸.
*
*@param$image待处理的二进制图片
*@param$width处理后图片尺寸的宽度(px)
*@param$height处理后图片尺寸的高度(px)
*@param$crop是否裁剪图片
*
*@return处理好的二进制图片
*/
functionresize($image,$width,$height,$crop)
{
$imagick=newImagick($image);
$w=$imagick->getImageWidth();
$h=$imagick->getImageHeight();
if($w>$width||$h>$height)
{
if($crop)
{
$imagick->cropThumbnailImage($width,$height);
}
else
{
$imagick->resizeImage($width,$height,Imagick::FILTER_LANCZOS,1,true);
}
}
$processed_image=$imagick->getImageBlob();
return$processed_image;
}
$s=resize("123.jpg",60,40,1);
echo$s;
?>
但我要用这个扩展做的,是将PDF转成PNG。PDF哦!
<?php
/**
*PDF2PNG
*@param$pdf 待处理的PDF文件
*@param$path待保存的图片路径
*@param$page待导出的页面-1为全部0为第一页1为第二页
*@return 保存好的图片路径和文件名
*/
functionpdf2png($pdf,$path,$page=-1)
{
if(!extension_loaded("imagick"))
{
returnfalse;
}
if(!file_exists($pdf))
{
returnfalse;
}
$im=newImagick();
$im->setResolution(120,120);
$im->setCompressionQuality(100);
if($page==-1)
$im->readImage($pdf);
else
$im->readImage($pdf."[".$page."]");
foreach($imas$Key=>$Var)
{
$Var->setImageFormat("png");
$filename=$path."/".md5($Key.time()).".png";
if($Var->writeImage($filename)==true)
{
$Return[]=$filename;
}
}
return$Return;
}
$path="images";//请确保当前目录下有这个文件夹,由于一直要用,所以就不加检测了
$s=pdf2png("test.pdf",$path);
$scount=count($s);
for($i=0;$i<$scount;$i++)
{
echo"<divalign=center><fontcolor=red>Page ".($i+1)."</font><br><ahref=\"".$s[$i]."\"target=_blank><imgborder=3height=120width=90src=\"".$s[$i]."\"></a></div><p>";
}
?>
跟上面的差不多吧?可是总是不成功,readImage那边异常了,查看错误输出:
PHPFatalerror: Uncaughtexception"ImagickException"withmessage"PostscriptDelegateFailed`test.pdf":Nosuchfileordirectory"
百度谷歌了好久,最后灵机一动,不是说Postscript么?我找,我装。
这个Postscript,其实是GhostScript。
装好后,一跑,OK啦!
相关文章
- PDF to word for Mac(pdf转word转换器)v4.3.4激活版
- pdf增强插件:Enfocus PitStop Pro 2022 for Mac(PDF文件编辑和校验工具) 介绍及特点
- Linux系统下安装PHP的指南(linux怎么安装php)
- PHP连接MySQL的实现方法(php怎么连接mysql)
- Redis入门教程:PDF下载解析(redis教程pdf下载)
- MySQL验证:PHP实现细节(php验证mysql)
- PHP不支持MySQL:危机深重的问题(php不支持mysql)
- 使用PHP连接Linux,轻松实现物联网设备管理。(php连接linux)
- 使用PHP连接MySQL数据库实现数据操作(php连接mysql类)
- PHP连接MSSQL数据库的方法和步骤(php如何连接mssql)
- 如何在Linux上重新安装PHP(linux重新安装php)
- PHP高效操作MSSQL数据库实战(php连接操作mssql)
- PHP访问MSSQL数据库的实现方法(php 读取 mssql)
- PHP实现MSSQL数据库分页功能(php分页类 mssql)
- PHP连接MSSQL实现数据交互(mssql类php)
- PHP与MySQL实现无缝连接(php mysql 连接)
- Redis高级教程PDF打造高效存储系统(redis高级教程pdf)
- 利用PHP实现Redis队列任务的并发处理(redis队列任务php)
- PHP实现高效的Redis连接(redis连接 php)
- Redis运维指南让你轻松精通PDF(redis运维 pdf)
- 使用PHP实现Redis自旋锁(redis自旋锁php)
- PHP学习笔记IIS7下安装配置php环境
- php使浏览器直接下载pdf文件的方法