问题描述
我有一个调用数据库类的函数,并要求提供图像列表。
这是下面的函数。
I have a function that calls on a database class, and asks for a list of images.This below is the function.
//Hämtar en array av filer från disk.
public function GetFiles()
{
$sql = "SELECT pic FROM pictures";
$stmt = $this->database->Prepare($sql);
$fileList = $this->database->GetAll($stmt);
echo $fileList;
if($fileList)
{
return $fileList;
}
return false;
}
这是GetFiles调用的我的数据库类方法。
And this is my database class method that GetFiles calls.
public function GetAll($sqlQuery) {
if ($sqlQuery === FALSE)
{
throw new \Exception($this->mysqli->error);
}
//execute the statement
if ($sqlQuery->execute() == FALSE)
{
throw new \Exception($this->mysqli->error);
}
$ret = 0;
if ($sqlQuery->bind_result($ret) == FALSE)
{
throw new \Exception($this->mysqli->error);
}
$data = array();
while ($sqlQuery->fetch())
{
$data[] = $ret;
echo $ret;
}
$sqlQuery->close();
return $data;
}
GetFiles函数的返回值随后将由另一个函数处理
The GetFiles function return value is then later processed by another function
public function FileList($fileList)
{
if(count($fileList) == 0)
{
return "<p class='error'> There are no files in array</p>";
}
$list = '';
$list .= "<div class='list'>";
$list .= "<h2>Uploaded images</h2>";
foreach ($fileList as $file) {
$list .= "<img src=".$file." />";
}
$list .= "</div>";
return $list;
}
但是我的数据库只是把长毛像很多驯鹿一样返回,我该怎么办要让长号显示为图像?
But my database just returns the longblob as a lot of carachters, how do i get the longblob to display as images?
推荐答案
您需要对base64进行编码,然后通过数据URI进行传递,例如
You'd need to base64 encode it and pass it in via a data URI, e.g.
但是,如果您提供的是大图片,这将使您大吃一惊page肿的页面,绝对没有办法缓存图像数据以节省用户以后的下载流量。您最好使用显式的图像服务脚本,例如
However, if you're serving up "large" pictures, this is going to make for a hideously bloated page, with absolutely no way to cache the image data to save users the download traffic later on. You'd be better off with an explicitly image-serving script, e.g.
<img src="getimage.php?imageID=XXX" />
,然后在该脚本中添加数据库代码:
and then have your db code in that script:
$blob = get_image_data($_GET[xxx]);
header('Content-type: image/jpeg');
echo $blob;
这样的问题就是为什么从数据库提供图像通常不是一个好主意。
Problems like this are why it's generally a bad idea to serve images out of a database.
这篇关于PHP Longblob转IMG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!