7

WordPress实现截取pdf首页作为缩略图片方法

 2 years ago
source link: https://www.huhexian.com/28000.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

WordPress实现截取pdf首页作为缩略图片方法

2022-02-2115:09:54评论932字

我们在开发使用 WordPress 的时候,偶尔会有客户要求截取文章 PDF 附件首页作为封面缩微图,下面我们找到一个办法,有兴趣的可以尝试下,WordPress PHP 如何利用 imagick+ghostscript 截取 pdf 首页作为缩略图片:

宝塔里 PHP 里可以安装扩展 imagick。

安装 ghostscript,安装很简单,将下载的 source code(tar.gz 格式)放到服务器任意一个位置

# tar -xzvf ghostscript-9.55.0.tar.gz

# cd ghostscript-9.55.0

# ./configure

# make

# make install

装完之后输入查看是否安装成功,如果输入下面这个命令会显示版本号,说明安装成功。

# gs --version

php 代码

  1. function pdf2png($pdf,$path,$page=0)
  2. if(!is_dir($path))
  3. mkdir($path,0777,true);
  4. if(!extension_loaded('imagick'))
  5. echo '没有找到imagick!' ;
  6. return false;
  7. if(!file_exists($pdf))
  8. echo '没有找到pdf' ;
  9. return false;
  10. $im = new Imagick();
  11. $im->setResolution(120,120); //设置图像分辨率
  12. $im->setCompressionQuality(80); //压缩比
  13. $im->readImage($pdf."[".$page."]"); //设置读取pdf的第一页
  14. //$im->thumbnailImage(200, 100, true); // 改变图像的大小
  15. $im->scaleImage(200,100,true); //缩放大小图像
  16. $filename = $path."/". time().'.png';
  17. if($im->writeImage($filename) == true)
  18. $Return = $filename;
  19. return $Return;

调用方法:

  1. $url = pdf2png($pdf, '保存路径');

以上就是给大家带来的WordPress如何实现截取pdf首页作为缩略图片的方法教程。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK