[英]Displaying thumbnail pdf using php
我是 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>';
}
}
?>
捕獲 exec() 的結果,然后粘貼結果。
我認為這是因為您的 shell 命令字符串未正確構建。 在 PHP 中構建 shell 命令並將變量注入字符串時,例如文件名,您確實應該使用escapeshellarg
函數來避免命令行中未轉義字符的問題。
'convert '.escapeshellarg($pdfWithPath).' -colorspace RGB -geometry 200 '. $thumbDirectory .escapeshellarg($thumb)
經過長時間的研究,我找到了一個使用 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 標頭作為圖像/png 返回結果並取消鏈接文件。
header("Content-type: image/png");
readfile($thumbnail_file);
unlink($thumbnail_file);
在 HTML 中使用此代碼的可選方式:
<img src=thumbnail.php?path=PDF_PATH />
有很多方法可以使用它,每個方法都會找到滿足其需求的最佳實現。
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.