【发布时间】: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