一个PDF文件中的多个图像

Searching for mPdf Code Example to convert multiple images into a single Pdf. but I failed. Anyone having any code example or link to read please suggest. Thanks in advance.

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

?>

Here is the code i tried.But Failed

This line of your code is completely wrong:

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

If you want to get uploaded images, you can format your html like below:

Generatepdf.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>

With the above html, you can upload multiple images. And you get the uploaded images using the following php code:

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]);
}

As the MPDF image() method does not accept image resource, I used a quick work around with GD image functions. So your new PDF class:

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

The code to generate images on one 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');

I don't have much time to play with MPDF image() method. You can visit the link and see what you can play with. I hope this will help you and give you something to work with GD library as well.