Error trying to create a jpeg thumb image of a PDF

2019-07-25 07:45发布

I'm getting this error trying to convert a pdf to a jpeg thumbnail:

Fatal error: Uncaught exception 'ImagickException' with message 
'PDFDelegateFailed `[ghostscript library] 
-q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT 
-dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 
"-sDEVICE=pam" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72" 
-dUseCIEColor -dFirstPage=1 -dLastPage=1 
"-sOutputFile=/tmp/magick-1514TynhowIwvsS8%d" 
"-f/tmp/magick-1514ToUFRp0QWKlG" 
"-f/tmp/magick-1514zEMWRNGv55Od"':
 -dname= must be followed by a valid token 
 @ error/pdf.c/InvokePDFDelegate/263'
  in /home/domain/public_html/pdfPreview.php:46 

  Stack trace: #0 /home/domain/public_html/pdfPreview.php(46): 
  Imagick->__construct('./assets/whatsn...') 
  #1 {main} thrown in 
  /home/domain/public_html/pdfPreview.php on line 46

Have no issues with this anywhere else, this is on a godaddy server the PATH is /sbin:/usr/sbin:/bin:/usr/bin and ghostscript is here: /usr/bin/ghostscript

but I'm not understanding [or finding any info on]:

-dname= must be followed by a valid token 

here is the actual script for reference:

<?php

ini_set('display_errors', 1);

ini_set('display_startup_errors', 1);

error_reporting(E_ALL);

echo getenv('PATH') ;

echo "<pre>"; 
system("which ghostscript");   
system("ghostscript --version"); 
echo "</pre>"; 

$pdf = './assets/whatsnew/36215A_IG_040915.pdf';

$id = '1';

$filename = basename($pdf, '.pdf');

$path_parts = pathinfo($pdf);

$dir = $path_parts['dirname'];

$filename = $path_parts['filename'];

$filename = strtolower($filename);

$filename = preg_replace("/[^a-z0-9_\s-]/", "", $filename);

$filename = preg_replace("/[\s-]+/", " ", $filename);

$filename = preg_replace("/[\s_]/", "-", $filename);

$thumb = $dir . '/' . $filename . '-' . $id . '.jpg';

echo $thumb;

if(!file_exists($thumb)){

    if(file_exists($pdf)){

        $img = new Imagick($pdf.'[0]');

        $img->scaleImage(85,110);

        $img->setImageFormat('jpg');

        $img->writeImage($thumb);


    }else{

        $thumb = $dir . '/pdf-default.gif';

    }

}

return '<img src="' . $thumb . '" class="tip-image" alt="' . $path_parts['filename'] . '" />';

1条回答
Deceive 欺骗
2楼-- · 2019-07-25 08:40

Try to change the line...

$img = new Imagick($pdf.'[0]');

to...

$img = new Imagick($pdf.'[1]');

For some reason, Ghostscript is not accepting the zero argumento as first page, got to be "1".

查看更多
登录 后发表回答