简体   繁体   English

PHP的:如何从blob显示图像

[英]PHP : How to display image from blob

I have a little problem here. 我这里有点问题。 I try to convert an image into string base64, after that I want to save the string into blob in MySQL. 我尝试将图像转换为字符串base64,之后再将字符串保存到MySQL中的blob中。 So, the blob can be displayed on the mobile apps. 因此,斑点可以显示在移动应用程序上。

this is my code : 这是我的代码:

$data = file_get_contents($_FILES["picture"]["tmp_name"]);
$image = base64_encode($data);

I already successfully save the blob into MySQL, but I can not displayed the image in website. 我已经成功将Blob保存到MySQL,但是无法在网站上显示该图像。

<td> <img src="<?php echo base64_decode($user->getPicture()); ?>"></td>

because the result is : and many more 因为结果是: and many more

Am I wrong ? 我错了吗 ? Please correct me :) 请纠正我:)

The src attribute of an image MUST point at a url. 图片的src属性必须指向网址。 You cannot dump the raw binary contents of an image in there and expect it to work. 您不能在其中转储图像的原始二进制内容并期望它能工作。 The browser will take that raw binary data and try to hit the page's originating server and request that data as if it was a file url. 浏览器将获取该原始二进制数据,并尝试访问页面的原始服务器,并请求该数据,就好像它是文件url。 ie you have this on a page loaded from http://example.com/foo/bar/baz.php : 也就是说,您在从http://example.com/foo/bar/baz.php加载的页面上有此代码:

<img src="blahblahblahblah" />

which will result in the browser requesting 这将导致浏览器请求

http://example.com/foo/bar/blahblahblahblah

If you want to embed your image in the page, then you have to use a Data URI : 如果要在页面中嵌入图像,则必须使用Data URI

<img src="data:image/jpeg;base64,<?php echo $base64_encoded_image ?>" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM