需要将mysql数据库中的图像显示到fpdf

发布于 2024-09-24 10:41:26 字数 95 浏览 1 评论 0原文

A 将图像保存在 mysql 数据库中作为 bolb,我希望使用 php 将其显示在 fpdf 中。我在执行此操作时遇到问题,因为我对 fpdf 很陌生。我真的需要帮助。谢谢。

A have image save in mysql database as bolb and I wish to display it out in fpdf using php. I'm having problem doing this as I am very new to fpdf. I really need help. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初吻给了烟 2024-10-01 10:41:26

您将需要 FPDF 的此扩展: http://www.fpdf.org/en/ script/script45.php

[更新]

$query = "SELECT imageField FROM yyy WHERE ...";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$image = $row['imageField'];

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

You will need this extension to FPDF: http://www.fpdf.org/en/script/script45.php

[updated]

$query = "SELECT imageField FROM yyy WHERE ...";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$image = $row['imageField'];

$pdf->MemImage($image, 50, 30);
雪化雨蝶 2024-10-01 10:41:26
//Alternatively
//1) Put the class VariableStream inside your php file instead of declaring it
//--------------------------

enter code here

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();
    }
}
//----------------------------------------------------
//2) open and read your mysql longblob that contains your binary data from your image
//in my case the function declaration is this

function showImage($cdImg) {

Global $pdf; 
    //declare your pdf class
    //Connects the way you do. this case assigned as $odbc_conn

   $query = 'SELECT cd_img, ds_img, bin_img FROM tb_img WHERE cd_img = '.$cdImg;

$result= mysqli_query($odbc_conn, $query);

  $row = mysqli_fetch_array($result);

  if (!empty($row["bin_img"]))
  {

    //IF your data is encoded
    //$data=base64_decode($row['bin_img']);
    //
        $data=$row['bin_img'];
   }

   mysqli_free_result($result);

   mysqli_close($odbc_conn);

    stream_wrapper_register('var', 'VariableStream');

$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)
        $pdf->Error('Invalid image data');
        $type = substr(strstr($a['mime'],'/'),1);
        $pdf->Cell(1);
        $pdf->Cell(26, 1, '', '', 0,'L');
        $pdf->Cell(150, 1, '', '', 1,'L');
        //650=150 choose your options for height and width
        //845=156
        //$xc=ceil((676-$a['width'])/2);

        $pdf->Image('var://'.$v, 44, $y, 150, 0, $type, $link);

        unset($GLOBALS[$v]);
}
//Alternatively
//1) Put the class VariableStream inside your php file instead of declaring it
//--------------------------

enter code here

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();
    }
}
//----------------------------------------------------
//2) open and read your mysql longblob that contains your binary data from your image
//in my case the function declaration is this

function showImage($cdImg) {

Global $pdf; 
    //declare your pdf class
    //Connects the way you do. this case assigned as $odbc_conn

   $query = 'SELECT cd_img, ds_img, bin_img FROM tb_img WHERE cd_img = '.$cdImg;

$result= mysqli_query($odbc_conn, $query);

  $row = mysqli_fetch_array($result);

  if (!empty($row["bin_img"]))
  {

    //IF your data is encoded
    //$data=base64_decode($row['bin_img']);
    //
        $data=$row['bin_img'];
   }

   mysqli_free_result($result);

   mysqli_close($odbc_conn);

    stream_wrapper_register('var', 'VariableStream');

$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)
        $pdf->Error('Invalid image data');
        $type = substr(strstr($a['mime'],'/'),1);
        $pdf->Cell(1);
        $pdf->Cell(26, 1, '', '', 0,'L');
        $pdf->Cell(150, 1, '', '', 1,'L');
        //650=150 choose your options for height and width
        //845=156
        //$xc=ceil((676-$a['width'])/2);

        $pdf->Image('var://'.$v, 44, $y, 150, 0, $type, $link);

        unset($GLOBALS[$v]);
}
浅语花开 2024-10-01 10:41:26

这是代码$pdf->Image('http://www.elwebmaster.com/wp-content/uploads/2015/06/PHP-logo.png',30,278,8); 或者如果在您的根目录中 $pdf->Image($_SESSION['raiz'].'imagens/tsunami.jpg',150,285,12);

您可以在您的sql 数据库,然后使用 $pdf->Image($_SESSION['raiz'].'imagens/picture.php?id=1',150‌ ,285,12); 其中 id 是数据库索引和 picture.php 是检索要显示的图像的文件

this is the code$pdf->Image('http://www.elwebmaster.com/wp-content/uploads/2015/06/PHP-logo.png',30,278,8); or if in your root directory $pdf->Image($_SESSION['raiz'].'imagens/tsunami.jpg',150,285,12);

you could have blob saved images in your sql data base then use $pdf->Image($_SESSION['raiz'].'imagens/picture.php?id=1',150‌​,285,12); where id is the database index and picture.php is the file thats retrieve the image that you want to show

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文