0

I'm using Ghostscript 9.50 and ImageMagick 7.0.9 to generate first page thumbnail from uploaded PDF documents.

Here's my code I've got from http://www.rainbodesign.com/pub/image-tools/pdf-to-jpg.html then I use it as a function in my Ebook controller

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

But when I want to display the thumbnail there:

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

The image can't displayed and when load the URL from the img tag 'src' in my browser to check, I get this error :

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

Any thoughts?

  • Are you sure path is correct? – Simone Rossaini Jan 13 '20 at 07:12
  • You're quoting an error from your own code. It would be far more useful to capture the error from ImageMagick or, even better, error/warning message(s) from Ghostscript. – chrisl Jan 13 '20 at 13:49
  • @SimoneRossaini Yes, the path is correct – Yohanes Dwi Listio Jan 14 '20 at 13:25
  • @chrisl I capture that error from my browser. I can't capture the error from ImageMagick or Ghostscript because the conversion is executed via this PHP code (not directly using Ghostscript CLI or ImageMagick application) – Yohanes Dwi Listio Jan 14 '20 at 13:29
  • Well, the best I can suggest is to double check the environment under which PHP runs the commands: PHP usually runs under special user, and often the PATH environment variable not set as it is for "normal" users. Worth double checking that. – chrisl Jan 14 '20 at 15:45

1 Answers1

0

I had the exact same error using the exact same code from rainbodesign.com. I set the files/directory the code writes its thumbnail to 777 permissions and still the code is not writing to the thumbs directory. So I checked what is actually passing to the pdf2jpg.php program from rainbodesign and found the path to the pdf was not "from" the root directory, but included it. I made adjustments to what variables are being passed to pdf2jpg.php, along w/the permission adjustment and it now works!

example src call:

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

RC Allen
  • 1
  • 1