【问题标题】:Failed to Convert PDF First Page Document to Image Thumbnail in PHP CodeIngiter在 PHP CodeIgniter 中无法将 PDF 第一页文档转换为图像缩略图
【发布时间】:2020-01-13 03:51:17
【问题描述】:

我正在使用 Ghostscript 9.50 和 ImageMagick 7.0.9 从上传的 PDF 文档生成首页缩略图。

这是我从 http://www.rainbodesign.com/pub/image-tools/pdf-to-jpg.html 获得的代码,然后我将其用作电子书控制器中的函数

function thumbnail(){
  $pdfPath = $_SERVER['DOCUMENT_ROOT'] . str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);         // Path to PDF files on the server
  $imagesPath = $_SERVER['DOCUMENT_ROOT'] . str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']) . 'ebooks/';      // Path to your images directory on the server
  $thumbsPath = $imagesPath . 'thumbnails/';                // Path to image thumbnails directory
  $thumbsize = 250;                     // Width of thumbnail images (set to 0 for full size)
  $pageNo = 0;                          // Page # from PDF to convert (ordinal!)

  // Constants and Defaults
  // ----------------------
  $theWMType = 'jpg:';
  $extension = '.jpg';
  $pdf = '';
  $error = '';

  // Fetch user settings and default overrides via query string
  if (isset($_GET['src'])) { $pdf = $_GET['src']; }
  if (isset($_GET['path'])) { $pdfPath = $_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['path']; }
  if (isset($_GET['width'])) { $thumbsize = $_GET['width']; }
  if (isset($_GET['page'])) { $pageNo = $_GET['page'] - 1; }    // Page # is ordinal in ImageMagick!

  if ($pdf == '') { $error .= "Source Image Not Specified.<br>\n"; }

  $original = $pdfPath . $pdf;                  // Add file path to PDF file name
  $originalBase = basename($pdf,'.pdf');                // Extract PDF file name basename w/o extension
  $thumbnail = $thumbsPath . $originalBase . $extension;        // Use basename for the thumbnail .jpg file name

  if (!file_exists($original)) {                    // Make sure PDF file exists
    $error .= "Source PDF Document Not Found<br>\n" . $original . "<br>\n";
  }

  // Fix file name(s) to select 1st page of PDF and enquote $original if it/they contain <space>s.
  if (strpos($original, ' ') !== false) {
    $original = "\"" . $original . "[$pageNo]\"";       // Enclose file name in quotes and add Page #
    $thumbnail = str_replace(' ', '_', $thumbnail);     // Replace <space>s with underscores
  } else {
    $original = $original . "[$pageNo]";            // Just add Page # to PDF $original
  }

  // Check to see if the thumbnail already exists in the "cache"
  if (!file_exists($thumbnail)) {

  // No! Convert PDF to JPG with ImageMagick/GhostScript now!
    if ($error == '') {
      $wmCmd = "convert $original";
      if ($thumbsize != 0) { $wmCmd .= " -resize " . $thumbsize . "x"; }
      $wmCmd .= " $theWMType$thumbnail";
      $result = exec($wmCmd);
    } // endif $error == ''

  // A little error-checking overkill
    if (!file_exists($thumbnail)) {
    $error .= "Convert Failed!  Can't find $thumbnail<br>\n";
    $error .= $result . "<br>\n" . $original . "<br>\n" . $thumbnail . "<br>\n";
    } // endif !file_exists

  } // endif !file_exists($thumbnail)

  if ($error != '') {               // Did we see an error?
  header("Content-type: text/html\n");      // Yes! Send MIME-type header for HTML files
  echo($error);
    } else {
  header("Content-type: image/jpeg\n"); // No.  OK, send MIME-type header for JPEG files
  echo(file_get_contents($thumbnail));      // Fetch image file contents and send it!
  } // endif $error

  exit;
}

但是当我想在那里显示缩略图时:

<img src="<?= base_url('ebook/thumbnail?src=ebooks/' . $e->file) ?>" width="100" alt="pdf document">

图片无法显示,当从浏览器中的 img 标签“src”加载 URL 进行检查时,出现此错误:

Convert Failed! Can't find D:/htdocs/halokes_library/ebooks/thumbnails/XML_Bible.jpg

D:/htdocs/halokes_library/ebooks/XML_Bible.pdf[0]
D:/htdocs/halokes_library/ebooks/thumbnails/XML_Bible.jpg

有什么想法吗?

【问题讨论】:

  • 你确定路径正确吗?
  • 您引用了您自己的代码中的错误。从 ImageMagick 捕获错误会更有用,或者更好的是从 Ghostscript 捕获错误/警告消息。
  • @SimoneRossaini 是的,路径是正确的
  • @chrisl 我从浏览器中捕获了该错误。我无法从 ImageMagick 或 Ghostscript 捕获错误,因为转换是通过此 PHP 代码执行的(不直接使用 Ghostscript CLI 或 ImageMagick 应用程序)
  • 好吧,我能建议的最好的方法是仔细检查 PHP 运行命令的环境: PHP 通常在特殊用户下运行,并且通常 PATH 环境变量未设置为“正常”用户。值得仔细检查。

标签: php codeigniter imagemagick ghostscript


【解决方案1】:

使用来自 rainbodesign.com 的完全相同的代码时,我遇到了完全相同的错误。我将文件/目录设置为代码将其缩略图写入 777 权限,但代码仍然没有写入 thumbs 目录。所以我检查了实际从rainbodesign传递给pdf2jpg.php程序的内容,发现pdf的路径不是“来自”根目录,而是包含它。我对传递给 pdf2jpg.php 的变量进行了调整,以及权限调整,现在它可以工作了!

示例 src 调用:

/pdf2jpg/pdf2jpg.php?src=/7751/Scan_200512.pdf&mdir=7751

【讨论】:

    猜你喜欢
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 2010-09-09
    • 1970-01-01
    • 2011-04-01
    • 2011-06-23
    相关资源
    最近更新 更多