简体   繁体   English

HTML“ Content-Type”标签动态更改

[英]HTML “Content-Type” tag dynamic change

I started working on page for my school project today, and I find myself stucked. 我今天开始为我的学校项目制作页面,但发现自己陷入了困境。 The HTML and PHP is quite simple, even if PHP I saw for first time, because it's almost same as C. But the problem I got is about HTML meta tags and so on, specificaly the content-type. HTML和PHP非常简单,即使我第一次看到PHP也是如此,因为它与C几乎相同。但是我遇到的问题是关于HTML元标记等等,具体来说就是内容类型。 I want to generate image with php code which I already do with an ease, but when I want to display it that tag comes to game. 我想用已经很容易做到的php代码生成图像,但是当我要显示它时,这个标记就出现在游戏中了。 I already came to conclusion that setting Content-Type tag with PHP is using last call to command like 我已经得出结论,用PHP设置Content-Type标记正在使用对命令的最后一次调用,例如

header("Content-type:image/jpeg");

Now that I want to display my image, this tag has to be set as in the command, but then the whole page comes as image. 现在,我要显示图像,必须像命令中一样设置此标记,但是整个页面都将作为图像显示。 On other hand with setting text/html, image displays as text. 另一方面,设置text / html时,图像显示为文本。 Is it possible to dynamicaly change that element in one file? 是否可以在一个文件中动态更改该元素? I doubt, because only one head specification is possible at all, but I think there are guys much smarter then me with some work-arounds. 我对此表示怀疑,因为根本不可能只有一个头部规格,但是我认为有些人比我更聪明一些。

Everything I want to do now is generating image and displaying it where I want. 我现在想要做的一切就是生成图像并将其显示在我想要的位置。 I want to be able to parametrize that image also. 我也希望能够对该图像进行参数化。 I already created working solution with HTML tag <img src="img.php",x,y> ,where img.php is generating image, but it's setting anyway Content-Type to image and it's just whole way around, as how to get 2 head specifications. 我已经用HTML标签<img src="img.php",x,y>创建了工作解决方案,其中img.php生成图像,但是无论如何,它都将Content-Type设置为图像,并且整个过程都是这样。得到2头规格。 Also I can't set any parameters for this. 我也不能为此设置任何参数。

You can only commit one Content-Type per response. 每个响应只能提交一个Content-Type You can respond with an image or you can respond with a page, but not both. 您可以使用图像进行响应,也可以使用页面进行响应,但不能两者都进行。 They must be two separate HTTP requests. 它们必须是两个单独的HTTP请求。

One way to solve this is to have your img.php script serve the image (as it sounds like it already is), and then a different script which serves your HTML. 解决此问题的一种方法是让img.php脚本提供图像(听起来好像已经),然后由另一个脚本提供HTML。 The HTML it serves will reference the img.php script, such as: 它提供的HTML将引用img.php脚本,例如:

<img src="img.php?x=4&y=5" alt="" />

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

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