I am trying to display an image from a MySQL table in pdf format. I when I execute the code, I receive the following error:
Connection established Database selected Query: 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¥`ÿÙ
I have stored the image in my mysql database as a blob file. My code is as follows:
<?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');
?>
You have to use MemImage
function to display blob
data of image.
$pdf->MemImage($url, 50, 30);
Edit
Source
<?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);
}
}
?>
Example
<?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();
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.