【问题标题】:how to convert pdf to jpg image format如何将pdf转换为jpg图片格式
【发布时间】:2016-10-18 05:55:51
【问题描述】:

代码写在下面。输出带有成功转换但未在图像上找到的图像。 它转换成功,但图像未在我定义的文件夹中正确生成。有什么建议吗??

<?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;
?>

【问题讨论】:

    标签: php pdf


    【解决方案1】:

    安装ghostscript

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

    在 Linux 上 apt-get install ghostscript

    在 RedHat/Centos 上安装 ghostscript

    安装 ImageMagick

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

    在 Linux 上 apt-get install ImageMagick

    在 RedHat/Centos 上安装 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
    

    【讨论】:

      【解决方案2】:

      你可以使用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

      【讨论】:

        猜你喜欢
        • 2018-12-01
        • 2015-02-09
        • 2018-08-26
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多