【问题标题】:Displaying thumbnail pdf using php使用php显示缩略图pdf
【发布时间】:2017-08-29 15:53:51
【问题描述】:

我是 imageMagick 的新手。在我们将 pdf 上传到目录后,我正在尝试显示 pdf 缩略图。 exec() 函数似乎在我的代码中不起作用。 pdf 正在完美地上传到目录中,但没有显示缩略图,而是显示了指向 pdf 的链接。我希望缩略图显示在屏幕上。请帮帮我!!!!

<html>
   <head>
      <meta charset="utf-8" />
      <title>PDF Preview Image</title>
   </head>
   <body>
      <form method="post" enctype="multipart/form-data">
      <table>
      <tr><td>Select only pdf's<input type="file" name="pdfupload" id="pdfupload" placeholder="Select only pdf" multiple="multiple"></td>
      <td><input type="submit" name="submit" value="Upload" /></td></tr></td>
      </table>
      </form>
   </body>
</html>



<?php
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
  //Define the directory to store the uploaded PDF
    $pdfDirectory = "pdf/";

    //Define the directory to store the PDF Preview Image
    $thumbDirectory = "pdfimage/";

    //Get the name of the file (Basename)
    $filename = basename( $_FILES['pdfupload']['name'], ".pdf");

    // Clean the filename
    //Remove all characters from the file name other than letters, numbers, hyphens and underscores
    $filename = preg_replace("/[^A-Za-z0-9_-]/", "", $filename).(".pdf");

    //Name the thumbnail image (Same as the pdf file -  You can set custom name)
    $thumb = basename($filename, ".pdf");

     //Upload the PDF
        if(move_uploaded_file($_FILES['pdfupload']['tmp_name'], $pdfDirectory.$filename)) {

        //Set path to the PDF file
        $pdfWithPath = $filename;

        //Add the desired extension to the thumbnail
        $thumb = $thumb.".jpg";

        //execute imageMagick's 'convert', setting the color space to RGB
        //This will create a jpg having the widthg of 200PX
       exec("convert \"{$pdfWithPath}[0]\" -colorspace RGB -geometry 200 $thumbDirectory$thumb");

        // Finally display the image
        echo '<p><a href="'.$pdfWithPath.'"><img src="pdfimage/'.$thumb.'" alt="" /></a></p>';
        }
    }
    ?>

【问题讨论】:

  • 尝试shell_exec()一次。

标签: php html pdf imagemagick imagick


【解决方案1】:

捕获 exec() 的结果,然后粘贴结果。

我认为这是因为您的 shell 命令字符串没有正确构建。在 PHP 中构建 shell 命令时,将变量(例如文件名)注入字符串中,您确实应该使用 escapeshellarg 函数来避免命令行中字符未转义的问题。

'convert '.escapeshellarg($pdfWithPath).' -colorspace RGB -geometry 200 '. $thumbDirectory .escapeshellarg($thumb)

【讨论】:

    【解决方案2】:

    经过长时间的研究,我找到了一个使用 xpdf 的简单解决方案。

    重要提示 - 此示例适用于 Windows 操作系统,但 xpdf 也适用于 Mac OS 和 Linux。

    每个人都建议使用 ImageMagick 和 GhostScript,我觉得这对于这么小的任务来说有点过头了。

    先去https://www.xpdfreader.com/下载xpdf文件

    您将获得几个 .exe 文件,将所有文件放在您的 Apaches 服务有权访问和许可的文件夹中(例如“C:\xpdf”)。

    用法:

    $filepath = YOUR_PDF_FILE_PATH;
    $thumbnail_file = 'C:\\temp\\thumbnail.png'; \\there is no such file yet, xpdf will create it shortly
    $xpdf_path = 'C:\\xpdf\\pdftopng.exe';
    $xpdf_cmd = $xpdf_path.' -f 1 -l 1 -r 100 '.$filepath.' '.$thumbnail_file;
    exec($xpdf_cmd);
    

    参数说明: -f = 要转换为图像的第一页 - 我想要第一页,例如1 -l = 要转换为图像的最后一页 - 我也想要第一页,例如1 您可以同时删除“-f 1 -l 1”,您的输出将是单独 PNG 图像文件中的每一页。

    -r = DPI 分辨率(默认为 150)我不希望 rsult PNG 文件太大,因为它仍然只是一个缩略图,所以我使用了“-r 100”,这会产生大约 50kb 的 png 文件。

    现在您可以选择如何处理新创建的 PNG 图像文件。 在我的项目中,我只需要在使用后显示并删除它,因此我使用 PHP 标头作为 image/png 返回结果并取消链接文件。

    header("Content-type: image/png");
    readfile($thumbnail_file);
    unlink($thumbnail_file);
    

    在 HTML 中使用此代码的可选方式:

    <img src=thumbnail.php?path=PDF_PATH />
    

    有很多方法可以使用它,每种方法都会找到最适合自己需求的实现方式。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2011-02-21
      • 1970-01-01
      • 2012-11-19
      • 2018-02-13
      • 2010-09-06
      • 2014-03-23
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      相关资源
      最近更新 更多