在PHP脚本中显示图像 [英] Show image in php script

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

问题描述

我的名字中有一些没有扩展名的图片。现在,当我想打开它们时,某些浏览器无法将数据检测为图像并显示二进制文本。

I have some images without extension in their name. Now, when I want to open them some browsers can't detect data as image and show up the binary text.

什么是正确的脚本(php)这样的图像的地址,检测它的格式并用图像输出设置右边的标题?

What is the right script (php) which will take the address of such image, detect it's format and set right headers with image output?

推荐答案

你可以使用 exif_imagetype 函数可以正确检测它是什么类型的图像,然后 image_type_to_mime_type 函数获取所需的mime类型:

You can use the exif_imagetype function to properly detect what type of image it is, then the image_type_to_mime_type function to get the mime type you want:

header("Content-type: " . image_type_to_mime_type(exif_imagetype("image.jpg")));

你需要内置exif支持。我推荐这个,因为它实际上检查文件的签名而不是盲目地假设基于扩展的mime类型。但是,如果这不是一个选项,您将不得不回退到 fileinfo 正如其他人所提到的那样。

You'll need exif support builtin though. I recommend this because it actually checks the signature of the file versus blindly assuming the mime type based on extension. However, if this isn't an option, you'll have to fallback to fileinfo as others have mentioned.

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

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