在没有 header(“Content-type: image/jpg”) 的 php 中将 Blob 显示为图像

发布于 2024-08-23 05:40:06 字数 163 浏览 1 评论 0原文

我从 mySql 数据库中提取二进制数据,并希望将其显示为图像。

我不想为它创建一个单独的页面来显示图像(这将涉及对数据库的额外调用等)

我只是希望能够做

很多事情,但是 $Image 变量是其 longblob 格式并且我需要转换它。

提前致谢。

Im pulling the binary data out of my mySql database and want to display it as a image.

I do not want to make a separate page for it to display the image (this would involve a extra call to the databae among other things)

I simply want to be able to do

Pretty much but the $Image variable is in its longblob format and I need to convert it.

THanks in advance.

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

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

发布评论

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

评论(5

寄与心 2024-08-30 05:40:06

我知道这不是您问题的具体答案,但请考虑通过删除该数据库调用,您将显着增加服务器负载,增加每个页面的大小并降低站点的响应速度。

考虑任何页面堆栈溢出。大部分是动态的,因此页面无法被缓存。但用户的缩略图是静态的并且可以被缓存。

如果您将缩略图作为数据 URI 发送,则您将为每个页面上的每个缩略图执行数据库查找和数据传输。

如果您将其作为链接图像发送,则在首次加载图像时会进行一次数据库查找,从那时起它将被缓存(如果您随其发送正确的 HTTP 标头),从而使您的服务器负载更轻,并且您的网站运行得更快!

I know this is not a specific answer to your question, but consider that by removing that database call, you are dramatically increasing your server load, increasing the size of each page and slowing down the responsiveness of your site.

Consider any page stackoverflow. Most of it is dynamic, so the page cannot be cached. but the users' thumbnail is static and can be cached.

If you send the thumbnail as a data URI, you are doing the DB lookup and data transfer for every thumbnail on every page.

If you send it as a linked image, you incur a single DB lookup for when the image is first loaded, and from then on it will be cached (if you send the correct HTTP headers with it), making your server load lighter, and your site run faster!

怎樣才叫好 2024-08-30 05:40:06

我不想制作一个单独的页面来显示图像

您可以对图像数据进行 base64 编码并将其作为 数据 URI。在大多数情况下,这不是一个好主意:

  • IE 不支持它。 8
  • (显然)它极大地增加了 HTML 页面的大小。
  • 它会减慢渲染速度,因为浏览器必须先加载资源才能完成 HTML 渲染

最好构建一个单独的脚本,并进行一次额外的调用。

I do not want to make a separate page for it to display the image

You can base64 encode your image data and include it directly into the markup as a data URI. In most cases, that's not a good idea though:

  • It's not supported by IE < 8
  • It (obviously) sizes up the HTML page massively.
  • It slows down rendering because the browser has to load the resource first before it can finish HTML rendering

Better build a separate script, and make that one extra call.

和影子一齐双人舞 2024-08-30 05:40:06

您可以使用 Base64 编码的数据 URI 来完成此操作。

我不确定是否可以直接进入 img 标签,但您可以通过为 div 设置背景图像来实现。

基本上,您将常规更改

.smurfette {
    background: url(smurfette.png);
}

.smurfette {
    background: url(data:image/png;base64,iVBORw0KGgo [...] P6VAAAAAElFTkSuQmCC);
}

数据 URI 支持:

* Firefox 2+
* Safari – all versions
* Google Chrome – all versions
* Opera 7.2+
* Internet Explorer 8+

从 Robert Nyman 借来的信息: http://robertnyman.com/2010/01/15/how-to-reduce-the-number-of-http-requests/

You could probably do this using Base64-encoded Data URIs.

I'm not sure if it's possible to do straight into a img-tag, but you can do it by setting a background-image for a div.

Basically you change the regular

.smurfette {
    background: url(smurfette.png);
}

to

.smurfette {
    background: url(data:image/png;base64,iVBORw0KGgo [...] P6VAAAAAElFTkSuQmCC);
}

Data URIs are supported in:

* Firefox 2+
* Safari – all versions
* Google Chrome – all versions
* Opera 7.2+
* Internet Explorer 8+

Info borrowed from Robert Nyman: http://robertnyman.com/2010/01/15/how-to-reduce-the-number-of-http-requests/

只怪假的太真实 2024-08-30 05:40:06

$_GET 将结果放入单独的变量中,即 $myvar = $_GET['Id'];在处理 $imageResult 行之前,例如:

$myid = $_GET['Id'];

$ImageResult = "从 Id = '$myid' 的玩家中选择player.Image";

$_GET the result into a separate variable i.e. $myvar = $_GET['Id']; before you process the $imageResult line e.g.:

$myid = $_GET['Id'];

$ImageResult = "select player.Image from player where Id = '$myid'";

记忆で 2024-08-30 05:40:06

感谢您的回答,我决定使用单独的 GetImage.php 页面,但现在似乎无法执行最简单的任务

$ImageResult = "select player.Image from player where Id = " . $_GET['Id']; 

    $result = mysql_query($ImageResult) or die ("data recovery failed -3");

    header("Content-type: image/jpeg");
    echo mysql_result($result, 0);

但这仅返回一个损坏的链接,无法找出我错过的内容

thanks for the answers, ive decided to go with a separate GetImage.php page but now cant seem to do the simplest of tasks

$ImageResult = "select player.Image from player where Id = " . $_GET['Id']; 

    $result = mysql_query($ImageResult) or die ("data recovery failed -3");

    header("Content-type: image/jpeg");
    echo mysql_result($result, 0);

But this returns just a broken link and cannot work out what I have missed out

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