php 如何处理图像的 MIME 类型“应用程序/八位字节流”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9707204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how handling image's mime type "application/octet-stream"?
提问by user504363
I have a function to make thumbnail of a url image on fly! I always pass to this functions images with type jpg, but the problem appears when I pass an image with ".jpg" extension. but when i try to get its mime type, i found that's "application/octet-stream" .. in this php page, this mime type refers to one of
我有一个功能可以即时制作 url 图像的缩略图!我总是将类型为 jpg 的图像传递给此函数,但是当我传递带有“.jpg”扩展名的图像时会出现问题。但是当我尝试获取它的 mime 类型时,我发现它是“ application/octet-stream”..在这个php 页面中,这个 mime 类型是指其中之一
IMAGETYPE_JPC,IMAGETYPE_JPX,IMAGETYPE_JB2
IMAGETYPE_JPC,IMAGETYPE_JPX,IMAGETYPE_JB2
what I need to modify my function to handle this mime type ??
我需要修改我的函数来处理这种 mime 类型吗??
notice ^^^^^^
注意^^^^^^
function thumb($path,$width,$height) // $path => image url
{
$file_dimensions = getimagesize($path);
$file_type = image_type_to_mime_type($file_dimensions[2]);
list($Cwidth, $Cheight) = getimagesize($path);
if ($file_type=='image/jpeg'||$file_type=='image/pjpeg'){
// Load
$thumb = imagecreatetruecolor($width, $height);
$source = imagecreatefromjpeg($path);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $width, $height, $Cwidth, $Cheight);
header('Content-Type: image/jpeg');
imagejpeg($thumb);
}
else if ($file_type=='application/octet-stream')
{
// ^^^^^ what I should write here
}
else
{
echo "Not supported type";
}
}
回答by hakre
We can't tell you because application/octet-stream
is a sort of general-type-of-binary-file mime-type. It can be everything. You can try with imagecreatefromstring
on the files binary content. But keep fingers crossed ;).
我们不能告诉你,因为application/octet-stream
它是一种通用类型的二进制文件 MIME 类型。它可以是一切。您可以尝试使用imagecreatefromstring
文件二进制内容。但保持手指交叉;)。
The actual issue here is that getimagesize
is independent to the GD library you use for resizing the image. So it provides infos about files GD itself is not able to deal with. So you can just output some sort of "unsupported image type" until you find some additional library that is able to deal with the specific mime- or better saying image-type.
这里的实际问题getimagesize
是独立于您用于调整图像大小的 GD 库。因此,它提供了有关 GD 本身无法处理的文件的信息。因此,您可以只输出某种“不受支持的图像类型”,直到找到一些能够处理特定 mime 或更好的说法图像类型的附加库。
See as well:
另见:
回答by quevedo
I'm working around the same thing just now.
我现在正在处理同样的事情。
I was testing some images, .gif, .jpeg, .png ... using finfo
I found the mime-type you read depends on the constants you use to read the file. More! You read application/octet-stream as mimetype from images! and that info is not wrong. See:
我正在测试一些图像,.gif、.jpeg、.png ...使用finfo
我发现您读取的 mime 类型取决于您用于读取文件的常量。更多的!您从图像中将 application/octet-stream 作为 mimetype 读取!并且该信息没有错。看:
If you use finfo_open() without constants:
如果您使用没有常量的 finfo_open():
<?php
$finfo = finfo_open();
$FileInfo = finfo_file($finfo, $tmp_name);
finfo_close($finfo);
You get the mime type you expect:
你得到你期望的 mime 类型:
If .svg -> HTML document, ASCII text, with very long lines, with no line terminators
If .jpg (from your phone camera) -> JPEG image data, EXIF standard 2.2
If .gif (saved from paint) -> GIF image data, version 89a, w x h
如果 .svg -> HTML 文档,ASCII 文本,有很长的行,没有行终止符
如果 .jpg(来自手机相机)-> JPEG 图像数据,EXIF 标准 2.2
如果 .gif(从油漆中保存)-> GIF 图像数据,版本 89a,wxh
while using a constant like FILEINFO_MIME_TYPE
使用像 FILEINFO_MIME_TYPE 这样的常量时
<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE, $mf); // $mf is a magic file
$FileInfo = finfo_file($finfo, $tmp_name);
finfo_close($finfo);
you got a different value:
你得到了不同的价值:
if .svg -> text/plain
if .jpg (from your phone camera) -> application/octet-stream
if .gif (saved from paint) -> application/octet-stream
如果 .svg -> 文本/纯文本
如果 .jpg(来自您的手机摄像头)-> 应用程序/八位字节流
如果 .gif(从油漆中保存)-> 应用程序/八位字节流
So you must test what you read when testing mimetypes. See Fileinfo Predefined constants
因此,在测试 mimetype 时,您必须测试您阅读的内容。请参阅Fileinfo 预定义常量
Hope it helps
希望能帮助到你
回答by hellojava
In the case of application/octet-stream, You could get the original file name and check its extension. If its jpg you should be good to go
在 application/octet-stream 的情况下,您可以获取原始文件名并检查其扩展名。如果它是 jpg 你应该很高兴去