1

我正在尝试以 pdf 格式显示 MySQL 表中的图像。我在执行代码时收到以下错误:

已建立连接 已选择数据库 查询:SELECT * FROM users WHERE id = 31 executed FPDF error: Unsupported image type:/Û7úw‹é(n¼Þcé^Ô£-ûÑŒöÍä¿j÷÷§íÛÉ~•*v¶ g¿½xßnÞkô¯g¥ ¸ßnÞkô©r¶§Ù´®{±Þݼ—és÷¿ÿ¨o%úwµ(ê—Òº<ýïÇêÉ~•çïn;Û·'ý*t Ûa¤túuŒöíä¿j©ô›í›È}+ Ê”´†¶x}"Åûä>•_Çñ>Õ¼‡Ò¥jÔƒª]ž½‰ö§È}+ÏÇ1Ôû¾•*q£j}m¼g´>ï¥oÇ1Ðû¾•*v§ÙáÛ8h}ßj¯â÷ý ¡÷}*t¬mo²§j^ýgÝyÞÆ\aÄŠò¥c[0©r¥`ÿÙ

我已将图像作为 blob 文件存储在我的 mysql 数据库中。我的代码如下:

<?php
require 'fpdf/fpdf.php';
$host      = "localhost";
$user      = "root";
$pass      = "";
$db        = "cliniops";
$usertable = "users";

// Create fpdf object
$pdf = new FPDF('P', 'pt', 'Letter');

// Add a new page to the document
$pdf->addPage();

// Try to connect to DB
$r = mysql_connect($host, $user, $pass);
if (!$r) {
    echo "Could not connect to server\n";
    trigger_error(mysql_error(), E_USER_ERROR);
} else {
    echo "Connection established\n";
}

// Try to select the database
$r2 = mysql_select_db($db);
if (!$r2) {
    echo "Cannot select database\n";
    trigger_error(mysql_error(), E_USER_ERROR);
} else {
    echo "Database selected\n";
}

// Try to execute the query
$query = "SELECT * FROM users WHERE id = 31";
$rs    = mysql_query($query);
if (!$rs) {
    echo "Could not execute query: $query";
    trigger_error(mysql_error(), E_USER_ERROR);
} else {
    echo "Query: $query executed\n";
}

while ($row = mysql_fetch_assoc($rs)) {
    // Get the image from each row
    $url = $row['image'];

    // Place the image in the pdf document
    $pdf->Image($url);
}

// Close the db connection
mysql_close();

// Close the document and save to the filesystem with the name images.pdf
$pdf->Output('images1.pdf', 'F');

?>
4

1 回答 1

1

您必须使用MemImage功能来显示blob图像数据。

$pdf->MemImage($url, 50, 30);

资源

编辑

资源

<?php
require('fpdf.php');

//Stream handler to read from global variables
class VariableStream
{
var $varname;
var $position;

function stream_open($path, $mode, $options, &$opened_path)
{
    $url = parse_url($path);
    $this->varname = $url['host'];
    if(!isset($GLOBALS[$this->varname]))
    {
        trigger_error('Global variable '.$this->varname.' does not exist', E_USER_WARNING);
        return false;
    }
    $this->position = 0;
    return true;
}

function stream_read($count)
{
    $ret = substr($GLOBALS[$this->varname], $this->position, $count);
    $this->position += strlen($ret);
    return $ret;
}

function stream_eof()
{
    return $this->position >= strlen($GLOBALS[$this->varname]);
}

function stream_tell()
{
    return $this->position;
}

function stream_seek($offset, $whence)
{
    if($whence==SEEK_SET)
    {
        $this->position = $offset;
        return true;
    }
    return false;
}

function stream_stat()
{
    return array();
}
}

class PDF_MemImage extends FPDF
{
function PDF_MemImage($orientation='P', $unit='mm', $format='A4')
{
    $this->FPDF($orientation, $unit, $format);
    //Register var stream protocol
    stream_wrapper_register('var', 'VariableStream');
}

function MemImage($data, $x=null, $y=null, $w=0, $h=0, $link='')
{
    //Display the image contained in $data
    $v = 'img'.md5($data);
    $GLOBALS[$v] = $data;
    $a = getimagesize('var://'.$v);
    if(!$a)
        $this->Error('Invalid image data');
    $type = substr(strstr($a['mime'],'/'),1);
    $this->Image('var://'.$v, $x, $y, $w, $h, $type, $link);
    unset($GLOBALS[$v]);
}

function GDImage($im, $x=null, $y=null, $w=0, $h=0, $link='')
{
    //Display the GD image associated to $im
    ob_start();
    imagepng($im);
    $data = ob_get_clean();
    $this->MemImage($data, $x, $y, $w, $h, $link);
}
}
?>

例子

<?php
require('mem_image.php');

$pdf = new PDF_MemImage();
$pdf->AddPage();

//Load an image into a variable
$logo = file_get_contents('logo.jpg');
//Output it
$pdf->MemImage($logo, 50, 30);

//Create a GD graphics
$im = imagecreate(200, 150);
$bgcolor = imagecolorallocate($im, 255, 255, 255);
$bordercolor = imagecolorallocate($im, 0, 0, 0);
$color1 = imagecolorallocate($im, 255, 0, 0);
$color2 = imagecolorallocate($im, 0, 255, 0);
$color3 = imagecolorallocate($im, 0, 0, 255);
imagefilledrectangle($im, 0, 0, 199, 149, $bgcolor);
imagerectangle($im, 0, 0, 199, 149, $bordercolor);
imagefilledrectangle($im, 30, 100, 60, 148, $color1);
imagefilledrectangle($im, 80, 80, 110, 148, $color2);
imagefilledrectangle($im, 130, 40, 160, 148, $color3);
//Output it
$pdf->GDImage($im, 120, 25, 40);
imagedestroy($im);

$pdf->Output();
?>
于 2013-08-06T03:54:34.770 回答