php imagejpeg mime 类型

发布于 2024-08-07 18:25:53 字数 429 浏览 3 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(3

缪败 2024-08-14 18:25:53

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?

早乙女 2024-08-14 18:25:53

听起来您正在将文件的内容转储到浏览器,但实际上并没有告诉浏览器它是什么类型。在将图像输出到浏览器之前尝试添加 Content-type 标头:

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

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:

header('Content-type: image/jpeg');
一梦浮鱼 2024-08-14 18:25:53

您确定没有使用错误的文件扩展名吗?
否则,只需在服务器中放置一个普通图像并确保正确配置 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.

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