从MySQL在PHP拉动BLOB的图像数据 [英] Pulling BLOB image data from MySQL in PHP

查看:114
本文介绍了从MySQL在PHP拉动BLOB的图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我就如何我的图片上传到数据库二进制而不是把他们的服务器本身的一些教程阅​​读,以及我得到它像这样的工作:

I was reading from a few tutorials on how to upload my image into the DB as binary opposed to putting them on the server itself, well I got it to work like this:

PHP:

$image = chunk_split(base64_encode(file_get_contents($tmpfile)));
    mysql_query("INSERT INTO images (`img_location`, `caption`, `user`, `genre`, `when`) VALUES ('$image', '$caption', '$id', '$genre', '$when')");

我的问题是你怎么现在从数据库中提取它,我读过这样做的几种方法,尝试所有这些,也无法弄明白,我没有得到一个MySQL错误,这里就是我的'中号尝试它:

My issue is how do you now pull it from the database, I've read several ways of doing it, tried them all, can't figure it out, I'm not getting a MySQL error, here's how I'm trying it:

$get_pics = mysql_query("SELECT * FROM images WHERE user='$id' ");
while($get_pics2 = mysql_fetch_array($get_pics))
{
$sixfour_enc = base64_decode($get_pics2['img_location']);

$new .= "<img src=\"".$sixfour_enc."\" >";
}

这个工程...样的,正在发生的事情是,它打印出原始二进制中的IMG标记。

This works... kind of, what's happening is that it's printing out raw binary in the IMG tag.

我如何得到这个重新编译到readble形象?此外,在存储在数据库中的图像愚蠢吗?如果我只是做我平时做,并将其存储在服务器上?

How do I get this to compile to a readble image again? Also, is storing the images in the database stupid? Should I just do what I usually do and store them on the server?

感谢您
-Mike

Thank you -mike

推荐答案

您可以在数据库中存储的图像,如果你想(虽然有没有错只是将其存储为文件要么,选择任何在您的情况是合适的),但存储原始二进制数据的BLOB(即不带code将其使用Base64)。您可以嵌入您从的file_get_contents 直接查询得到的二进制数据,只要你使用正确的逃生功能( mysql_real_escape_string 你的情况)第一位。

You can store images in your database if you want to (though there's nothing wrong with just storing them as files either, choose whatever is appropriate in your situation), but store the raw binary data in a BLOB (i.e. don't encode it with base64). You can embed the binary data you get from file_get_contents in your query directly, provided you use the proper escape function (mysql_real_escape_string in your case) first.

对于图像的输出,你可以做你现在正在做的方式,但你必须把它输出的base64恩codeD并以数据 URI方案是这样的:

As for the outputting of the image, you can do it the way you're doing it right now, but you'll have to output it base64-encoded and with a data URI scheme like this:

echo '<img alt="embedded image" src="data:image/png;base64,' . chunk_split(base64_encode($get_pics2['img_location'])) . '">';

请注意,有一些优点和嵌入图像数据的缺点。是Base64编码的严重开销(大约比原来大33%)和潜在的缓存问题的一些重要的缺点要注意。

Note that there are some advantages and disadvantages of embedded image data. Some important disadvantages to be aware of are the severe overhead of base64 encoding (around 33% larger than original) and potential caching problems.

这篇关于从MySQL在PHP拉动BLOB的图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆