数据库中的图像无法使用 FPDF PHP 在 PDF 上显示

问题描述 投票:0回答:1

我想显示数据库文件夹中的图像,该图像存储的格式是png,jpeg。我已经遵循了正在使用的 FPDF 手册;

function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')

代码如下所示;

$pdf->AddPage('L','A4');

include 'config.php';

if(isset($_POST['add_gambarvk'])){

$gambarvk_name = $_POST['gambarvk_name'];
$gambarvk_image = $_FILES['gambarvk_image']['name'];
$gambarvk_image_tmp_name = $_FILES['gambarvk_image']['tmp_name'];
$size = getimagesize($gambarvk_image_tmp_name, $gambarvk_image_folder);
$gambarvk_image_folder = './uploaded_img/' . $gambarvk_image;

if(empty($gambarvk_name) || empty($gambarvk_image)){
  $message[] = 'please fill out all';
}

  else{

          // Get all the submitted data from the form
          $insert = "INSERT INTO gambarvk(name, image) VALUES('$gambarvk_name',      '$gambarvk_image')";
          $upload = mysqli_query($conn,$insert);
          
          if($upload){

                     //move the uploaded image into the folder: image
                     move_uploaded_file($gambarvk_image_tmp_name,      $gambarvk_image_folder);
                     $message[] = 'Gambar berjaya dimasukkan';
                    }

   else{
        $message[] = 'Gambar tidak berjaya dimasukkan';
       }

}

    $select = mysqli_query($conn, "SELECT * FROM gambarvk ORDER BY id DESC LIMIT 1");

    while($row = mysqli_fetch_assoc($select)){

    
    //function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')


    $pdf->Image('./uploaded_img/'.$gambarvk_image,100,84,310,163, 'JPEG'); 
}

  };


  $pdf->Output();
  ?>

当我生成 PDF 时,它没有显示任何图像。我该如何解决这个问题。

生成 pdf 时,图像应显示在 pdf 上。

php mysqli
1个回答
0
投票

我修改了你的代码:

$pdf->AddPage('L','A4');

include 'config.php';

if(isset($_POST['add_gambarvk'])){

$gambarvk_name = $_POST['gambarvk_name'];
$gambarvk_image = $_FILES['gambarvk_image']['name'];
$gambarvk_image_tmp_name = $_FILES['gambarvk_image']['tmp_name'];
$size = getimagesize($gambarvk_image_tmp_name, $gambarvk_image_folder);
$gambarvk_image_folder = './uploaded_img/' . $gambarvk_image;

if(empty($gambarvk_name) || empty($gambarvk_image)){
  $message[] = 'please fill out all';
}

  else{

          // Get all the submitted data from the form
          $insert = "INSERT INTO gambarvk(name, image) VALUES('$gambarvk_name',      '$gambarvk_image')";
          $upload = mysqli_query($conn,$insert);

//// NEW CODE STARTS FROM HERE ///

// Check if the image file exists
if (file_exists($gambarvk_image_folder)) {
    // Get image dimensions
    list($imageWidth, $imageHeight) = getimagesize($gambarvk_image_folder);

    // Set coordinates and dimensions for the image in the PDF
    $x = 100;
    $y = 84;
    $w = 310;
    $h = 163;

    // Check image format 
    $imageFormat = 'JPEG';

    // Add the image to the PDF
    $pdf->Image($gambarvk_image_folder, $x, $y, $w, $h, $imageFormat);

    // Output the PDF
    $pdf->Output();
} else {
    echo 'Image file does not exist.';
}


//// Put your remaining code here ////


© www.soinside.com 2019 - 2024. All rights reserved.