如何将base64图像插入fpdf?
How to insert base64 image into fpdf?
我已经看到了对此的各种回答,但无论出于何种原因,我似乎无法让它们工作。
首先,我从我的数据库中提取文件:
try {
$getQuery = "SELECT * FROM firstForm WHERE id =:id";
$statement = $db->prepare($getQuery);
$statement->execute(array(':id' => 2));
if($row = $statement->fetch()) {
$fileName = $row['pic'];
}
}
当我回显 $fileName 它产生 "data:image/png;base64..."
如何转换 $fileName 以便我可以在 fpdf 中使用它,如下所示:
$pdf = new FPDF('P','in','A4'); // orientation (portrait), inches, ??
$pdf->SetMargins(0.5,0.5,0.5); // l, t, rs
$pdf->AddPage();
$pdf->Cell(4,3,'', 1, 2, 'C'); // w, h, text, border width, ???, align
$pdf->Image($fileName);
$pdf->Output();
不过,正如 James 所建议的那样,答案 print BASE64 coded image into a FPDF document 提供了一个很好的解决方案。在您的情况下,您可以将以下代码保存为 image.php
,然后将该图像(设置为 $_GET['id']
)添加到 pdf 中:$pdf->Image('/image.php?id=2');
。调用image.php
.
前一定要检查图片是否存在于数据库中
<?php
if ($_SERVER['SERVER_ADDR'] != $_SERVER['REMOTE_ADDR']) { // Only allow requests from server.
header($_SERVER["SERVER_PROTOCOL"] . " 791 The Internet shut down due to copyright restrictions", true, 791);
die();
}
if (empty($_GET['id'])) {
die('Handle this.');
}
// get your $db object in here.
$getQuery = "SELECT * FROM firstForm WHERE id =:id";
$statement = $db->prepare($getQuery);
$statement->execute(array(':id' => 2));
if ($row = $statement->fetch()) {
$data = $row['pic'];
} else {
// This shouldn't happening, you should be checking whether firstForm with the id you're calling the image for exists.
}
$exploded = explode(';', $data);
$mimetype = substr($exploded[0], 5);
$data = explode(',', $exploded[1])[1];
header('Content-type: ' . $mimetype);
echo base64_decode($data);
您的 pdf 创建看起来像这样:
$pdf = new FPDF('P','in','A4'); // orientation (portrait), inches, ??
$pdf->SetMargins(0.5,0.5,0.5); // l, t, rs
$pdf->AddPage();
$pdf->Cell(4,3,'', 1, 2, 'C'); // w, h, text, border width, ???, align
$pdf->Image('/image.php?id=2');
$pdf->Output();
所以,我想通了。答案就在这个 SO answer print BASE64 coded image into a FPDF document 中并且有效。
问题在于图像是如何存储在数据库中的。当出于某种原因需要将其存储为 LONGBLOG 时,我将其存储为 BLOB。
感谢您尝试@Nielles。
我已经看到了对此的各种回答,但无论出于何种原因,我似乎无法让它们工作。
首先,我从我的数据库中提取文件:
try {
$getQuery = "SELECT * FROM firstForm WHERE id =:id";
$statement = $db->prepare($getQuery);
$statement->execute(array(':id' => 2));
if($row = $statement->fetch()) {
$fileName = $row['pic'];
}
}
当我回显 $fileName 它产生 "data:image/png;base64..."
如何转换 $fileName 以便我可以在 fpdf 中使用它,如下所示:
$pdf = new FPDF('P','in','A4'); // orientation (portrait), inches, ??
$pdf->SetMargins(0.5,0.5,0.5); // l, t, rs
$pdf->AddPage();
$pdf->Cell(4,3,'', 1, 2, 'C'); // w, h, text, border width, ???, align
$pdf->Image($fileName);
$pdf->Output();
不过,正如 James 所建议的那样,答案 print BASE64 coded image into a FPDF document 提供了一个很好的解决方案。在您的情况下,您可以将以下代码保存为 image.php
,然后将该图像(设置为 $_GET['id']
)添加到 pdf 中:$pdf->Image('/image.php?id=2');
。调用image.php
.
<?php
if ($_SERVER['SERVER_ADDR'] != $_SERVER['REMOTE_ADDR']) { // Only allow requests from server.
header($_SERVER["SERVER_PROTOCOL"] . " 791 The Internet shut down due to copyright restrictions", true, 791);
die();
}
if (empty($_GET['id'])) {
die('Handle this.');
}
// get your $db object in here.
$getQuery = "SELECT * FROM firstForm WHERE id =:id";
$statement = $db->prepare($getQuery);
$statement->execute(array(':id' => 2));
if ($row = $statement->fetch()) {
$data = $row['pic'];
} else {
// This shouldn't happening, you should be checking whether firstForm with the id you're calling the image for exists.
}
$exploded = explode(';', $data);
$mimetype = substr($exploded[0], 5);
$data = explode(',', $exploded[1])[1];
header('Content-type: ' . $mimetype);
echo base64_decode($data);
您的 pdf 创建看起来像这样:
$pdf = new FPDF('P','in','A4'); // orientation (portrait), inches, ??
$pdf->SetMargins(0.5,0.5,0.5); // l, t, rs
$pdf->AddPage();
$pdf->Cell(4,3,'', 1, 2, 'C'); // w, h, text, border width, ???, align
$pdf->Image('/image.php?id=2');
$pdf->Output();
所以,我想通了。答案就在这个 SO answer print BASE64 coded image into a FPDF document 中并且有效。
问题在于图像是如何存储在数据库中的。当出于某种原因需要将其存储为 LONGBLOG 时,我将其存储为 BLOB。
感谢您尝试@Nielles。