代码写在下面。输出来的图像转换成功但不影响图像。 它转换成功,但在我定义的文件夹中未正确生成图像。有什么建议??
<?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 = "photos/";
$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;
?>
答案 0 :(得分:1)
https://www.ghostscript.com/faq.html
在Linux上apt-get install ghostscript
在RedHat / Centos yum上安装ghostscript
https://blog.runcloud.io/imagemagick/
在Linux上apt-get install ImageMagick
在RedHat / Centos yum上安装ImageMagick
您需要安装perl,也可以使用PHP或其他语言自行进行几何计算。
此bash脚本将email_001.pdf转换为email_001.jpg
# convert possibly multi-part PDF to one or more jpegs
gs -sDEVICE=jpeg -dNOPAUSE -dBATCH -dSAFER -r600x600 -sOutputFile=email-%02d.jpg ./email_001.pdf
# get dimensions of the jpeg and then divide it by 4 or you get a huge combined jpg
GEOMETRY=`identify email-01.jpg | perl -ane '{@d=split "x", $F[3]; printf "%dx%d+0+0", $d[0]/4, $d[1]/4}'`
# join the jpegs together into a single jpeg
montage -tile 1 -density 200 -quality 100 -geometry $GEOMETRY ./email-*.jpg ./email_001.jpg
# remove the intermediate jpegs
rm ./email-*.jpg
答案 1 :(得分:0)
你可以使用ImageMagick来做得更轻松
从类Imagick()
<?php
// create Imagick object
$imagick = new Imagick();
// Reads image from PDF
$imagick->readImage('myfile.pdf');
// Writes an image or image sequence Example- converted-0.jpg, converted-1.jpg
$imagick->writeImages('converted.jpg', false);
?>
如果您遇到空输出问题,可以使用以下示例:(Origanl Post Here)
<?php
$pdf = 'testfile.pdf';
$save = 'output.jpg';
exec('convert "'.$pdf.'" -colorspace RGB -resize 800 "'.$save.'"', $output, $return_var);
?>
如果这一切对您没有帮助。查看here了解详细说明