Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php显示缩略图pdf_Php_Html_Pdf_Imagemagick_Imagick - Fatal编程技术网

使用php显示缩略图pdf

使用php显示缩略图pdf,php,html,pdf,imagemagick,imagick,Php,Html,Pdf,Imagemagick,Imagick,我不熟悉imageMagick。我试图显示pdf缩略图后,我们上传到该目录的pdf。 exec()函数在我的代码中似乎不起作用。 pdf文件正完美地上传到目录中,但缩略图并没有显示出来,而是显示了指向pdf的链接。我想在屏幕上显示缩略图。请帮帮我 <html> <head> <meta charset="utf-8" /> <title>PDF Preview Image</title> </h

我不熟悉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>';
        }
    }
    ?>

PDF预览图像
仅选择pdf文件

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

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

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

$thumbDirectory.escapeshellarg($thumb)

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

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

每个人都建议使用ImageMagick和GhostScript,我觉得对于这样一个小任务来说,这是一种过分的手段

首先从下载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 />

有很多方法可以使用它,每个方法都会找到满足其需求的最佳实现

希望有帮助

请尝试
shell\u exec()
一次。