一个PDF文件中的多个图像

时间:2018-06-18 16:17:44

标签: php image-processing mpdf

搜索mPdf代码示例以将多个图像转换为单个Pdf。但我失败了。有任何代码示例或链接阅读的人请建议。提前致谢。

    <?php
require_once("mpdf60/mpdf.php");
$imageStorage='';
$p1=$_FILES['p1']['name'];
$p1Temp=$_FILES['p1']['tmp_name'];
$p2=$_FILES['p2']['name'];
$p2Temp=$_FILES['p2']['tmp_name'];
$p3=$_FILES['p3']['name'];
$p3Temp=$_FILES['p3']['tmp_name'];
$p4=$_FILES['p4']['name'];
$p4Temp=$_FILES['p4']['tmp_name'];
$imageStorage=$_FILES['p1']['tmp_name'],$_FILES['p2']['tmp_name'],$_FILES['p3']['tmp_name'];,$_FILES['p4']['tmp_name'];

class PDF extends MPDF {
  const DPI = 96;
  const MM_IN_INCH = 25.4;
  const A4_HEIGHT = 297;
  const A4_WIDTH = 210;
// tweak these values (in pixels)
  const MAX_WIDTH = 800;
  const MAX_HEIGHT = 500;
  function pixelsToMM($val) {
    return $val * self::MM_IN_INCH / self::DPI;
  }
  function resizeToFit($imgFilename) {
    list($width, $height) = getimagesize($imgFilename);
    $widthScale = self::MAX_WIDTH / $width;
    $heightScale = self::MAX_HEIGHT / $height;
    $scale = min($widthScale, $heightScale);
    return array(
      round($this->pixelsToMM($scale * $width)),
      round($this->pixelsToMM($scale * $height))
    );
  }
  function centreImage($img) {
    list($width, $height) = $this->resizeToFit($img);
    $this->Image(
      $img, (self::A4_HEIGHT - $width) / 2,
      (self::A4_WIDTH - $height) / 2,
      $width,
      $height
    );
  }
}


$pdf = new PDF();
$pdf->AddPage("L");
$pdf->centreImage($imageStorage);
$pdf->Output();

?>

这是我试过的代码。但是失败了

1 个答案:

答案 0 :(得分:1)

这行代码是完全错误的:

$imageStorage=$_FILES['p1']['tmp_name'],$_FILES['p2']['tmp_name'],$_FILES['p3']['tmp_name'];,$_FILES['p4']['tmp_name'];

如果要获取上传的图像,则可以如下设置html格式:

生成pdf.php

<form method="post" action="uploaded.php" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple>
    <input type="submit" name="submit" value="Upload">
</form>

使用上述html,您可以上传多张图片。然后,您可以使用以下php代码获取上传的图片:

Uploaded.php

require_once("mpdf.php");//
$uploaded = [];//New empty array for holding uploaded images
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
  $uploaded[] = file_get_contents($_FILES['files']['tmp_name'][$key]);
}

由于MPDF image()方法不接受图像资源,因此我快速处理了GD图像功能。因此,您的新PDF类:

class PDF extends MPDF {
  const DPI = 96;
  const MM_IN_INCH = 25.4;
  const A4_HEIGHT = 297;
  const A4_WIDTH = 210;
// tweak these values (in pixels)
  const MAX_WIDTH = 800;
  const MAX_HEIGHT = 500;
  function pixelsToMM($val) {
    return $val * self::MM_IN_INCH / self::DPI;
  }
  function resizeToFit($imgFilename) {
    list($width, $height) = getimagesizefromstring($imgFilename);
    $widthScale = self::MAX_WIDTH / $width;
    $heightScale = self::MAX_HEIGHT / $height;
    $scale = min($widthScale, $heightScale);
    return array(
      round($this->pixelsToMM($scale * $width)),
      round($this->pixelsToMM($scale * $height))
    );
  }
  function centreImage($imgdata) {
    list($width, $height) = getimagesizefromstring($imgdata);//Old size
    list($newwidth, $newheight) = $this->resizeToFit($imgdata);//new size
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromstring($imgdata);
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);  
    ob_start(); // Let's start output buffering.
    imagejpeg($thumb); //This will normally output the image, but because of ob_start(), it won't.
    $contents = ob_get_contents(); //Instead, output above is saved to $contents
    ob_end_clean(); //End the output buffer.    
    return $contents;
  }
}

在一个PDF上生成图像的代码:

$pdf = new PDF();
$pdf->AddPage("L");
$pdf->SetColumns(4,2);//Four columns, 2mm gap between the columns
$pdf->max_colH_correction = 10;
foreach($uploaded as $changefile){
  $pdf->WriteHTML("<img src='data:image/jpeg;base64,".base64_encode($pdf->centreImage($changefile))."' />");
}  
$pdf->Output('filename.pdf','F');

我没有太多时间来使用MPDF image()方法。您可以访问该链接并查看可以玩什么。希望这对您有所帮助,并为您提供一些GD library可以使用的功能。