我正在使用脚本将 PDF 在线转换为 JPG。但是当我在 zend 服务器上运行这个脚本并上传任何 pdf 文件时,文件就会上传到服务器上,并且在浏览器上显示损坏的图像。
我在编写这段代码的地方使用了一个文件夹和一个索引文件。
- 上传文件夹
- index.php文件
这是我正在运行的脚本:
<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>
损坏的图像是:
最佳答案
根据您的要求在浏览器上查看 PDF。 在线尝试 pdf viewer . Download它和享受。我希望它对你有用。
关于php - 使用 ZEND 服务器在 PHP 中将 PDF 转换为 JPEG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23967946/