簡體   English   中英

如何將pdf轉換成jpg圖片格式

[英]how to convert pdf to jpg image format

代碼寫在下面。輸出帶有轉換成功但未在圖像上找到的圖像。 它轉換成功,但在我定義的文件夾中沒有正確生成圖像。 有什么建議嗎??

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

安裝ghostscript

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

在 Linux 上 apt-get install ghostscript

在 RedHat/Centos 上 yum install ghostscript

安裝 ImageMagick

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

在 Linux 上 apt-get 安裝 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

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

如果您遇到空輸出問題,這是一些示例:(原帖在這里

<?php
    $pdf = 'testfile.pdf';
    $save = 'output.jpg';

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


如果這一切對你沒有幫助。 這里的詳細解釋

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM