代码之家  ›  专栏  ›  技术社区  ›  Mango D

使用PDFlib(PHP)将MySQL中的图像块加载到PDF中

  •  0
  • Mango D  · 技术社区  · 7 年前

    如何转换斑点,以便将其用作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图像也可以使用)?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Rainer    7 年前

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

    这在starter_pvf中得到了演示。php示例,包括在PDFlib php包中以及PDFlib CoobBook中: 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", "");
    
        2
  •  1
  •   alistaircol    7 年前

    我认为这样的事情对你会有用:

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