[英]convert PDF to JPEG in PHP using ZEND server
我正在使用腳本將PDF在線轉換為JPG。 但是,當我在zend服務器上運行此腳本並上傳任何pdf文件時,該文件就會在服務器上上傳並在瀏覽器中顯示損壞的圖像。
我使用一個文件夾和一個索引文件來編寫此代碼。
這是我正在運行的腳本:
<html>
<head>
<style type="text/css">
img {border-width: 0}
* {font-family:'Lucida Grande', sans-serif;}
</style>
</head>
<body>
<?php
$message = "";
$display = "";
if($_FILES)
{
$output_dir = "uploads/";
ini_set("display_errors",1);
if(isset($_FILES["myfile"]))
{
$RandomNum = time();
$ImageName = str_replace(' ','-',strtolower($_FILES['myfile']['name']));
$ImageType = $_FILES['myfile']['type']; //"image/png", image/jpeg etc.
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
if($ImageExt != "pdf")
{
$message = "Invalid file format only <b>\"PDF\"</b> allowed.";
}
else
{
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $NewImageName);
$location = "/usr/local/bin/convert";
$name = $output_dir. $NewImageName;
$num = count_pages($name);
$RandomNum = time();
$nameto = $output_dir.$RandomNum.".jpg";
$convert = $location . " " . $name . " ".$nameto;
exec($convert);
for($i = 0; $i<$num;$i++)
{
$display .= "<img src='$output_dir$RandomNum-$i.jpg' title='Page-$i' /><br>";
}
$message = "PDF converted to JPEG sucessfully!!";
}
}
}
function count_pages($pdfname) {
$pdftext = file_get_contents($pdfname);
$num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
return $num;
}
$content = $message.'<br />'.$display.'<br><form enctype="multipart/form-data" action="" method="post">
Please choose a file: <input name="myfile" type="file" /><br />
<input type="submit" value="Upload" />
</form>';
echo $content;
?>
</body>
</html>
損壞的圖像為:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.