php imagejpeg mime 类型
我正在使用 php 的 imagejpeg 将 GD 图像资源保存到文件中,这样做:
imagejpeg($im, '../images/' . $image_id . '.jpg');
它工作正常,但根据我的浏览器,它尝试将文件读取为文本/纯文本:
资源解释为图像,但以 MIME 类型文本/纯文本传输。
在保存文件之前是否有一个步骤,我应该做以确保它使用正确的地雷类型?
我使用的是 Windows (XAMPP),这可能是 Windows 问题吗? 编辑:不。我刚刚在linux服务器上测试过。
就实际显示而言,它只是普通的 html 。 我的上传代码应该将文件保存为服务器中的纯 jpeg。它只是没有用正确的哑剧类型保存它。
谢谢
I am using php's imagejpeg to save a GD image resource to a file, doing this:
imagejpeg($im, '../images/' . $image_id . '.jpg');
It works fine, but according to my browser, it tries to read the file as text/plain:
Resource interpreted as image but transferred with MIME type text/plain.
Is there a step before saving the file that I am supposed to do to make sure it's using the right mine-type?
I am using windows (XAMPP), could it be a Windows issue?
EDIT: nope. I just tested in a linux server.
As far as the actual displaying, it's just plain html .
My upload code is supposed to saves= the file as a plain jpeg in the server. It's just not saving it with the right mime type.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AFAIK,Apache 服务器 - 在标准的开箱即用配置中 - 应该纯粹基于文件扩展名发送内容类型标头。 Apache 甚至不应该查看内容或它最初是如何生成/存储的。
在我的开箱即用的 Apache2 上,文件 conf/mime.types 包含以下行:
image/jpeg jpeg jpg jpe
应该可以做到这一点,对吧?
您能否发布一个测试用例,例如,一个带有两个 img 标签的简单 html 页面:一个用于生成的图像,另一个用于看起来工作正常的标准图像?
最后一个想法:所有浏览器都会出现这种情况吗?也许这是浏览器的问题,而不是服务器的问题?
AFAIK, the Apache server - in standard out of the box configuration - should sent content-type headers purely based upon file extension. Apache shouldn't even even be looking at the contents or how it was originally generated/stored.
On my out-of-the-box Apache2, the file conf/mime.types contains the line:
image/jpeg jpeg jpg jpe
which ought to do it, right?
Can you post a test-case, say, a simple html page with two img tags: one for your generated image, and one for a standard image that seems to work fine?
One last thought: Does it occur in all browsers? Maybe it's a browser issue, not a server one?
听起来您正在将文件的内容转储到浏览器,但实际上并没有告诉浏览器它是什么类型。在将图像输出到浏览器之前尝试添加 Content-type 标头:
It sounds like you're dumping the contents of the file to the browser and not actually telling the browser what type of file it is. Try adding a Content-type header before you output your image to the browser:
您确定没有使用错误的文件扩展名吗?
否则,只需在服务器中放置一个普通图像并确保正确配置 mime 类型即可。
也可能是您的图像数据在处理过程中被损坏。
Are you sure you aren't using the wrong file-extension name?
Otherwise, just put an normal image in the server and make sure the mime-types are properly configured.
It could also be that your image data from the manipulation is corrupted.