使用 PDFlib (PHP) 将图像 blob 从 MySQL 加载到 PDF 中

Load an image blob from MySQL into PDF with PDFlib (PHP)

我想从 MySQL 获取我的图像 blob 并将其粘贴到带有 PDFlib 的 PDF 中。 如何转换 blob 以便我可以将其用作 PDFlib 的普通图像?

这就是我目前正在尝试的方式。

$img = imagecreatefromstring($qra['photo']); //$qra['photo'] is the mysql blob field

// Loading the image with PDFlib (this code is already tested with a normal image src like /images/example.jpg and works)
$image = $p->load_image("auto", $img, "");
$p->fit_image($image, 10, 10, "boxsize={50 50} position=center fitmethod=meet");

不知何故,imagecreatefromstring 无法正确创建图像,因为我在使用该代码时遇到错误。

如何正确获取 blob 图像以便我可以使用它(将其保存在服务器上作为 tmp 图像也可以)?

我认为这样的东西对你有用:

<?php

$img = imagecreatefromstring($qra['photo']); //$qra['photo'] is the mysql blob field

ob_start(); // capture the image from the blob
imagepng($img); //  Output a PNG image to either the browser or a file
$img = ob_get_clean(); // Get current buffer contents and delete current output buffer

// img should now be png resource

// Loading the image with PDFlib (this code is already tested with a normal image src like /images/example.jpg and works)
$image = $p->load_image("auto", $img, "");
$p->fit_image($image, 10, 10, "boxsize={50 50} position=center fitmethod=meet");

这个比较简单。我希望您将图像作为字节数组存储在数据库中。所以它应该很简单,当您只检索 BLOB 字段作为字符串并使用 PVF(PDFlib 虚拟文件系统)时。 使用这种技术,您可以为变量命名,之后可以将其用于 load_image() 调用。

这在 starter_pvf.php 示例中进行了演示,该示例包含在 PDFlib PHP 包以及 PDFlib Coobkook 中: http://www.pdflib.com/pdflib-cookbook/general-programming/starter-pvf/php-starter-pvf/

示例只是从光盘中读取图像数据,但这应该类似于从数据库中获取图像数据

# We just read some image data from a file; to really benefit
# from using PVF read the data from a Web site or a database instead

$imagedata = read_file("../input/PDFlib-logo.tif");
$p->create_pvf("/pvf/image", $imagedata, "");

# Load the image from the PVF
$image = $p->load_image("auto", "/pvf/image", "");