2017-04-04 93 views
-1

我是imageMagick的新用戶。我在將pdf上傳到目錄後試圖顯示pdf縮略圖。 exec()函數似乎不能在我的代碼中工作。 pdf正在完美上傳到目錄中,但不會顯示縮略圖,而是顯示與pdf的鏈接。我想讓縮略圖顯示在屏幕上。請幫我解決!使用php顯示縮略圖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>'; 
     } 
    } 
    ?> 
+0

嘗試'shell_exec()'一次。 –

回答

0

捕獲EXEC的()的結果,然後粘貼的結果。

我認爲這是因爲你的shell命令字符串沒有正確構建。當在PHP中構建用於向字符串中注入變量的shell命令時(例如文件名),實際上應該使用escapeshellarg函數來避免命令行中未轉義字符的問題。

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