在网页上显示原始数据中的图像 [英] Display Images from raw data on web page

查看:144
本文介绍了在网页上显示原始数据中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能有一个简单的答案,我可能会以错误的方式问它.

This question may have a simple answer and I may be asking it the wrong way.

我有一个Web应用程序,该应用程序通过API(特别是librets和MLS feed)从服务器提取信息.该API的一部分以原始二进制数据的形式返回图像.

I have a web application that pulls information from a server via an API (specifically librets and an MLS feed). Part of the API returns images but in the form of raw binary data.

目前,我正在将此原始二进制数据写入.jpg文件,并将它们存储在我的身边,以备后用.我想知道是否有一种方法可以使用原始数据在网页上显示这些图像,而不必将它们保存在我的身边.

Currently I am writing this raw binary data to a .jpg file and storing them on my side for access later. I was wondering if there was a way to display these images on a webpage using the raw data instead of having to save them on my side.

推荐答案

我同样不确定我的答案是否正确,所以请忽略或说出我的意思,而不是如果我对您的质疑的理解是in the wrong way喜欢你的问题!

I am equally unsure I am on the right track with an answer, so just kindly ignore or say, rather than downvote me if my interpretation of your querstion is in the wrong way like your question!

您可以通过PHP而不是文件来提供图像-我的意思是,您可以让PHP动态创建并提供图像,而不必在Web服务器的文件系统中拥有一个文件,而不必在服务器中按名称引用它. HTML中<image>标记的src字段.

You can serve an image from PHP rather than from a file - I mean you can have PHP dynamically create an image and serve it rather than having to have a file in your webserver's filesystem and having to refer to it by name in the src field of an <image> tag in your HTML.

所以,而不是

<image src="....jpg" alt="..." size="...">

您可以使用

<img src="/php/thumb.php?param1=$col&param2=$ref"/>

这会导致在呈现页面时调用"/php/thumb.php"中的PHP脚本.在该脚本中,您可以像下面这样动态创建图像(如果需要,可以使用其他参数):

which causes the PHP script at `/php/thumb.php" to be called when the page is rendered. In that script, you can dynamically create the image (using extra parameters if you wish) like this:

<?php    
header("Content-type: image/jpeg");
$p1 = $_GET['param1'];
$p2 = $_GET['param2'];

$src = imagecreatefromstring(SOMESTRING);
$dst = imagecreatetruecolor($width,$height);
imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
imagedestroy($src);
imagejpeg($dst);
imagedestroy($dst);
?>

在前三行之后,我省略了一些代码,因此您只看到该技术,而不是我代码的所有细节.您感兴趣的实际行是:

I have omitted some code after the first 3 lines so you just see the technique rather than all the gory details of my code. The actual lines you are interested in are:

header(...image/jpeg);

告诉浏览器即将出现什么类型的东西-即图像,并且

which tells the browser what type of stuff is coming - i.e. an image, and

imagejpeg();

实际上将JPEG数据流发送到浏览器.

which actually sends the stream of JPEG data to the browser.

这篇关于在网页上显示原始数据中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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