首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将pdf格式转换为jpg格式

如何将pdf格式转换为jpg格式
EN

Stack Overflow用户
提问于 2016-10-18 13:55:51
回答 2查看 7.1K关注 0票数 1

代码是写的,below.The输出附带了成功转换的图像,但没有建立在图像上。它已成功转换,但图像未在我定义的文件夹中正确生成。有什么建议吗?

代码语言:javascript
复制
<?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;
?>
EN

回答 2

Stack Overflow用户

发布于 2020-11-18 22:11:38

安装ghostscript

https://www.ghostscript.com/faq.html

在Linux apt上-get install ghostscript

在RedHat/Centos yum上安装ghostscript

安装ImageMagick

https://blog.runcloud.io/imagemagick/

在Linux apt上-get安装ImageMagick

在RedHat/Centos yum上安装ImageMagick

您需要安装perl,也可以使用PHP或其他语言自己进行几何计算。

转换脚本

此bash脚本将email_001.pdf转换为email_001.jpg

代码语言:javascript
复制
# 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
EN

Stack Overflow用户

发布于 2016-10-18 14:16:42

你可以使用ImageMagick来做更简单的事情。

Imagick()类创建一个新对象

代码语言:javascript
复制
<?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)

代码语言:javascript
复制
<?php
    $pdf = 'testfile.pdf';
    $save = 'output.jpg';

    exec('convert "'.$pdf.'" -colorspace RGB -resize 800 "'.$save.'"', $output, $return_var);
?>

如果所有这些都对您没有帮助,请使用。有关详细说明,请查看

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40100562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档