无法在PHP CodeIngiter中将PDF首页文档转换为图像缩略图

无法在PHP CodeIngiter中将PDF首页文档转换为图像缩略图,php,codeigniter,imagemagick,ghostscript,Php,Codeigniter,Imagemagick,Ghostscript,我正在使用Ghostscript 9.50和ImageMagick 7.0.9从上传的PDF文档生成第一页缩略图 这是我的代码,我把它作为电子书控制器的一个函数 function thumbnail(){ $pdfPath = $_SERVER['DOCUMENT_ROOT'] . str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']); // Path to PDF files on

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

这是我的代码,我把它作为电子书控制器的一个函数

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;
}

有什么想法吗?

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

示例src调用:


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

您确定路径正确吗?您引用了自己代码中的错误。从ImageMagick中捕获错误,或者更好地从Ghostscript中捕获错误/警告消息,将非常有用。@SimoneRossaini是的,路径是correct@chrisl我从浏览器中捕获该错误。我无法从ImageMagick或Ghostscript中捕获错误,因为转换是通过此PHP代码执行的(不直接使用Ghostscript CLI或ImageMagick应用程序)。好吧,我建议最好是仔细检查PHP运行命令的环境:PHP通常在特殊用户下运行,通常,PATH环境变量的设置与“普通”用户不同。值得仔细检查一下。
<img src="<?= base_url('ebook/thumbnail?src=ebooks/' . $e->file) ?>" width="100" alt="pdf document">
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